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

AttributesFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
dl 0
loc 37
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 8 3
A set_initializer_options() 0 4 1
A coerce_adapter() 0 4 2
A add_attribute() 0 5 2
A create() 0 8 1
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