class Warp::Dir::Store

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.

Attributes

config[R]
points_collection[R]
serializer[R]

Public Class Methods

new(config, serializer_class = Warp::Dir::Serializer.default) click to toggle source
# 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

Public Instance Methods

<<(value) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

add to memory representation only

# 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!() click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/warp/dir/store.rb, line 36
def first
  points_collection.to_a.sort.first
end
insert(**opts) click to toggle source

a version of add that save right after.

# File lib/warp/dir/store.rb, line 83
def insert(**opts)
  add(**opts)
  save!
end
last() click to toggle source
# File lib/warp/dir/store.rb, line 40
def last
  points_collection.to_a.sort.last
end
points() click to toggle source
# File lib/warp/dir/store.rb, line 55
def points
  points_collection.to_a
end
remove(point_name: nil) click to toggle source
# 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) click to toggle source
# File lib/warp/dir/store.rb, line 130
def replace(point, existing_point)
  remove(point_name: existing_point)
  insert(point: point)
end
restore!() click to toggle source
# File lib/warp/dir/store.rb, line 27
def restore!
  @points_collection = Set.new
  self.serializer.restore!
end
save!() click to toggle source
# File lib/warp/dir/store.rb, line 78
def save!
  serializer.persist!
end