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

AttributesFactory.initialize()   A

Complexity

Conditions 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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