Module: DynaModel::Attributes::ClassMethods

Defined in:
lib/dyna_model/attributes.rb

Instance Method Summary collapse

Instance Method Details

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

Note:

This should not be used for large objects.

Adds a DynamoDB binary attribute to this class. A binary attribute acts the same as a string attribute, except

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.



157
158
# File 'lib/dyna_model/attributes.rb', line 157

def binary_attr name, options = {}
end

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

Adds a boolean attribute to this class.

Examples:


class Book < AWS::Record::HashModel
  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.



86
87
88
89
90
91
92
93
94
95
# File 'lib/dyna_model/attributes.rb', line 86

def boolean_attr name, options = {}

  attr = add_attribute(AWS::Record::Attributes::BooleanAttr.new(name, options))

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

Examples:

A standard date attribute


class Person < AWS::Record::HashModel
  date_attr :birthdate
end

baby = Person.new
baby.birthdate = Time.now
baby.birthdate #=> <Date: ....>

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



141
142
143
# File 'lib/dyna_model/attributes.rb', line 141

def date_attr name, options = {}
  add_attribute(AWS::Record::Attributes::DateAttr.new(name, options))
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::HashModel
  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):

  • :set (Boolean) — default: false

    When true this attribute can have multiple date times.



118
119
120
# File 'lib/dyna_model/attributes.rb', line 118

def datetime_attr name, options = {}
  add_attribute(AWS::Record::Attributes::DateTimeAttr.new(name, options))
end

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

Adds a float attribute to this class.

class Listing < AWS::Record::HashModel
  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.



65
66
67
# File 'lib/dyna_model/attributes.rb', line 65

def float_attr name, options = {}
  add_attribute(AWS::Record::Attributes::FloatAttr.new(name, options))
end

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

Adds an integer attribute to this class.

class Recipe < AWS::Record::HashModel
  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.



48
49
50
# File 'lib/dyna_model/attributes.rb', line 48

def integer_attr name, options = {}
  add_attribute(AWS::Record::Attributes::IntegerAttr.new(name, options))
end

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



160
161
162
# File 'lib/dyna_model/attributes.rb', line 160

def serialized_attr name, options = {}
  add_attribute(AWS::Record::Attributes::SerializedAttr.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::HashModel
  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::HashModel
  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.



31
32
33
# File 'lib/dyna_model/attributes.rb', line 31

def string_attr name, options = {}
  add_attribute(AWS::Record::Attributes::StringAttr.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::HashModel
  timestamps
end

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


178
179
180
181
182
# File 'lib/dyna_model/attributes.rb', line 178

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