class HoneyFormat::Registry
Registry
that holds value callers
Public Class Methods
Instantiate a caller registry @param [Hash] default hash of defaults
# File lib/honey_format/registry.rb, line 8 def initialize(default = {}) @callers = nil @default = default.dup reset! end
Public Instance Methods
Returns the given type or raises error if type doesn’t exist @param [Symbol, String] type the name of the type @return [Object] returns the caller @raise [UnknownTypeError] if type does not exist
# File lib/honey_format/registry.rb, line 68 def [](type) @callers.fetch(to_key(type)) { unknown_type_error!(type) } end
Register a caller @param [Symbol, String] type the name of the type @param [#call] caller that responds to call
@return [Object] returns the caller @raise [TypeExistsError] if type is already registered
# 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 @param [Symbol, String, call
] type the name of the type @param [Object] value to be converted
# 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 a caller @param [Symbol, String] type the name of the type @param [#call] caller that responds to call
@return [Registry] returns self @raise [TypeExistsError] if type is already registered
# File lib/honey_format/registry.rb, line 25 def register(type, caller) self[type] = caller self end
Resets the caller registry to its default configuration @return [Registry] returns the caller registry
# File lib/honey_format/registry.rb, line 83 def reset! @callers = @default.dup self end
Returns true if the type exists, false otherwise @param [Symbol, String] type the name of the type @return [true, false] true if type exists, false otherwise
# File lib/honey_format/registry.rb, line 75 def type?(type) return false unless keyable?(type) @callers.key?(to_key(type)) end
Returns list of registered types @return [Array<Symbol>] list of registered types
# File lib/honey_format/registry.rb, line 16 def types @callers.keys end
Unregister a caller @param [Symbol, String] type the name of the type @return [Registry] returns self @raise [UnknownTypeError] if type is already registered
# 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
Private Instance Methods
# File lib/honey_format/registry.rb, line 90 def keyable?(key) key.respond_to?(:to_sym) end
# File lib/honey_format/registry.rb, line 94 def to_key(key) key.to_sym end
# File lib/honey_format/registry.rb, line 98 def unknown_type_error!(type) raise(Errors::UnknownTypeError, "unknown type '#{type}'") end