Class: Brakeman::FilePath
- Inherits:
-
Object
- Object
- Brakeman::FilePath
- Defined in:
- lib/brakeman/file_path.rb
Overview
Class to represent file paths within Brakeman. FilePath objects track both the relative and absolute paths to make it easier to manage paths.
Instance Attribute Summary collapse
-
#absolute ⇒ Object
readonly
Returns the value of attribute absolute.
-
#relative ⇒ Object
readonly
Returns the value of attribute relative.
Class Method Summary collapse
-
.from_app_tree(app_tree, path) ⇒ Object
Create a new FilePath using an AppTree object.
Instance Method Summary collapse
-
#<=>(rhs) ⇒ Object
Compare FilePaths.
-
#==(rhs) ⇒ Object
Compare FilePaths.
-
#basename ⇒ Object
Just the file name, no path.
- #eql?(rhs) ⇒ Boolean
-
#exists? ⇒ Boolean
Check if absolute path exists.
- #hash ⇒ Object
-
#initialize(absolute_path, relative_path) ⇒ FilePath
constructor
Create a new FilePath with the given absolute and relative paths.
-
#read ⇒ Object
Read file from absolute path.
-
#to_s ⇒ Object
Returns a string with the absolute path.
-
#to_str ⇒ Object
Returns a string with the absolute path.
Constructor Details
#initialize(absolute_path, relative_path) ⇒ FilePath
Create a new FilePath with the given absolute and relative paths.
33 34 35 36 |
# File 'lib/brakeman/file_path.rb', line 33 def initialize absolute_path, relative_path @absolute = absolute_path @relative = relative_path end |
Instance Attribute Details
#absolute ⇒ Object (readonly)
Returns the value of attribute absolute.
8 9 10 |
# File 'lib/brakeman/file_path.rb', line 8 def absolute @absolute end |
#relative ⇒ Object (readonly)
Returns the value of attribute relative.
8 9 10 |
# File 'lib/brakeman/file_path.rb', line 8 def relative @relative end |
Class Method Details
.from_app_tree(app_tree, path) ⇒ Object
Create a new FilePath using an AppTree object.
Note that if the path is already a FilePath, that path will be returned unaltered.
Additionally, paths are cached. If the absolute path already has a FilePath in the cache, that existing FilePath will be returned.
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/brakeman/file_path.rb', line 18 def self.from_app_tree app_tree, path return path if path.is_a? Brakeman::FilePath absolute = app_tree.(path).freeze if fp = @cache[absolute] return fp end relative = app_tree.relative_path(path).freeze self.new(absolute, relative).tap { |fp| @cache[absolute] = fp } end |
Instance Method Details
#<=>(rhs) ⇒ Object
Compare FilePaths. Raises an ArgumentError unless both objects are FilePaths.
54 55 56 57 |
# File 'lib/brakeman/file_path.rb', line 54 def <=> rhs raise ArgumentError unless rhs.is_a? Brakeman::FilePath self.relative <=> rhs.relative end |
#==(rhs) ⇒ Object
Compare FilePaths. Raises an ArgumentError unless both objects are FilePaths.
60 61 62 63 64 |
# File 'lib/brakeman/file_path.rb', line 60 def == rhs return false unless rhs.is_a? Brakeman::FilePath self.absolute == rhs.absolute end |
#basename ⇒ Object
Just the file name, no path
39 40 41 |
# File 'lib/brakeman/file_path.rb', line 39 def basename @basename ||= File.basename(self.relative) end |
#eql?(rhs) ⇒ Boolean
80 81 82 83 |
# File 'lib/brakeman/file_path.rb', line 80 def eql? rhs @absolute == rhs.absolute and @relative == rhs.relative end |
#exists? ⇒ Boolean
Check if absolute path exists.
49 50 51 |
# File 'lib/brakeman/file_path.rb', line 49 def exists? File.exist? self.absolute end |
#hash ⇒ Object
76 77 78 |
# File 'lib/brakeman/file_path.rb', line 76 def hash @hash ||= [@absolute, @relative].hash end |
#read ⇒ Object
Read file from absolute path.
44 45 46 |
# File 'lib/brakeman/file_path.rb', line 44 def read File.read self.absolute end |
#to_s ⇒ Object
Returns a string with the absolute path.
72 73 74 |
# File 'lib/brakeman/file_path.rb', line 72 def to_s self.to_str end |
#to_str ⇒ Object
Returns a string with the absolute path.
67 68 69 |
# File 'lib/brakeman/file_path.rb', line 67 def to_str self.absolute end |