Class: Warp::Dir::Point

Inherits:
Object show all
Defined in:
lib/warp/dir/point.rb

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

Class Method Summary collapse

Instance Method Summary collapse

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_pathObject

Instance Methods



44
45
46
# File 'lib/warp/dir/point.rb', line 44

def full_path
  @full_path
end

#nameObject

Instance Methods



44
45
46
# File 'lib/warp/dir/point.rb', line 44

def name
  @name
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

Returns:

  • (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

#hashObject



61
62
63
# File 'lib/warp/dir/point.rb', line 61

def hash
  Digest::SHA1.base64digest("#{full_path.hash}#{name.hash}").hash
end

#inspectObject



53
54
55
# File 'lib/warp/dir/point.rb', line 53

def inspect
  sprintf("(#{object_id})[name: '%s', path: '%s']", name, relative_path)
end

#serializeObject



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