Class: Pry::CodeFile
Constant Summary collapse
- 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
-
#code_type ⇒ Symbol
readonly
The type of code stored in this wrapper.
Instance Method Summary collapse
-
#abs_path ⇒ String
private
Absolute path for the given
filename
. -
#code ⇒ String
The code contained in the current
@filename
. -
#code_path ⇒ Array
private
All the paths that contain code that Pry can use for its API's.
- #from_load_path ⇒ String private
- #from_pry_init_pwd ⇒ String private
- #from_pwd ⇒ String private
-
#initialize(filename, code_type = type_from_filename(filename)) ⇒ CodeFile
constructor
A new instance of CodeFile.
-
#readable?(path) ⇒ Boolean
private
If the path, with or without the default ext, is a readable file then
true
, otherwisefalse
. -
#type_from_filename(filename, default = :unknown) ⇒ Symbol?
private
The CodeRay type of a file from its extension, or
nil
if:unknown
.
Constructor Details
#initialize(filename, code_type = type_from_filename(filename)) ⇒ CodeFile
Returns a new instance of 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_type ⇒ Symbol (readonly)
Returns The type of code stored in this wrapper.
29 30 31 |
# File 'lib/pry/code/code_file.rb', line 29 def code_type @code_type end |
Instance Method Details
#abs_path ⇒ String (private)
Returns absolute path for the given filename
.
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 |
#code ⇒ String
Returns The code contained in the current @filename
.
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_path ⇒ Array (private)
Returns All the paths that contain code that Pry can use for its API's. Skips directories.
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_path ⇒ String (private)
104 105 106 |
# File 'lib/pry/code/code_file.rb', line 104 def from_load_path $LOAD_PATH.map { |path| File.(@filename, path) } end |
#from_pry_init_pwd ⇒ String (private)
99 100 101 |
# File 'lib/pry/code/code_file.rb', line 99 def from_pry_init_pwd File.(@filename, Pry::INITIAL_PWD) end |
#from_pwd ⇒ String (private)
94 95 96 |
# File 'lib/pry/code/code_file.rb', line 94 def from_pwd File.(@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
.
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)
Returns The CodeRay type of a file from its extension, or
nil
if :unknown
.
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 |