class Dronr::Register

Constants

CANONICAL_PATH

Attributes

drones[R]

Public Class Methods

load(path) click to toggle source
# File lib/dronr/register.rb, line 34
def self.load(path)
  begin
    yaml = YAML.load_file(path)
  rescue Psych::SyntaxError
    raise MalformedRegisterError.new(yaml), 'is not valid YAML'
  end

  unless yaml.has_key?('incoming') && yaml.has_key?('bundled') && yaml.has_key?('finished')
    raise MalformedRegisterError.new(yaml), 'is missing required keys (incoming, bundled, finished)'
  end

  drones = %w{incoming bundled finished}.map do |state|
    (yaml[state] || []).map do |drone_name|
      klass = Drone.find_class_by_human_name(drone_name)
      klass.new(state.to_sym)
    end
  end.flatten

  new(drones).tap do |register|
    unless register.valid?
      raise InvalidRegisterError.new(register), "Couldnt load register at #{path}"
    end
  end
end
load_canonical() click to toggle source
# File lib/dronr/register.rb, line 29
def self.load_canonical
  canonical_path = File.expand_path '../register/canonical.yml', __FILE__
  load(canonical_path)
end
new(drones=[]) click to toggle source
# File lib/dronr/register.rb, line 25
def initialize(drones=[])
  @drones = drones
end

Public Instance Methods

commit(path) click to toggle source
# File lib/dronr/register.rb, line 83
def commit(path)
  File.open(path, 'w+') do |f|
    f.write(to_yaml)
  end
end
sort_by_example_register(example_register) click to toggle source
# File lib/dronr/register.rb, line 69
def sort_by_example_register(example_register)
  @drones = example_register.drones.map do |drone|
    @drones.detect{ |d| d.class == drone.class }
  end.compact
end
to_yaml() click to toggle source
# File lib/dronr/register.rb, line 75
def to_yaml
  {
    'incoming' => incoming_drones.map(&:human_name).presence,
    'bundled' => bundled_drones.map(&:human_name).presence,
    'finished' => finished_drones.map(&:human_name).presence
  }.to_yaml
end
valid?() click to toggle source
# File lib/dronr/register.rb, line 59
def valid?
  has_valid_drones = drones.all? do |drone|
    drone && (drone.class < Drone)
  end

  has_no_duplicate_drones = (drones.count == drones.map(&:class).uniq.count)

  has_valid_drones && has_no_duplicate_drones
end