Class: Pry::CodeFile

Inherits:
Object show all
Defined in:
lib/pry/code/code_file.rb

Constant Summary

DEFAULT_EXT =
'.rb'
EXTENSIONS =

List of all supported languages.

{
  %w(.py)        => :python,
  %w(.js)        => :javascript,
  %w(.css)       => :css,
  %w(.xml)       => :xml,
  %w(.php)       => :php,
  %w(.html)      => :html,
  %w(.diff)      => :diff,
  %w(.java)      => :java,
  %w(.json)      => :json,
  %w(.c .h)      => :c,
  %w(.rhtml)     => :rhtml,
  %w(.yaml .yml) => :yaml,
  %w(.cpp .hpp .cc .h .cxx) => :cpp,
  %w(.rb .ru .irbrc .gemspec .pryrc .rake) => :ruby,
}
FILES =
{
  %w(Gemfile Rakefile Guardfile Capfile) => :ruby
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, code_type = type_from_filename(filename)) ⇒ CodeFile



33
34
35
36
# File 'lib/pry/code/code_file.rb', line 33

def initialize(filename, code_type = type_from_filename(filename))
  @filename = filename
  @code_type = code_type
end

Instance Attribute Details

#code_typeSymbol (readonly)



29
30
31
# File 'lib/pry/code/code_file.rb', line 29

def code_type
  @code_type
end

Instance Method Details

#abs_pathString (private)

Returns absolute path for the given filename.

Raises:

  • (MethodSource::SourceNotFoundError)

    if the filename is not readable for some reason.



58
59
60
61
62
# File 'lib/pry/code/code_file.rb', line 58

def abs_path
  code_path.detect { |path| readable?(path) } or
    raise MethodSource::SourceNotFoundError,
          "Cannot open #{ @filename.inspect } for reading."
end

#codeString



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pry/code/code_file.rb', line 39

def code
  if @filename == Pry.eval_path
    Pry.line_buffer.drop(1)
  elsif Pry::Method::Patcher.code_for(@filename)
    Pry::Method::Patcher.code_for(@filename)
  elsif RbxPath.is_core_path?(@filename)
    File.read(RbxPath.convert_path_to_full(@filename))
  else
    path = abs_path
    @code_type = type_from_filename(path)
    File.read(path)
  end
end

#code_pathArray (private)



74
75
76
# File 'lib/pry/code/code_file.rb', line 74

def code_path
  [from_pwd, from_pry_init_pwd, *from_load_path]
end

#from_load_pathString (private)



104
105
106
# File 'lib/pry/code/code_file.rb', line 104

def from_load_path
  $LOAD_PATH.map { |path| File.expand_path(@filename, path) }
end

#from_pry_init_pwdString (private)



99
100
101
# File 'lib/pry/code/code_file.rb', line 99

def from_pry_init_pwd
  File.expand_path(@filename, Pry::INITIAL_PWD)
end

#from_pwdString (private)



94
95
96
# File 'lib/pry/code/code_file.rb', line 94

def from_pwd
  File.expand_path(@filename, Dir.pwd)
end

#readable?(path) ⇒ Boolean (private)



67
68
69
70
# File 'lib/pry/code/code_file.rb', line 67

def readable?(path)
  File.readable?(path) && !File.directory?(path) or
    File.readable?(path << DEFAULT_EXT)
end

#type_from_filename(filename, default = :unknown) ⇒ Symbol? (private)



83
84
85
86
87
88
89
90
91
# File 'lib/pry/code/code_file.rb', line 83

def type_from_filename(filename, default = :unknown)
  _, @code_type = EXTENSIONS.find do |k, _|
    k.any? { |ext| ext == File.extname(filename) }
  end || FILES.find do |k, _|
    k.any? { |file_name| file_name == File.basename(filename) }
  end

  code_type || default
end