Module: ActsAsHashable::Factory

Extended by:
Forwardable
Defined in:
lib/acts_as_hashable/factory.rb

Overview

This class serves as a singleton that can make mapped acts_as_hashable components. It is important to note that these components must be acts_as_hashable objects. In order to use you just have to subclass this class and implement a method called ‘registry’, such as:

def registry
  {
    'Table': Table,
    'Text': Text
  }
end

You can also use the ‘register’ DSL:

register 'some_class_name', SomeClassName
register 'some_class_name', '', SomeClassName

or:

register 'some_class_name', ->(_key) { SomeClassName }

Instance Method Summary collapse

Instance Method Details

#materialize_registryObject

:nodoc:



45
46
47
48
49
50
# File 'lib/acts_as_hashable/factory.rb', line 45

def materialize_registry # :nodoc:
  @registry.map do |k, v|
    value = v.is_a?(Proc) ? v.call(k) : v
    [k, value]
  end.to_h
end

#register(*args) ⇒ Object

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
# File 'lib/acts_as_hashable/factory.rb', line 31

def register(*args)
  raise ArgumentError, "missing at least one key and value: #{args}" if args.length < 2

  value = args.last

  args[0..-2].each do |key|
    registry[key] = value
  end
end

#registryObject

:nodoc:



41
42
43
# File 'lib/acts_as_hashable/factory.rb', line 41

def registry # :nodoc:
  @registry ||= {}
end

#type_key(key) ⇒ Object

:nodoc:



52
53
54
# File 'lib/acts_as_hashable/factory.rb', line 52

def type_key(key) # :nodoc:
  @typed_with = key
end

#typed_withObject

:nodoc:



56
57
58
# File 'lib/acts_as_hashable/factory.rb', line 56

def typed_with # :nodoc:
  @typed_with || TypeFactory::DEFAULT_TYPE_KEY
end