class EditInPlace::Registrar

{Registrar} is a class that is capable of storing a list of objects registered with symbol names. Note that it makes no attempt to validate the objects registered. If such validation is required feel free to subclass {Registrar} and override the {#validate_registration!} method.

@author Jacob Lockard @since 0.1.0

Attributes

registrations[R]

The hash of registrations contained in this {Registrar}. @return [Hash<(Symbol, Object)>] the hash of registrations.

Public Class Methods

new() click to toggle source

Creates a new instance of {Registrar}.

# File lib/edit_in_place/registrar.rb, line 13
def initialize
  @registrations = {}
end

Public Instance Methods

all() click to toggle source

Gets a hash of all registrations. Note that this hash is a deep clone of the actual, internal one and can be safely modified. @return [Hash<(Symbol, Object)>] the hash of registered names and objects.

# File lib/edit_in_place/registrar.rb, line 61
def all
  # Be sure to not duplicate classes.
  registrations.transform_values { |r| r.instance_of?(Class) ? r : r.deep_dup }
end
dup() click to toggle source

Creates a deep copy of this {Registrar} that can be safely modified. @return [Registrar] a deep copy of this registrar.

# File lib/edit_in_place/registrar.rb, line 19
def dup
  r = self.class.new
  r.register_all(all)
  r
end
find(name) click to toggle source

Attempts to find an object associated with the given symbol name. @param name [Symbol] the symbol name to search for. @return [Object] if found. @return [nil] if no object was associated with the given name.

# File lib/edit_in_place/registrar.rb, line 54
def find(name)
  registrations[name]
end
register(name, object) click to toggle source

Registers the given object with the given symbol name. @param name [Symbol] the symbol name with which to associate the object. @param object the object to register. @return [void] @see register_all

# File lib/edit_in_place/registrar.rb, line 30
def register(name, object)
  validate_registration!(name, object)
  registrations[name] = object
end
register_all(objects) click to toggle source

Registers all the symbol names and objects in the given hash. All elements of the hash are passed through {#register}. @param objects [Hash<(Symbol, Object)>] the hash of names and objects to register. @return [void] @see register

# File lib/edit_in_place/registrar.rb, line 40
def register_all(objects)
  # The identical loops are necessary to prevent anyything from being registered if even one
  # fails the validation.

  # rubocop:disable Style/CombinableLoops
  objects.each { |n, t| validate_registration!(n, t) }
  objects.each { |n, t| register(n, t) }
  # rubocop:enable Style/CombinableLoops
end

Protected Instance Methods

validate_registration!(name, _object) click to toggle source

Should ensure that the given registration is valid. By default a registration is valid if its name is not already taken and it is a symbol. Subclasses may override this method to add validation rules. Errors should be raised for any invalid registrations. @param name [Symbol] the name to validate. @param _object [Object] the object to validate. @return [void]

# File lib/edit_in_place/registrar.rb, line 74
    def validate_registration!(name, _object)
      if registrations.key?(name)
        raise DuplicateRegistrationError, <<~ERR
          The name '#{name}' has already been registered!
        ERR
      end

      unless name.is_a?(Symbol)
        raise InvalidRegistrationNameError, <<~ERR
          The name '#{name}' is not a valid symbol registration name!
        ERR
      end
    end