Module: AWS::Record::AttributeMacros

Included in:
Base
Defined in:
lib/aws/record/attribute_macros.rb

Instance Method Summary collapse

Instance Method Details

#boolean_attr(name, options = {}) ⇒ Object

Adds a boolean attribute to this class.

Examples:


class Book < AWS::Record::Base
  boolean_attr :read
end

b = Book.new
b.read? # => false
b.read = true
b.read? # => true

listing = Listing.new(:score => '123.456'
listing.score # => 123.456

Parameters:

  • name (Symbol)

    The name of the attribute.



181
182
183
184
185
186
187
188
189
190
# File 'lib/aws/record/attribute_macros.rb', line 181

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

#datetime_attr(name, options = {}) ⇒ Object

Adds a datetime attribute to this class.

If you add a datetime_attr for :created_at and/or :updated_at those will be automanaged.

Examples:

A standard datetime attribute


class Recipe < AWS::Record::Base
  datetime_attr :invented
end

recipe = Recipe.new(:invented => Time.now)
recipe.invented #=> <DateTime ...>

Parameters:

  • name (Symbol)

    The name of the attribute.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :precision (Integer)

    When set, the integer will be serialized with the correct number of digits to SimpleDB, left padded by zeros to allow sorting.

  • :set (Boolean) — default: false

    When true this attribute can have multiple values.



213
214
215
# File 'lib/aws/record/attribute_macros.rb', line 213

def datetime_attr name, options = {}
  add_attribute(DateTimeAttribute.new(name, options))
end

#float_attr(name, options = {}) ⇒ Object

Adds a float attribute to this class.

class Listing < AWS::Record::Base
  float_attr :score
end

listing = Listing.new(:score => '123.456')
listing.score # => 123.456

Parameters:

  • name (Symbol)

    The name of the attribute.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :set (Boolean) — default: false

    When true this attribute can have multiple values.



117
118
119
# File 'lib/aws/record/attribute_macros.rb', line 117

def float_attr name, options = {}
  add_attribute(FloatAttribute.new(name, options))
end

#integer_attr(name, options = {}) ⇒ Object

Adds an integer attribute to this class.

class Recipe < AWS::Record::Base
  integer_attr :servings
end

recipe = Recipe.new(:servings => '10')
recipe.servings #=> 10

Parameters:

  • name (Symbol)

    The name of the attribute.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :set (Boolean) — default: false

    When true this attribute can have multiple values.



67
68
69
# File 'lib/aws/record/attribute_macros.rb', line 67

def integer_attr name, options = {}
  add_attribute(IntegerAttribute.new(name, options))
end

#sortable_float_attr(name, options = {}) ⇒ Object

Note:

If you change the :range after some values have been persisted you must also manually migrate all of the old values to have the correct padding & offset or they will be interpreted differently.

Adds sortable float attribute to this class.

Persisted values are stored (and sorted) as strings. This makes it more difficult to sort numbers because they don’t sort lexicographically unless they have been offset to be positive and then zero padded.

Postive Floats

To store floats in a sort-friendly manor:

sortable_float_attr :score, :range => (0..10)

This will cause values like 5.5 to persist as a string like ‘05.5’ so that they can be sorted lexicographically.

Negative Floats

If you need to store negative sortable floats, increase your :range to include a negative value.

sortable_float_attr :position, :range => (-10..10)

AWS::Record will add 10 to all values and zero pad them (e.g. -10.0 will be represented as ‘00.0’ and 10 will be represented as ‘20.0’). This will allow the values to be compared lexicographically.

Parameters:

  • name (Symbol)

    The name of the attribute.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :range (Range)

    The range of numbers this attribute should represent. The min and max values of this range will determine how many digits of precision are required and how much of an offset is required to make the numbers sort lexicographically.

  • :set (Boolean) — default: false

    When true this attribute can have multiple values.



160
161
162
# File 'lib/aws/record/attribute_macros.rb', line 160

def sortable_float_attr name, options = {}
  add_attribute(SortableFloatAttribute.new(name, options))
end

#sortable_integer_attr(name, options = {}) ⇒ Object

Adds a sortable integer attribute to this class.

class Person < AWS::Record::Base
  sortable_integer_attr :age, :range => 0..150
end

person = Person.new(:age => 10)
person.age #=> 10

Validations

It is recomended to apply a validates_numericality_of with minimum and maximum value constraints. If a value is assigned to a sortable integer that falls outside of the +:range: it will raise a runtime error when the record is saved.

Difference Between Sortable an Regular Integer Attributes

Because SimpleDB does not support numeric types, all values must be converted to strings. This complicates sorting by numeric values. To accomplish sorting numeric attributes the values must be zero padded and have an offset applied to eliminate negative values.

Parameters:

  • name (Symbol)

    The name of the attribute.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :range (Range)

    A numeric range the represents the minimum and maximum values this attribute should accept.

  • :set (Boolean) — default: false

    When true this attribute can have multiple values.



100
101
102
# File 'lib/aws/record/attribute_macros.rb', line 100

def sortable_integer_attr name, options = {}
  add_attribute(SortableIntegerAttribute.new(name, options))
end

#string_attr(name, options = {}) ⇒ Object

Adds a string attribute to this class.

Examples:

A standard string attribute


class Recipe < AWS::Record::Base
  string_attr :name
end

recipe = Recipe.new(:name => "Buttermilk Pancakes")
recipe.name #=> 'Buttermilk Pancakes'

A string attribute with :set set to true


class Recipe < AWS::Record::Base
  string_attr :tags, :set => true
end

recipe = Recipe.new(:tags => %w(popular dessert))
recipe.tags #=> #<Set: {"popular", "desert"}> 

Parameters:

  • name (Symbol)

    The name of the attribute.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :set (Boolean) — default: false

    When true this attribute can have multiple values.



50
51
52
# File 'lib/aws/record/attribute_macros.rb', line 50

def string_attr name, options = {}
  add_attribute(StringAttribute.new(name, options))
end

#timestampsObject

A convenience method for adding the standard two datetime attributes :created_at and :updated_at.

Examples:


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

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


231
232
233
234
235
# File 'lib/aws/record/attribute_macros.rb', line 231

def timestamps
  c = datetime_attr :created_at
  u = datetime_attr :updated_at
  [c, u]
end