Class: Pry::CInternals::CodeFetcher

Inherits:
Object
  • Object
show all
Includes:
Helpers::Text
Defined in:
lib/pry-doc/pry_ext/show_source_with_c_internals/code_fetcher.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line_number_style: nil) ⇒ CodeFetcher

Returns a new instance of CodeFetcher.



32
33
34
35
# File 'lib/pry-doc/pry_ext/show_source_with_c_internals/code_fetcher.rb', line 32

def initialize(line_number_style: nil)
  @line_number_style = line_number_style
  @symbol_extractor = SymbolExtractor.new(self.class.ruby_source_folder)
end

Class Attribute Details

.ruby_source_folderObject

Returns the value of attribute ruby_source_folder.



11
12
13
# File 'lib/pry-doc/pry_ext/show_source_with_c_internals/code_fetcher.rb', line 11

def ruby_source_folder
  @ruby_source_folder
end

.ruby_source_installerObject

Returns the value of attribute ruby_source_installer.



12
13
14
# File 'lib/pry-doc/pry_ext/show_source_with_c_internals/code_fetcher.rb', line 12

def ruby_source_installer
  @ruby_source_installer
end

.symbol_map=(value) ⇒ Object

Sets the attribute symbol_map

Parameters:

  • value

    the value to set the attribute symbol_map to.



13
14
15
# File 'lib/pry-doc/pry_ext/show_source_with_c_internals/code_fetcher.rb', line 13

def symbol_map=(value)
  @symbol_map = value
end

Instance Attribute Details

#line_number_styleObject (readonly)

Returns the value of attribute line_number_style.



29
30
31
# File 'lib/pry-doc/pry_ext/show_source_with_c_internals/code_fetcher.rb', line 29

def line_number_style
  @line_number_style
end

#symbol_extractorObject (readonly)

Returns the value of attribute symbol_extractor.



30
31
32
# File 'lib/pry-doc/pry_ext/show_source_with_c_internals/code_fetcher.rb', line 30

def symbol_extractor
  @symbol_extractor
end

Class Method Details

.ruby_versionObject

The Ruby version that corresponds to a downloadable release Note that after Ruby 2.1.0 they exclude the patchlevel from the release name



18
19
20
21
22
23
24
# File 'lib/pry-doc/pry_ext/show_source_with_c_internals/code_fetcher.rb', line 18

def self.ruby_version
  if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.1.0")
    RUBY_VERSION.tr(".", "_")
  else
    RUBY_VERSION.tr(".", "_") + "_#{RUBY_PATCHLEVEL}"
  end
end

Instance Method Details

#fetch_all_definitions(symbol) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pry-doc/pry_ext/show_source_with_c_internals/code_fetcher.rb', line 37

def fetch_all_definitions(symbol)
  infos = self.class.symbol_map[symbol]
  return unless infos

  result = ""
  infos.count.times do |index|
    result << fetch_first_definition(symbol, index).first << "\n"
  end

  return [result, infos.first.file]
end

#fetch_first_definition(symbol, index = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pry-doc/pry_ext/show_source_with_c_internals/code_fetcher.rb', line 49

def fetch_first_definition(symbol, index=nil)
  infos = self.class.symbol_map[symbol]
  return unless infos

  info = infos[index || 0]
  code = symbol_extractor.extract(info)

  result = ""
  result << "\n#{bold('From: ')}#{info.file} @ line #{info.line}:\n"
  result << "#{bold('Number of implementations:')} #{infos.count}\n" unless index
  result << "#{bold('Number of lines: ')} #{code.lines.count}\n\n"
  result << Pry::Code.new(code, start_line_for(info.line), :c).
              with_line_numbers(use_line_numbers?).highlighted

  return [result, info.file]

end