Class: Koine::Attributes::AttributesFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/koine/attributes/attributes_factory.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ AttributesFactory

Returns a new instance of AttributesFactory.



4
5
6
7
# File 'lib/koine/attributes/attributes_factory.rb', line 4

def initialize(options = {})
  @adapters = {}
  @options = options
end

Instance Method Details

#add_attribute(name, adapter) {|adapter| ... } ⇒ Object

Yields:

  • (adapter)


13
14
15
16
17
# File 'lib/koine/attributes/attributes_factory.rb', line 13

def add_attribute(name, adapter, &block)
  adapter = coerce_adapter(adapter)
  yield(adapter) if block
  @adapters[name.to_sym] = adapter.freeze
end

#coerce_adapter(adapter) ⇒ Object



19
20
21
22
# File 'lib/koine/attributes/attributes_factory.rb', line 19

def coerce_adapter(adapter)
  return adapter unless adapter.instance_of?(::Symbol)
  Object.const_get("Koine::Attributes::Adapter::#{adapter.to_s.capitalize}").new
end

#create(target_object) ⇒ Object



9
10
11
# File 'lib/koine/attributes/attributes_factory.rb', line 9

def create(target_object)
  Attributes.new(target_object, adapters: @adapters, options: @options)
end

#freezeObject



24
25
26
27
28
# File 'lib/koine/attributes/attributes_factory.rb', line 24

def freeze
  super
  @adapters.freeze
  @options.freeze
end