Class: Warp::Dir::Point
Overview
This class encapsulates the tuple: name + path. It provides convenience accessors to retrieve absolute or realtive path of a point, optionally via a set of predefined filters.
In addition, this class is responsible for serializing and deserializing itself properly.
Instance Attribute Summary collapse
-
#full_path ⇒ Object
Instance Methods.
-
#name ⇒ Object
Instance Methods.
Class Method Summary collapse
- .deserialize(line) ⇒ Object
-
.filtered_paths(path_hash) ⇒ Object
This method creates/defines methods used to access the #full_path component of the Point instance, but enclosing it in a chain of provided filters.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #eql?(another) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(name, full_path) ⇒ Point
constructor
A new instance of Point.
- #inspect ⇒ Object
- #serialize ⇒ Object
- #to_s(width = 0) ⇒ Object
Constructor Details
#initialize(name, full_path) ⇒ Point
Returns a new instance of Point.
46 47 48 49 50 51 |
# File 'lib/warp/dir/point.rb', line 46 def initialize(name, full_path) raise ArgumentError.new ':name is required' if name.nil? raise ArgumentError.new ':full_path is required' if full_path.nil? @full_path = Warp::Dir.absolute full_path @name = name.to_sym end |
Instance Attribute Details
#full_path ⇒ Object
Instance Methods
44 45 46 |
# File 'lib/warp/dir/point.rb', line 44 def full_path @full_path end |
Class Method Details
.deserialize(line) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/warp/dir/point.rb', line 27 def self.deserialize(line) name, path = line.split(/:/) if name.nil? || path.nil? raise Warp::Dir::Errors::StoreFormatError.new( 'warprc file may be corrupt, offending line is: ' + line, line) end self.new(name, path) end |
.filtered_paths(path_hash) ⇒ Object
This method creates/defines methods used to access the #full_path component of the Point instance, but enclosing it in a chain of provided filters.
17 18 19 20 21 22 23 24 25 |
# File 'lib/warp/dir/point.rb', line 17 def self.filtered_paths(path_hash) path_hash.each_pair do |method, filters| define_method method.to_sym do |*args| filters.inject(self.full_path) do |memo, filter| self.send(filter, memo) end end end end |
Instance Method Details
#<=>(other) ⇒ Object
65 66 67 |
# File 'lib/warp/dir/point.rb', line 65 def <=>(other) name <=> other.name end |
#eql?(another) ⇒ Boolean
69 70 71 72 73 74 75 |
# File 'lib/warp/dir/point.rb', line 69 def eql?(another) return false unless another.is_a?(Warp::Dir::Point) %i(name full_path).each do |attribute| return false unless send(attribute) == another.send(attribute) end true end |
#hash ⇒ Object
61 62 63 |
# File 'lib/warp/dir/point.rb', line 61 def hash Digest::SHA1.base64digest("#{full_path.hash}#{name.hash}").hash end |
#inspect ⇒ Object
53 54 55 |
# File 'lib/warp/dir/point.rb', line 53 def inspect sprintf("(#{object_id})[name: '%s', path: '%s']", name, relative_path) end |
#serialize ⇒ Object
77 78 79 |
# File 'lib/warp/dir/point.rb', line 77 def serialize "#{name}:#{full_path}" end |
#to_s(width = 0) ⇒ Object
57 58 59 |
# File 'lib/warp/dir/point.rb', line 57 def to_s(width = 0) sprintf("%#{width}s -> %s", name, relative_path) end |