class Anyway::Loaders::Registry

Attributes

registry[R]

Public Class Methods

new() click to toggle source
# File lib/anyway/loaders.rb, line 10
def initialize
  @registry = []
end

Public Instance Methods

append(id, handler = nil, &block) click to toggle source
# File lib/anyway/loaders.rb, line 19
def append(id, handler = nil, &block)
  handler ||= block
  insert_at(registry.size, id, handler)
end
delete(id) click to toggle source
# File lib/anyway/loaders.rb, line 47
def delete(id)
  find(id).then do |id_to_handler|
    raise ArgumentError, "Loader with ID #{id} hasn't been registered" if id_to_handler.nil?
    registry.delete id_to_handler
  end
end
each(&block) click to toggle source
# File lib/anyway/loaders.rb, line 54
def each(&block)
  registry.each(&block)
end
find(id) click to toggle source
# File lib/anyway/loaders.rb, line 68
def find(id)
  registry.find { |(hid, _)| hid == id }
end
freeze() click to toggle source
# File lib/anyway/loaders.rb, line 58
  def freeze() = registry.freeze

  private

  def insert_at(index, id, handler)
    raise ArgumentError, "Loader with ID #{id} has been already registered" unless find(id).nil?

    registry.insert(index, [id, handler])
  end

  def find(id)
    registry.find { |(hid, _)| hid == id }
  end
end
insert_after(another_id, id, handler = nil, &block) click to toggle source
# File lib/anyway/loaders.rb, line 32
def insert_after(another_id, id, handler = nil, &block)
  ind = registry.find_index { |(hid, _)| hid == another_id }
  raise ArgumentError, "Loader with ID #{another_id} hasn't been registered" if ind.nil?

  handler ||= block
  insert_at(ind + 1, id, handler)
end
insert_at(index, id, handler) click to toggle source
# File lib/anyway/loaders.rb, line 62
def insert_at(index, id, handler)
  raise ArgumentError, "Loader with ID #{id} has been already registered" unless find(id).nil?

  registry.insert(index, [id, handler])
end
insert_before(another_id, id, handler = nil, &block) click to toggle source
# File lib/anyway/loaders.rb, line 24
def insert_before(another_id, id, handler = nil, &block)
  ind = registry.find_index { |(hid, _)| hid == another_id }
  raise ArgumentError, "Loader with ID #{another_id} hasn't been registered" if ind.nil?

  handler ||= block
  insert_at(ind, id, handler)
end
override(id, handler) click to toggle source
# File lib/anyway/loaders.rb, line 40
def override(id, handler)
  find(id).then do |id_to_handler|
    raise ArgumentError, "Loader with ID #{id} hasn't been registered" if id_to_handler.nil?
    id_to_handler[1] = handler
  end
end
prepend(id, handler = nil, &block) click to toggle source
# File lib/anyway/loaders.rb, line 14
def prepend(id, handler = nil, &block)
  handler ||= block
  insert_at(0, id, handler)
end