Module: Dynamoid::Fields::ClassMethods
- Defined in:
- lib/dynamoid/fields.rb
Instance Method Summary collapse
-
#field(name, type = :string, options = {}) ⇒ Object
Specify a field for a document.
-
#range(name, type = :string, options = {}) ⇒ Object
Declare a table range key.
-
#remove_field(field) ⇒ Object
Remove a field declaration.
-
#table(options) ⇒ Object
Set table level properties.
- #timestamps_enabled? ⇒ Boolean
Instance Method Details
#field(name, type = :string, options = {}) ⇒ Object
Specify a field for a document.
class User
include Dynamoid::Document
field :last_name
field :age, :integer
field :last_sign_in, :datetime
end
Its type determines how it is coerced when read in and out of the datastore. You can specify string, integer, number, set, array, map, datetime, date, serialized, raw, boolean and binary or specify a class that defines a serialization strategy.
By default field type is string.
Set can store elements of the same type only (it’s a limitation of DynamoDB itself). If a set should store elements only some particular type of option should be specified:
field :hobbies, :set, of: :string
Only string, integer, number, date, datetime and serialized element types are supported.
Element type can have own options - they should be specified in the form of Hash:
field :hobbies, :set, of: { serialized: { serializer: JSON } }
Array can contain element of different types but if supports the same of option to convert all the provided elements to the declared type.
field :rates, :array, of: :number
By default date and datetime fields are stored as integer values. The format can be changed to string with option store_as_string:
field :published_on, :datetime, store_as_string: true
Boolean field by default is stored as a string t or f. But DynamoDB supports boolean type natively. In order to switch to the native boolean type an option store_as_native_boolean should be specified:
field :active, :boolean, store_as_native_boolean: true
If you specify the serialized type a value will be serialized to string in Yaml format by default. Custom way to serialize value to string can be specified with serializer option. Custom serializer should have dump and load methods.
If you specify a class for field type, Dynamoid will serialize using dynamoid_dump method and load using dynamoid_load method.
Default field type is string.
A field can have a default value. It’s assigned at initializing a model if no value is specified:
field :age, :integer, default: 1
If a defautl value should be recalculated every time it can be specified as a callable object (it should implement a call method e.g. Proc object):
field :date_of_birth, :date, default: -> { Date.today }
For every field Dynamoid creates several methods:
-
getter
-
setter
-
predicate <name>? to check whether a value set
-
<name>_before_type_cast? to get an original field value before it was type casted
It works in the following way:
class User
include Dynamoid::Document
field :age, :integer
end
user = User.new
user.age # => nil
user.age? # => false
user.age = 20
user.age? # => true
user.age = '21'
user.age # => 21 - integer
user.age_before_type_cast # => '21' - string
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/dynamoid/fields.rb', line 134 def field(name, type = :string, = {}) named = name.to_s if type == :float Dynamoid.logger.warn("Field type :float, which you declared for '#{name}', is deprecated in favor of :number.") type = :number end self.attributes = attributes.merge(name => { type: type }.merge()) # should be called before `define_attribute_methods` method because it defines a getter itself warn_about_method_overriding(name, name) warn_about_method_overriding("#{named}=", name) warn_about_method_overriding("#{named}?", name) warn_about_method_overriding("#{named}_before_type_cast?", name) define_attribute_method(name) # Dirty API generated_methods.module_eval do define_method(named) { read_attribute(named) } define_method("#{named}?") do value = read_attribute(named) case value when true then true when false, nil then false else !value.nil? end end define_method("#{named}=") { |value| write_attribute(named, value) } define_method("#{named}_before_type_cast") { read_attribute_before_type_cast(named) } end end |
#range(name, type = :string, options = {}) ⇒ Object
Declare a table range key.
class User
include Dynamoid::Document
range :last_name
end
By default a range key is a string. In order to use any other type it should be specified as a second argument:
range :age, :integer
Type options can be specified as well:
range :date_of_birth, :date, store_as_string: true
186 187 188 189 |
# File 'lib/dynamoid/fields.rb', line 186 def range(name, type = :string, = {}) field(name, type, ) self.range_key = name end |
#remove_field(field) ⇒ Object
Remove a field declaration
Removes a field from the list of fields and removes all te generated for a field methods.
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/dynamoid/fields.rb', line 255 def remove_field(field) field = field.to_sym attributes.delete(field) || raise('No such field') # Dirty API undefine_attribute_methods define_attribute_methods attributes.keys generated_methods.module_eval do remove_method field remove_method :"#{field}=" remove_method :"#{field}?" remove_method :"#{field}_before_type_cast" end end |
#table(options) ⇒ Object
Set table level properties.
There are some sensible defaults:
-
table name is based on a model class e.g.
usersforUserclass -
hash key name -
idby default -
hash key type -
stringby default -
generating timestamp fields
created_atandupdated_at -
billing mode and read/write capacity units
The table method can be used to override the defaults:
class User
include Dynamoid::Document
table name: :customers, key: :uuid
end
The hash key field is declared by default and a type is a string. If another type is needed the field should be declared explicitly:
class User
include Dynamoid::Document
field :id, :integer
end
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/dynamoid/fields.rb', line 229 def table() # a default 'id' column is created when Dynamoid::Document is included unless attributes.key? hash_key remove_field :id field(hash_key) end if [:timestamps] && !Dynamoid::Config. # Timestamp fields weren't declared in `included` hook because they # are disabled globaly field :created_at, :datetime field :updated_at, :datetime elsif [:timestamps] == false && Dynamoid::Config. # Timestamp fields were declared in `included` hook but they are # disabled for a table remove_field :created_at remove_field :updated_at end end |
#timestamps_enabled? ⇒ Boolean
272 273 274 |
# File 'lib/dynamoid/fields.rb', line 272 def [:timestamps] || ([:timestamps].nil? && Dynamoid::Config.) end |