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.



45
46
47
48
49
50
# File 'lib/warp/dir/point.rb', line 45

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



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

def full_path
  @full_path
end

#nameObject

Instance Methods



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

def name
  @name
end

Class Method Details

.deserialize(line) ⇒ Object



27
28
29
30
31
32
33
34
# 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



72
73
74
# File 'lib/warp/dir/point.rb', line 72

def <=>(other)
  name <=> other.name
end

#==(another) ⇒ Object Also known as: eql?



76
77
78
79
80
81
82
# File 'lib/warp/dir/point.rb', line 76

def ==(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

#exist?Boolean

Returns:

  • (Boolean)


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

def exist?
  ::Dir.exist?(full_path)
end

#hashObject



68
69
70
# File 'lib/warp/dir/point.rb', line 68

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

#inspectObject



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

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

#missing?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/warp/dir/point.rb', line 56

def missing?
  !exist?
end

#serializeObject



87
88
89
# File 'lib/warp/dir/point.rb', line 87

def serialize
  "#{name}:#{full_path}"
end

#to_s(width = 0) ⇒ Object



64
65
66
# File 'lib/warp/dir/point.rb', line 64

def to_s(width = 0)
  sprintf("%#{width}s  ->  %s", name, relative_path)
end