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