module AWS::Record::AttributeMacros

def add_attribute attribute

def add_attribute attribute
  attr_name = attribute.name
  attributes[attr_name] = attribute
  # setter
  define_method("#{attr_name}=") do |value|
    self[attr_name] = value
  end
  # getter
  define_method(attr_name) do
    self[attr_name]
  end
  # before type-cast getter
  define_method("#{attr_name}_before_type_cast") do
    @_data[attr_name]
  end
  ## dirty tracking methods
  define_method("#{attr_name}_changed?") do
    attribute_changed?(attr_name)
  end
  define_method("#{attr_name}_change") do
    attribute_change(attr_name)
  end
  define_method("#{attr_name}_was") do
    attribute_was(attr_name)
  end
  define_method("#{attr_name}_will_change!") do
    attribute_will_change!(attr_name)
  end
  define_method("reset_#{attr_name}!") do
    reset_attribute!(attr_name)
  end
  attribute
end

def boolean_attr name, options = {}

Parameters:
  • name (Symbol) -- The name of the attribute.
def boolean_attr name, options = {}
  attr = add_attribute(BooleanAttribute.new(name, options))
  # add the boolean question mark method
  define_method("#{attr.name}?") do
    !!__send__(attr.name)
  end
end

def datetime_attr name, options = {}

Options Hash: (**options)
  • :set (Boolean) -- When true this attribute
  • :precision (Integer) -- When set, the integer will be

Parameters:
  • options (Hash) --
  • name (Symbol) -- The name of the attribute.

Other tags:
    Example: A standard datetime attribute -
def datetime_attr name, options = {}
  add_attribute(DateTimeAttribute.new(name, options))
end

def float_attr name, options = {}

Options Hash: (**options)
  • :set (Boolean) -- When true this attribute

Parameters:
  • options (Hash) --
  • name (Symbol) -- The name of the attribute.
def float_attr name, options = {}
  add_attribute(FloatAttribute.new(name, options))
end

def integer_attr name, options = {}

Options Hash: (**options)
  • :set (Boolean) -- When true this attribute

Parameters:
  • options (Hash) --
  • name (Symbol) -- The name of the attribute.
def integer_attr name, options = {}
  add_attribute(IntegerAttribute.new(name, options))
end

def sortable_float_attr name, options = {}

Options Hash: (**options)
  • :set (Boolean) -- When true this attribute
  • :range (Range) -- The range of numbers this attribute

Parameters:
  • options (Hash) --
  • name (Symbol) -- The name of the attribute.

Other tags:
    Note: - If you change the +:range+ after some values have been persisted
def sortable_float_attr name, options = {}
  add_attribute(SortableFloatAttribute.new(name, options))
end

def sortable_integer_attr name, options = {}

Options Hash: (**options)
  • :set (Boolean) -- When true this attribute
  • :range (Range) -- A numeric range the represents the

Parameters:
  • options (Hash) --
  • name (Symbol) -- The name of the attribute.
def sortable_integer_attr name, options = {}
  add_attribute(SortableIntegerAttribute.new(name, options))
end

def string_attr name, options = {}

Options Hash: (**options)
  • :set (Boolean) -- When true this attribute

Parameters:
  • options (Hash) --
  • name (Symbol) -- The name of the attribute.

Other tags:
    Example: A string attribute with +:set+ set to true -
    Example: A standard string attribute -
def string_attr name, options = {}
  add_attribute(StringAttribute.new(name, options))
end

def timestamps


recipe.updated_at #=>
recipe.created_at #=>
recipe.save
recipe = Recipe.new

end
timestamps
class Recipe < AWS::Record::Base

@example

+:created_at+ and +:updated_at+.
A convenience method for adding the standard two datetime attributes
def timestamps
  c = datetime_attr :created_at
  u = datetime_attr :updated_at
  [c, u]
end