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.

Since:

  • 0.1.0

Direct Known Subclasses

FieldTypeRegistrar

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 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