Class: Pry::CodeFile
Constant Summary collapse
- DEFAULT_EXT =
'.rb'.freeze
- 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 }.freeze
- FILES =
{ %w[Gemfile Rakefile Guardfile Capfile] => :ruby }.freeze
- INITIAL_PWD =
Store the current working directory. This allows show-source etc. to work if your process has changed directory since boot. [Issue #675]
Dir.pwd
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 SyntaxHighlighter type of a file from its extension, or
nilif:unknown.
Constructor Details
#initialize(filename, code_type = type_from_filename(filename)) ⇒ CodeFile
Returns a new instance of CodeFile.
41 42 43 44 |
# File 'lib/pry/code/code_file.rb', line 41 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.
37 38 39 |
# File 'lib/pry/code/code_file.rb', line 37 def code_type @code_type end |
Instance Method Details
#abs_path ⇒ String (private)
Returns absolute path for the given filename.
64 65 66 67 68 |
# File 'lib/pry/code/code_file.rb', line 64 def abs_path code_path.detect { |path| readable?(path) } || raise(MethodSource::SourceNotFoundError, "Cannot open #{@filename.inspect} for reading.") end |
#code ⇒ String
Returns The code contained in the current @filename.
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/pry/code/code_file.rb', line 47 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) 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.
80 81 82 |
# File 'lib/pry/code/code_file.rb', line 80 def code_path [from_pwd, from_pry_init_pwd, *from_load_path] end |
#from_load_path ⇒ String (private)
110 111 112 |
# File 'lib/pry/code/code_file.rb', line 110 def from_load_path $LOAD_PATH.map { |path| File.(@filename, path) } end |
#from_pry_init_pwd ⇒ String (private)
105 106 107 |
# File 'lib/pry/code/code_file.rb', line 105 def from_pry_init_pwd File.(@filename, INITIAL_PWD) end |
#from_pwd ⇒ String (private)
100 101 102 |
# File 'lib/pry/code/code_file.rb', line 100 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.
73 74 75 76 |
# File 'lib/pry/code/code_file.rb', line 73 def readable?(path) File.readable?(path) && !File.directory?(path) || File.readable?(path << DEFAULT_EXT) end |
#type_from_filename(filename, default = :unknown) ⇒ Symbol? (private)
Returns The SyntaxHighlighter type of a file from its
extension, or nil if :unknown.
89 90 91 92 93 94 95 96 97 |
# File 'lib/pry/code/code_file.rb', line 89 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 |