Module: AWS::Record::AttributeMacros
- Included in:
- Base
- Defined in:
- lib/aws/record/attribute_macros.rb
Instance Method Summary collapse
-
#boolean_attr(name, options = {}) ⇒ Object
Adds a boolean attribute to this class.
-
#date_attr(name, options = {}) ⇒ Object
Adds a date attribute to this class.
-
#datetime_attr(name, options = {}) ⇒ Object
Adds a datetime attribute to this class.
-
#float_attr(name, options = {}) ⇒ Object
Adds a float attribute to this class.
-
#integer_attr(name, options = {}) ⇒ Object
Adds an integer attribute to this class.
-
#sortable_float_attr(name, options = {}) ⇒ Object
Adds sortable float attribute to this class.
-
#sortable_integer_attr(name, options = {}) ⇒ Object
Adds a sortable integer attribute to this class.
-
#string_attr(name, options = {}) ⇒ Object
Adds a string attribute to this class.
-
#timestamps ⇒ Object
A convenience method for adding the standard two datetime attributes
:created_at
and:updated_at
.
Instance Method Details
#boolean_attr(name, options = {}) ⇒ Object
Adds a boolean attribute to this class.
182 183 184 185 186 187 188 189 190 191 |
# File 'lib/aws/record/attribute_macros.rb', line 182 def boolean_attr name, = {} attr = add_attribute(BooleanAttribute.new(name, )) # add the boolean question mark method define_method("#{attr.name}?") do !!__send__(attr.name) end end |
#date_attr(name, options = {}) ⇒ Object
Adds a date attribute to this class.
237 238 239 |
# File 'lib/aws/record/attribute_macros.rb', line 237 def date_attr name, = {} add_attribute(DateAttribute.new(name, )) 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.
214 215 216 |
# File 'lib/aws/record/attribute_macros.rb', line 214 def datetime_attr name, = {} add_attribute(DateTimeAttribute.new(name, )) end |
#float_attr(name, options = {}) ⇒ Object
118 119 120 |
# File 'lib/aws/record/attribute_macros.rb', line 118 def float_attr name, = {} add_attribute(FloatAttribute.new(name, )) end |
#integer_attr(name, options = {}) ⇒ Object
68 69 70 |
# File 'lib/aws/record/attribute_macros.rb', line 68 def integer_attr name, = {} add_attribute(IntegerAttribute.new(name, )) end |
#sortable_float_attr(name, options = {}) ⇒ Object
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.
161 162 163 |
# File 'lib/aws/record/attribute_macros.rb', line 161 def sortable_float_attr name, = {} add_attribute(SortableFloatAttribute.new(name, )) 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.
101 102 103 |
# File 'lib/aws/record/attribute_macros.rb', line 101 def sortable_integer_attr name, = {} add_attribute(SortableIntegerAttribute.new(name, )) end |
#string_attr(name, options = {}) ⇒ Object
Adds a string attribute to this class.
51 52 53 |
# File 'lib/aws/record/attribute_macros.rb', line 51 def string_attr name, = {} add_attribute(StringAttribute.new(name, )) end |
#timestamps ⇒ Object
A convenience method for adding the standard two datetime attributes :created_at
and :updated_at
.
255 256 257 258 259 |
# File 'lib/aws/record/attribute_macros.rb', line 255 def c = datetime_attr :created_at u = datetime_attr :updated_at [c, u] end |