| Total Complexity | 9 |
| Total Lines | 42 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | module Koine |
||
| 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 |
||
| 47 |