Completed
Push — master ( 356d2e...fc6b68 )
by Marcelo
47s
created

AttributesFactory.create()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
1
module Koine
2
  module Attributes
3
    class AttributesFactory
4
      def initialize(options = {})
5
        @adapters = {}
6
        @options = options
7
      end
8
9
      def create(target_object)
10
        Attributes.new(target_object, adapters: @adapters, options: @options)
11
      end
12
13
      def add_attribute(name, adapter, &block)
14
        adapter = coerce_adapter(adapter)
15
        yield(adapter) if block
16
        @adapters[name.to_sym] = adapter.freeze
17
      end
18
19
      def coerce_adapter(adapter)
20
        return adapter unless adapter.instance_of?(::Symbol)
21
        Object.const_get("Koine::Attributes::Adapter::#{adapter.to_s.capitalize}").new
22
      end
23
24
      def freeze
25
        super
26
        @adapters.freeze
27
        @options.freeze
28
      end
29
    end
30
  end
31
end
32