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
- #remove_field(field) ⇒ Object
- #table(options) ⇒ Object
- #timestamps_enabled? ⇒ Boolean
Instance Method Details
#field(name, type = :string, options = {}) ⇒ Object
Specify a field for a document.
Its type determines how it is coerced when read in and out of the datastore. You can specify :integer, :number, :set, :array, :datetime, :date and :serialized, or specify a class that defines a serialization strategy.
If you specify a class for field type, Dynamoid will serialize using ‘dynamoid_dump` or `dump` methods, and load using `dynamoid_load` or `load` methods.
Default field type is :string.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/dynamoid/fields.rb', line 50 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()) 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
76 77 78 79 |
# File 'lib/dynamoid/fields.rb', line 76 def range(name, type = :string, = {}) field(name, type, ) self.range_key = name end |
#remove_field(field) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/dynamoid/fields.rb', line 101 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
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/dynamoid/fields.rb', line 81 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 |