Class: HoneyFormat::Registry
- Inherits:
-
Object
- Object
- HoneyFormat::Registry
- Defined in:
- lib/honey_format/registry.rb
Overview
Registry that holds value callers
Instance Method Summary collapse
-
#[](type) ⇒ Object
Returns the given type or raises error if type doesn’t exist.
-
#[]=(type, caller) ⇒ Object
Register a caller.
-
#call(value, type) ⇒ Object
Call value type.
-
#initialize(default = {}) ⇒ Registry
constructor
Instantiate a caller registry.
-
#register(type, caller) ⇒ Registry
Register a caller.
-
#reset! ⇒ Registry
Resets the caller registry to its default configuration.
-
#type?(type) ⇒ true, false
Returns true if the type exists, false otherwise.
-
#types ⇒ Array<Symbol>
Returns list of registered types.
-
#unregister(type) ⇒ Registry
Unregister a caller.
Constructor Details
#initialize(default = {}) ⇒ Registry
Instantiate a caller registry
8 9 10 11 12 |
# File 'lib/honey_format/registry.rb', line 8 def initialize(default = {}) @callers = nil @default = default.dup reset! end |
Instance Method Details
#[](type) ⇒ Object
Returns the given type or raises error if type doesn’t exist
68 69 70 |
# File 'lib/honey_format/registry.rb', line 68 def [](type) @callers.fetch(to_key(type)) { unknown_type_error!(type) } end |
#[]=(type, caller) ⇒ Object
Register a caller
54 55 56 57 58 59 60 61 62 |
# File 'lib/honey_format/registry.rb', line 54 def []=(type, caller) type = to_key(type) if type?(type) raise(Errors::TypeExistsError, "type '#{type}' already exists") end @callers[type] = caller end |
#call(value, type) ⇒ Object
Call value type
43 44 45 46 47 |
# File 'lib/honey_format/registry.rb', line 43 def call(value, type) return type.call(value) if type.respond_to?(:call) self[type].call(value) end |
#register(type, caller) ⇒ Registry
Register a caller
25 26 27 28 |
# File 'lib/honey_format/registry.rb', line 25 def register(type, caller) self[type] = caller self end |
#reset! ⇒ Registry
Resets the caller registry to its default configuration
83 84 85 86 |
# File 'lib/honey_format/registry.rb', line 83 def reset! @callers = @default.dup self end |
#type?(type) ⇒ true, false
Returns true if the type exists, false otherwise
75 76 77 78 79 |
# File 'lib/honey_format/registry.rb', line 75 def type?(type) return false unless keyable?(type) @callers.key?(to_key(type)) end |
#types ⇒ Array<Symbol>
Returns list of registered types
16 17 18 |
# File 'lib/honey_format/registry.rb', line 16 def types @callers.keys end |
#unregister(type) ⇒ Registry
Unregister a caller
34 35 36 37 38 |
# File 'lib/honey_format/registry.rb', line 34 def unregister(type) unknown_type_error!(type) unless type?(type) @callers.delete(to_key(type)) self end |