123456789_123456789_123456789_123456789_123456789_

Class: RSpec::Expectations::BlockSnippetExtractor Private

Do not use. This class is for internal use only.
Relationships & Source Files
Namespace Children
Classes:
Inherits: Object
Defined in: rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb

Constant Summary

Class Method Summary

Instance Attribute Summary

  • #method_name readonly Internal use only

    rubocop should properly handle ‘Struct.new {}` as an inner class definition.

  • #proc readonly Internal use only

    rubocop should properly handle ‘Struct.new {}` as an inner class definition.

Instance Method Summary

Class Method Details

.try_extracting_single_line_body_of(proc, method_name)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 9

def self.try_extracting_single_line_body_of(proc, method_name)
  lines = new(proc, method_name).body_content_lines
  return nil unless lines.count == 1
  lines.first
rescue Error
  nil
end

Instance Attribute Details

#method_name (readonly)

rubocop should properly handle ‘Struct.new {}` as an inner class definition.

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 7

attr_reader :proc, :method_name

#proc (readonly)

rubocop should properly handle ‘Struct.new {}` as an inner class definition.

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 7

attr_reader :proc, :method_name

Instance Method Details

#beginning_line_number (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 60

def beginning_line_number
  source_location.last
end

#block_token_extractor (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 39

def block_token_extractor
  @block_token_extractor ||= BlockTokenExtractor.new(method_name, source, beginning_line_number)
end

#body_content_lines

Ideally we should properly handle indentations of multiline snippet, but it’s not implemented yet since because we use result of this method only when it’s a single line and implementing the logic introduces additional complexity.

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 25

def body_content_lines
  raw_body_lines.map(&:strip).reject(&:empty?)
end

#file_path (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 56

def file_path
  source_location.first
end

#raw_body_lines (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 31

def raw_body_lines
  raw_body_snippet.split("\n")
end

#raw_body_snippet (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 35

def raw_body_snippet
  block_token_extractor.body_tokens.map(&:string).join
end

#source (private)

See additional method definition at line 44.

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 50

def source
  raise TargetNotFoundError unless File.exist?(file_path)
  RSpec.world.source_from_file(file_path)
end

#source_location (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 64

def source_location
  proc.source_location || raise(TargetNotFoundError)
end