module Dynamoid::Fields::ClassMethods

Public Instance Methods

field(name, type = :string, options = {}) click to toggle source

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, 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.

@param [Symbol] name the name of the field @param [Symbol] type the type of the field (refer to method description for details) @param [Hash] options any additional options for the field

@since 0.2.0

# File lib/dynamoid/fields.rb, line 45
def field(name, type = :string, options = {})
  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(options))

  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) }
end
range(name, type = :string) click to toggle source
# File lib/dynamoid/fields.rb, line 66
def range(name, type = :string)
  field(name, type)
  self.range_key = name
end
remove_field(field) click to toggle source
# File lib/dynamoid/fields.rb, line 79
def remove_field(field)
  field = field.to_sym
  attributes.delete(field) or raise "No such field"
  remove_method field
  remove_method :"#{field}="
  remove_method :"#{field}?"
end
table(options) click to toggle source
# File lib/dynamoid/fields.rb, line 71
def table(options)
  #a default 'id' column is created when Dynamoid::Document is included
  unless(attributes.has_key? hash_key)
    remove_field :id
    field(hash_key)
  end
end