Completed
Pull Request — master (#5)
by Marcelo
34s
created

AttributesFactory.set_initializer_options()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
1
module Koine
2
  module Attributes
3
    class AttributesFactory
4
      def initialize(initializer: {}, attributes: [])
5
        @attributes = {}
6
        attributes.each do |attribute|
7
          arguments = Array(attribute)
8
          add_attribute(*arguments)
9
        end
10
11
        set_initializer_options
12
13
        if initializer && initializer.is_a?(Hash)
14
          set_initializer_options(initializer)
15
        end
16
      end
17
18
      def create(target_object)
19
        Attributes.new(
20
          target_object,
21
          attributes: @attributes,
22
          strict: @strict,
23
          freeze: @freeze
24
        )
25
      end
26
27
      def add_attribute(name, adapter, &block)
28
        adapter = coerce_adapter(adapter)
29
        block.call(adapter) if block
30
        @attributes[name.to_sym] = adapter
31
      end
32
33
      def coerce_adapter(adapter)
34
        return adapter unless adapter.instance_of?(::Symbol)
35
        Object.const_get("Koine::Attributes::Adapter::#{adapter.to_s.capitalize}").new
36
      end
37
38
      private
39
40
      def set_initializer_options(strict: true, freeze: false)
41
        @strict = strict
42
        @freeze = freeze
43
      end
44
    end
45
  end
46
end
47