Class: Warp::Dir::Store
- 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
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#points_collection ⇒ Object
readonly
Returns the value of attribute points_collection.
-
#serializer ⇒ Object
readonly
Returns the value of attribute serializer.
Instance Method Summary collapse
- #<<(value) ⇒ Object
- #[](name) ⇒ Object
-
#add(point: nil, point_name: nil, point_path: nil, overwrite: false) ⇒ Object
add to memory representation only.
- #clean! ⇒ Object
- #find_point(name_or_point) ⇒ Object
- #first ⇒ Object
-
#initialize(config, serializer_class = Warp::Dir::Serializer.default) ⇒ Store
constructor
A new instance of Store.
-
#insert(**opts) ⇒ Object
a version of add that save right after.
- #last ⇒ Object
- #points ⇒ Object
- #remove(point_name: nil) ⇒ Object
- #replace(point, existing_point) ⇒ Object
- #restore! ⇒ Object
- #save! ⇒ Object
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
#config ⇒ Object (readonly)
Returns the value of attribute config.
18 19 20 |
# File 'lib/warp/dir/store.rb', line 18 def config @config end |
#points_collection ⇒ Object (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 |
#serializer ⇒ Object (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
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
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 122 123 124 125 126 127 128 |
# File 'lib/warp/dir/store.rb', line 89 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 |
#clean! ⇒ Object
71 72 73 74 75 76 |
# File 'lib/warp/dir/store.rb', line 71 def clean! points_collection.select(&:missing?).tap do |p| points_collection.reject!(&:missing?) save! 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 |
#first ⇒ Object
36 37 38 |
# File 'lib/warp/dir/store.rb', line 36 def first points_collection.to_a.sort.first end |
#insert(**opts) ⇒ Object
a version of add that save right after.
83 84 85 86 |
# File 'lib/warp/dir/store.rb', line 83 def insert(**opts) add(**opts) save! end |
#last ⇒ Object
40 41 42 |
# File 'lib/warp/dir/store.rb', line 40 def last points_collection.to_a.sort.last end |
#points ⇒ Object
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
130 131 132 133 |
# File 'lib/warp/dir/store.rb', line 130 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
78 79 80 |
# File 'lib/warp/dir/store.rb', line 78 def save! serializer.persist! end |