|
| 1 | +module Rectify |
| 2 | + module Attributable |
| 3 | + def self.included(base) |
| 4 | + base.extend(ClassMethods) |
| 5 | + end |
| 6 | + |
| 7 | + module ClassMethods |
| 8 | + def attribute(name, type = nil, default: nil) |
| 9 | + name = name.to_sym |
| 10 | + |
| 11 | + rectify_attributes[name] = AttributeDefinition.new(name, type, default) |
| 12 | + |
| 13 | + attr_writer name |
| 14 | + |
| 15 | + define_method(name) do |
| 16 | + if instance_variable_defined?(:"@#{name}") |
| 17 | + instance_variable_get(:"@#{name}") |
| 18 | + else |
| 19 | + resolve_default(self.class.rectify_attributes[name].default) |
| 20 | + end |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + def rectify_attributes |
| 25 | + @rectify_attributes ||= if superclass.respond_to?(:rectify_attributes) |
| 26 | + superclass.rectify_attributes.dup |
| 27 | + else |
| 28 | + {} |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + def attribute_set |
| 33 | + rectify_attributes.values |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + def initialize(attrs = {}) |
| 38 | + assign_attributes(attrs) |
| 39 | + end |
| 40 | + |
| 41 | + def attributes |
| 42 | + self.class.rectify_attributes.to_h do |name, _defn| |
| 43 | + [name, public_send(name)] |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + private |
| 48 | + |
| 49 | + def assign_attributes(attrs) |
| 50 | + return if attrs.nil? |
| 51 | + |
| 52 | + normalized = attrs.transform_keys(&:to_sym) |
| 53 | + |
| 54 | + self.class.rectify_attributes.each do |name, defn| |
| 55 | + raw = normalized[name] |
| 56 | + next if raw.nil? && !normalized.key?(name) |
| 57 | + |
| 58 | + coerced = defn.coerce(raw) |
| 59 | + public_send(:"#{name}=", coerced) |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + def resolve_default(default) |
| 64 | + default.respond_to?(:call) ? default.call : default |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + class AttributeDefinition |
| 69 | + attr_reader :name, :type, :default, :member_type |
| 70 | + |
| 71 | + def initialize(name, type, default) # rubocop:disable Metrics/MethodLength |
| 72 | + @name = name.to_sym |
| 73 | + @default = default |
| 74 | + |
| 75 | + if type.is_a?(Array) |
| 76 | + @array = true |
| 77 | + @type = Array |
| 78 | + @member_type = type.first |
| 79 | + elsif type == Array |
| 80 | + @array = true |
| 81 | + @type = Array |
| 82 | + @member_type = nil |
| 83 | + else |
| 84 | + @array = false |
| 85 | + @type = type |
| 86 | + @member_type = nil |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + def coerce(value) |
| 91 | + return coerce_array(value) if array? |
| 92 | + |
| 93 | + coerce_value(value, type) |
| 94 | + end |
| 95 | + |
| 96 | + def primitive |
| 97 | + array? ? Array : type |
| 98 | + end |
| 99 | + |
| 100 | + def array? |
| 101 | + @array |
| 102 | + end |
| 103 | + |
| 104 | + def collection? |
| 105 | + array? |
| 106 | + end |
| 107 | + |
| 108 | + def declared_class |
| 109 | + primitive |
| 110 | + end |
| 111 | + |
| 112 | + private |
| 113 | + |
| 114 | + def coerce_array(value) |
| 115 | + return [] if value.nil? |
| 116 | + |
| 117 | + items = value.is_a?(Hash) ? value.values : Array(value) |
| 118 | + |
| 119 | + if @member_type.respond_to?(:new) |
| 120 | + items.map { |item| coerce_value(item, @member_type) } |
| 121 | + else |
| 122 | + items |
| 123 | + end |
| 124 | + end |
| 125 | + |
| 126 | + def coerce_value(value, target_type) # rubocop:disable Metrics |
| 127 | + return value if target_type.nil? || value.nil? |
| 128 | + return value if target_type != String && value.is_a?(target_type) |
| 129 | + |
| 130 | + case target_type.to_s |
| 131 | + when 'Integer' then coerce_integer(value) |
| 132 | + when 'Float' then coerce_float(value) |
| 133 | + when 'String' then value.to_s |
| 134 | + when 'Symbol' then value.to_sym |
| 135 | + else coerce_complex_type(value, target_type) |
| 136 | + end |
| 137 | + end |
| 138 | + |
| 139 | + def coerce_integer(value) |
| 140 | + return nil if value.is_a?(String) && value.strip.empty? |
| 141 | + |
| 142 | + Integer(value) |
| 143 | + rescue ArgumentError, TypeError |
| 144 | + nil |
| 145 | + end |
| 146 | + |
| 147 | + def coerce_float(value) |
| 148 | + return nil if value.is_a?(String) && value.strip.empty? |
| 149 | + |
| 150 | + Float(value) |
| 151 | + rescue ArgumentError, TypeError |
| 152 | + nil |
| 153 | + end |
| 154 | + |
| 155 | + def coerce_complex_type(value, target_type) |
| 156 | + if value.is_a?(Hash) && target_type.respond_to?(:new) |
| 157 | + target_type.new(value) |
| 158 | + else |
| 159 | + value |
| 160 | + end |
| 161 | + end |
| 162 | + end |
| 163 | +end |
0 commit comments