Class: Warp::Dir::Store

Inherits:
Object show all
Extended by:
Forwardable
Defined in:
lib/warp/dir/store.rb

Overview

We want to keep around only one store, so we follow the Singleton patter. Due to us wanting to pass parameters to the singleton class’s #new method, using standard Singleton becomes more hassle than it’s worth.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, serializer_class = Warp::Dir::Serializer.default) ⇒ Store

Returns a new instance of Store.



20
21
22
23
24
25
# File 'lib/warp/dir/store.rb', line 20

def initialize(config, serializer_class = Warp::Dir::Serializer.default)
  @config            = config
  serializer_class   ||= Warp::Dir::Serializer.default
  @serializer        = serializer_class.new(self)
  restore!
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



18
19
20
# File 'lib/warp/dir/store.rb', line 18

def config
  @config
end

#points_collectionObject (readonly)

Returns the value of attribute points_collection.



18
19
20
# File 'lib/warp/dir/store.rb', line 18

def points_collection
  @points_collection
end

#serializerObject (readonly)

Returns the value of attribute serializer.



18
19
20
# File 'lib/warp/dir/store.rb', line 18

def serializer
  @serializer
end

Instance Method Details

#<<(value) ⇒ Object

Raises:

  • (ArgumentError)


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

def <<(value)
  raise ArgumentError.new("#{value} is not a Point") unless value.is_a?(Point)
  self.add(point: value)
end

#[](name) ⇒ Object



32
33
34
# File 'lib/warp/dir/store.rb', line 32

def [](name)
  find_point(name)
end

#add(point: nil, point_name: nil, point_path: nil, overwrite: false) ⇒ Object

add to memory representation only



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/warp/dir/store.rb', line 82

def add(point: nil,
        point_name: nil,
        point_path: nil,
        overwrite: false)
  unless point
    if !(point_name && point_path)
      raise ArgumentError.new('invalid arguments')
    end
    point = Warp::Dir::Point.new(point_name, point_path)
  end

  # Three use-cases here.
  # if we found this WarpPoint by name, and it's path is different from the incoming...
  existing = begin
    self[point]
  rescue Warp::Dir::Errors::PointNotFound
    nil
  end

  if existing.eql?(point) # found, but it's identical
    if config.debug
      puts "Point being added #{point} is identical to existing #{existing}, ignore."
    end
    return
  elsif existing # found, but it's different
    if overwrite # replace it
      if config.debug
       puts "Point being added #{point} is replacing the existing #{existing}."
      end
      replace(point, existing)
    else # reject it
      if config.debug
       puts "Point being added #{point} already exists, but no overwrite was set"
      end
      raise Warp::Dir::Errors::PointAlreadyExists.new(point)
    end
  else # no lookup found
    self.points_collection << point # add it
  end
end

#find_point(name_or_point) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/warp/dir/store.rb', line 59

def find_point(name_or_point)
  return if name_or_point.nil?
  result = if name_or_point.is_a?(Warp::Dir::Point)
             self.find_point(name_or_point.name)
           else
             matching_set = self.points_collection.classify { |p| p.name.to_sym }[name_or_point.to_sym]
             (matching_set && !matching_set.empty?) ? matching_set.first : nil
           end
  raise ::Warp::Dir::Errors::PointNotFound.new(name_or_point) unless result
  result
end

#firstObject



36
37
38
# File 'lib/warp/dir/store.rb', line 36

def first
  points_collection.to_a.sort.first
end

#insert(*args) ⇒ Object

a version of add that save right after.



76
77
78
79
# File 'lib/warp/dir/store.rb', line 76

def insert(*args)
  add(*args)
  save!
end

#lastObject



40
41
42
# File 'lib/warp/dir/store.rb', line 40

def last
  points_collection.to_a.sort.last
end

#pointsObject



55
56
57
# File 'lib/warp/dir/store.rb', line 55

def points
  points_collection.to_a
end

#remove(point_name: nil) ⇒ Object



49
50
51
52
53
# File 'lib/warp/dir/store.rb', line 49

def remove(point_name: nil)
  point = point_name.is_a?(Warp::Dir::Point) ? point_name : self[point_name]
  self.points_collection.delete(point) if point
  save!
end

#replace(point, existing_point) ⇒ Object



123
124
125
126
# File 'lib/warp/dir/store.rb', line 123

def replace(point, existing_point)
  remove(point_name: existing_point)
  insert(point: point)
end

#restore!Object



27
28
29
30
# File 'lib/warp/dir/store.rb', line 27

def restore!
  @points_collection = Set.new
  self.serializer.restore!
end

#save!Object



71
72
73
# File 'lib/warp/dir/store.rb', line 71

def save!
  serializer.persist!
end