|
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. |
@@ -43,15 +45,15 @@ module ClassMethods |
43 | 45 | # end |
44 | 46 | # |
45 | 47 | # Its type determines how it is coerced when read in and out of the |
46 | | - # datastore. You can specify +string+, +integer+, +number+, +set+, +array+, |
| 48 | + # data store. You can specify +string+, +integer+, +number+, +set+, +array+, |
47 | 49 | # +map+, +datetime+, +date+, +serialized+, +raw+, +boolean+ and +binary+ |
48 | 50 | # or specify a class that defines a serialization strategy. |
49 | 51 | # |
50 | 52 | # By default field type is +string+. |
51 | 53 | # |
52 | 54 | # Set can store elements of the same type only (it's a limitation of |
53 | | - # DynamoDB itself). If a set should store elements only some particular |
54 | | - # type +of+ option should be specified: |
| 55 | + # DynamoDB itself). If a set should store elements only of some particular |
| 56 | + # type then +of+ option should be specified: |
55 | 57 | # |
56 | 58 | # field :hobbies, :set, of: :string |
57 | 59 | # |
@@ -126,41 +128,31 @@ module ClassMethods |
126 | 128 | # user.age # => 21 - integer |
127 | 129 | # user.age_before_type_cast # => '21' - string |
128 | 130 | # |
| 131 | + # There is also an option +alias+ which allows to use another name for a |
| 132 | + # field: |
| 133 | + # |
| 134 | + # class User |
| 135 | + # include Dynamoid::Document |
| 136 | + # |
| 137 | + # field :firstName, :string, alias: :first_name |
| 138 | + # end |
| 139 | + # |
| 140 | + # user = User.new(firstName: 'Michael') |
| 141 | + # user.firstName # Michael |
| 142 | + # user.first_name # Michael |
| 143 | + # |
129 | 144 | # @param name [Symbol] name of the field |
130 | 145 | # @param type [Symbol] type of the field (optional) |
131 | 146 | # @param options [Hash] any additional options for the field type (optional) |
132 | 147 | # |
133 | 148 | # @since 0.2.0 |
134 | 149 | def field(name, type = :string, options = {}) |
135 | | - named = name.to_s |
136 | 150 | if type == :float |
137 | 151 | Dynamoid.logger.warn("Field type :float, which you declared for '#{name}', is deprecated in favor of :number.") |
138 | 152 | type = :number |
139 | 153 | 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 | 154 |
|
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 |
| 155 | + Dynamoid::Fields::Declare.new(self, name, type, options).call |
164 | 156 | end |
165 | 157 |
|
166 | 158 | # Declare a table range key. |
@@ -273,21 +265,14 @@ def timestamps_enabled? |
273 | 265 | options[:timestamps] || (options[:timestamps].nil? && Dynamoid::Config.timestamps) |
274 | 266 | end |
275 | 267 |
|
276 | | - private |
277 | | - |
| 268 | + # @private |
278 | 269 | def generated_methods |
279 | 270 | @generated_methods ||= begin |
280 | 271 | Module.new.tap do |mod| |
281 | 272 | include(mod) |
282 | 273 | end |
283 | 274 | end |
284 | 275 | end |
285 | | - |
286 | | - def warn_about_method_overriding(method_name, field_name) |
287 | | - if instance_methods.include?(method_name.to_sym) |
288 | | - Dynamoid.logger.warn("Method #{method_name} generated for the field #{field_name} overrides already existing method") |
289 | | - end |
290 | | - end |
291 | 276 | end |
292 | 277 |
|
293 | 278 | # You can access the attributes of an object directly on its attributes method, which is by default an empty hash. |
|
0 commit comments