Class: EditInPlace::Registrar

Inherits:
Object
  • Object
show all
Defined in:
lib/edit_in_place/registrar.rb

Overview

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

Direct Known Subclasses

FieldTypeRegistrar

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistrar

Creates a new instance of EditInPlace::Registrar.

Since:

  • 0.1.0


13
14
15
# File 'lib/edit_in_place/registrar.rb', line 13

def initialize
  @registrations = {}
end

Instance Attribute Details

#registrationsHash<(Symbol, Object)> (readonly, protected)

Returns A hash of registrations.

Returns:

  • (Hash<(Symbol, Object)>)

    A hash of registrations.

Since:

  • 0.1.0


68
69
70
# File 'lib/edit_in_place/registrar.rb', line 68

def registrations
  @registrations
end

Instance Method Details

#allHash<(Symbol, Object)>

Gets a hash of all registrations. Note that this hash is a deep clone of the actual, internal one and can be safely modified.

Returns:

  • (Hash<(Symbol, Object)>)

    the hash of registered names and objects.

Since:

  • 0.1.0


61
62
63
# File 'lib/edit_in_place/registrar.rb', line 61

def all
  registrations.deep_dup
end

#dupRegistrar

Creates a deep copy of this EditInPlace::Registrar that can be safely modified.

Returns:

  • (Registrar)

    a deep copy of this registrar.

Since:

  • 0.1.0


19
20
21
22
23
# File 'lib/edit_in_place/registrar.rb', line 19

def dup
  r = Registrar.new
  r.register_all(all)
  r
end

#find(name) ⇒ Object?

Attempts to find an object associated with the given symbol name.

Parameters:

  • name (Symbol)

    the symbol name to search for.

Returns:

  • (Object)

    if found.

  • (nil)

    if no object was associated with the given name.

Since:

  • 0.1.0


54
55
56
# File 'lib/edit_in_place/registrar.rb', line 54

def find(name)
  registrations[name]
end

#register(name, object) ⇒ void

This method returns an undefined value.

Registers the given object with the given symbol name.

Parameters:

  • name (Symbol)

    the symbol name with which to associate the object.

  • object

    the object to register.

See Also:

Since:

  • 0.1.0


30
31
32
33
# File 'lib/edit_in_place/registrar.rb', line 30

def register(name, object)
  validate_registration!(name, object)
  registrations[name] = object
end

#register_all(objects) ⇒ void

This method returns an undefined value.

Registers all the symbol names and objects in the given hash. All elements of the hash are passed through #register.

Parameters:

  • objects (Hash<(Symbol, Object)>)

    the hash of names and objects to register.

See Also:

Since:

  • 0.1.0


40
41
42
43
44
45
46
47
48
# 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

#validate_registration!(name, _object) ⇒ void (protected)

This method returns an undefined value.

Should ensure that the given registration is valid. By default a registration is valid if its name is not already taken and is a symbol. Subclasses may override this method to add validation rules. Errors should be raised for any invalid registrations.

Parameters:

  • name (Symbol)

    the name to validate.

  • _object (Object)

    the object to validate.

Since:

  • 0.1.0


76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/edit_in_place/registrar.rb', line 76

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