class Brakeman::FilePath

Class to represent file paths within Brakeman. FilePath objects track both the relative and absolute paths to make it easier to manage paths.

Attributes

absolute[R]
relative[R]

Public Class Methods

from_app_tree(app_tree, path) click to toggle source

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.

# 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.expand_path(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
new(absolute_path, relative_path) click to toggle source

Create a new FilePath with the given absolute and relative paths.

# File lib/brakeman/file_path.rb, line 33
def initialize absolute_path, relative_path
  @absolute = absolute_path
  @relative = relative_path
end

Public Instance Methods

<=>(rhs) click to toggle source

Compare FilePaths. Raises an ArgumentError unless both objects are FilePaths.

# File lib/brakeman/file_path.rb, line 54
def <=> rhs
  raise ArgumentError unless rhs.is_a? Brakeman::FilePath
  self.relative <=> rhs.relative
end
==(rhs) click to toggle source

Compare FilePaths. Raises an ArgumentError unless both objects are FilePaths.

# File lib/brakeman/file_path.rb, line 60
def == rhs
  return false unless rhs.is_a? Brakeman::FilePath

  self.absolute == rhs.absolute
end
basename() click to toggle source

Just the file name, no path

# File lib/brakeman/file_path.rb, line 39
def basename
  @basename ||= File.basename(self.relative)
end
eql?(rhs) click to toggle source
# File lib/brakeman/file_path.rb, line 80
def eql? rhs
  @absolute == rhs.absolute and
    @relative == rhs.relative
end
exists?() click to toggle source

Check if absolute path exists.

# File lib/brakeman/file_path.rb, line 49
def exists?
  File.exist? self.absolute
end
hash() click to toggle source
# File lib/brakeman/file_path.rb, line 76
def hash
  @hash ||= [@absolute, @relative].hash
end
read() click to toggle source

Read file from absolute path.

# File lib/brakeman/file_path.rb, line 44
def read
  File.read self.absolute
end
to_s() click to toggle source

Returns a string with the absolute path.

# File lib/brakeman/file_path.rb, line 72
def to_s
  self.to_str
end
to_str() click to toggle source

Returns a string with the absolute path.

# File lib/brakeman/file_path.rb, line 67
def to_str
  self.absolute
end