|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
| 3 | +require 'dynamoid/fields/declare' |
| 4 | + |
3 | 5 | module Dynamoid |
4 | 6 | # All fields on a Dynamoid::Document must be explicitly defined -- if you have fields in the database that are not |
5 | 7 | # specified with field, then they will be ignored. |
@@ -132,46 +134,12 @@ module ClassMethods |
132 | 134 | # |
133 | 135 | # @since 0.2.0 |
134 | 136 | def field(name, type = :string, options = {}) |
135 | | - named = name.to_s |
136 | 137 | if type == :float |
137 | 138 | Dynamoid.logger.warn("Field type :float, which you declared for '#{name}', is deprecated in favor of :number.") |
138 | 139 | type = :number |
139 | 140 | end |
140 | | - self.attributes = attributes.merge(name => { type: type }.merge(options)) |
141 | | - |
142 | | - # should be called before `define_attribute_methods` method because it defines a getter itself |
143 | | - warn_about_method_overriding(name, name) |
144 | | - warn_about_method_overriding("#{named}=", name) |
145 | | - warn_about_method_overriding("#{named}?", name) |
146 | | - warn_about_method_overriding("#{named}_before_type_cast?", name) |
147 | | - |
148 | | - define_attribute_method(name) # Dirty API |
149 | | - |
150 | | - generated_methods.module_eval do |
151 | | - define_method(named) { read_attribute(named) } |
152 | | - define_method("#{named}?") do |
153 | | - value = read_attribute(named) |
154 | | - case value |
155 | | - when true then true |
156 | | - when false, nil then false |
157 | | - else |
158 | | - !value.nil? |
159 | | - end |
160 | | - end |
161 | | - define_method("#{named}=") { |value| write_attribute(named, value) } |
162 | | - define_method("#{named}_before_type_cast") { read_attribute_before_type_cast(named) } |
163 | | - end |
164 | | - |
165 | | - if options[:alias] |
166 | | - alias_name = options[:alias].to_s |
167 | 141 |
|
168 | | - generated_methods.module_eval do |
169 | | - alias_method alias_name, named |
170 | | - alias_method "#{alias_name}=", "#{named}=" |
171 | | - alias_method "#{alias_name}?", "#{named}?" |
172 | | - alias_method "#{alias_name}_before_type_cast", "#{named}_before_type_cast" |
173 | | - end |
174 | | - end |
| 142 | + Dynamoid::Fields::Declare.new(self, name, type, options).call |
175 | 143 | end |
176 | 144 |
|
177 | 145 | # Declare a table range key. |
@@ -284,21 +252,14 @@ def timestamps_enabled? |
284 | 252 | options[:timestamps] || (options[:timestamps].nil? && Dynamoid::Config.timestamps) |
285 | 253 | end |
286 | 254 |
|
287 | | - private |
288 | | - |
| 255 | + # @private |
289 | 256 | def generated_methods |
290 | 257 | @generated_methods ||= begin |
291 | 258 | Module.new.tap do |mod| |
292 | 259 | include(mod) |
293 | 260 | end |
294 | 261 | end |
295 | 262 | end |
296 | | - |
297 | | - def warn_about_method_overriding(method_name, field_name) |
298 | | - if instance_methods.include?(method_name.to_sym) |
299 | | - Dynamoid.logger.warn("Method #{method_name} generated for the field #{field_name} overrides already existing method") |
300 | | - end |
301 | | - end |
302 | 263 | end |
303 | 264 |
|
304 | 265 | # You can access the attributes of an object directly on its attributes method, which is by default an empty hash. |
|
0 commit comments