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.

Returns:

  • (Hash)
{
  %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) => :ruby,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of CodeFile

Parameters:

  • filename (String)

    The name of a file with code to be detected

  • code_type (Symbol) (defaults to: type_from_filename(filename))

    The type of code the filename contains



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

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

Instance Attribute Details

#code_typeSymbol (readonly)

Returns The type of code stored in this wrapper.

Returns:

  • (Symbol)

    The type of code stored in this wrapper.



25
26
27
# File 'lib/pry/code/code_file.rb', line 25

def code_type
  @code_type
end

Instance Method Details

#abs_pathString (private)

Returns absolute path for the given filename.

Returns:

  • (String)

    absolute path for the given filename.

Raises:

  • (MethodSource::SourceNotFoundError)

    if the filename is not readable for some reason.



54
55
56
57
58
# File 'lib/pry/code/code_file.rb', line 54

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

#codeString

Returns The code contained in the current @filename.

Returns:

  • (String)

    The code contained in the current @filename.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pry/code/code_file.rb', line 35

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)

Returns All the paths that contain code that Pry can use for its API's. Skips directories.

Returns:

  • (Array)

    All the paths that contain code that Pry can use for its API's. Skips directories.



70
71
72
# File 'lib/pry/code/code_file.rb', line 70

def code_path
  [from_pwd, from_pry_init_pwd, *from_load_path]
end

#from_load_pathString (private)

Returns:

  • (String)


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

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

#from_pry_init_pwdString (private)

Returns:

  • (String)


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

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

#from_pwdString (private)

Returns:

  • (String)


88
89
90
# File 'lib/pry/code/code_file.rb', line 88

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

#readable?(path) ⇒ Boolean (private)

Returns if the path, with or without the default ext, is a readable file then true, otherwise false.

Parameters:

  • path (String)

Returns:

  • (Boolean)

    if the path, with or without the default ext, is a readable file then true, otherwise false.



63
64
65
66
# File 'lib/pry/code/code_file.rb', line 63

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

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

Returns The CodeRay type of a file from its extension, or nil if :unknown.

Parameters:

  • filename (String)
  • default (Symbol) (defaults to: :unknown)

    (:unknown) the file type to assume if none could be detected.

Returns:

  • (Symbol, nil)

    The CodeRay type of a file from its extension, or nil if :unknown.



79
80
81
82
83
84
85
# File 'lib/pry/code/code_file.rb', line 79

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

  code_type || default
end