class Pry::CodeFile

Constants

DEFAULT_EXT
EXTENSIONS

List of all supported languages. @return [Hash]

Attributes

code_type[R]

@return [Symbol] The type of code stored in this wrapper.

Public Class Methods

new(filename, code_type = type_from_filename(filename)) click to toggle source

@param [String] filename The name of a file with code to be detected @param [Symbol] #code_type The type of code the `filename` contains

# 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

Public Instance Methods

code() click to toggle source

@return [String] The code contained in the current `@filename`.

# 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

Private Instance Methods

abs_path() click to toggle source

@raise [MethodSource::SourceNotFoundError] if the `filename` is not

readable for some reason.

@return [String] absolute path for the given `filename`.

# 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
code_path() click to toggle source

@return [Array] All the paths that contain code that Pry can use for its

API's. Skips directories.
# File lib/pry/code/code_file.rb, line 70
def code_path
  [from_pwd, from_pry_init_pwd, *from_load_path]
end
from_load_path() click to toggle source

@return [String]

# 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_pwd() click to toggle source

@return [String]

# File lib/pry/code/code_file.rb, line 93
def from_pry_init_pwd
  File.expand_path(@filename, Pry::INITIAL_PWD)
end
from_pwd() click to toggle source

@return [String]

# File lib/pry/code/code_file.rb, line 88
def from_pwd
  File.expand_path(@filename, Dir.pwd)
end
readable?(path) click to toggle source

@param [String] path @return [Boolean] if the path, with or without the default ext,

is a readable file then `true`, otherwise `false`.
# 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) click to toggle source

@param [String] filename @param [Symbol] default (:unknown) the file type to assume if none could be

detected.

@return [Symbol, nil] The CodeRay type of a file from its extension, or

`nil` if `:unknown`.
# 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