AWS services or capabilities described in AWS Documentation may vary by region/location. Click Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

Class: AWS::Record::HashModel

Inherits:
Object
  • Object
show all
Extended by:
AbstractBase
Defined in:
lib/aws/record/hash_model.rb,
lib/aws/record/hash_model/scope.rb,
lib/aws/record/hash_model/attributes.rb,
lib/aws/record/hash_model/finder_methods.rb

Defined Under Namespace

Classes: Scope

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from AbstractBase

extended

Class Method Details

+ (Scope) all(options = {})

Returns an enumerable scope object represents all records.

Book.all.each do |book|
  # ...
end

This method is equivalent to find(:all), and therefore you can also pass aditional options.

Book.all(:where => { :author' => 'me' }).each do |my_book|
  # ...
end

Returns:

  • (Scope)

    Returns an enumerable scope object.



109
110
111
# File 'lib/aws/record/hash_model/finder_methods.rb', line 109

def all options = {}
  new_scope.find(:all, options)
end

+ (Object) binary_attr(name, options = {})

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.



169
170
# File 'lib/aws/record/hash_model/attributes.rb', line 169

def binary_attr name, options = {}
end

+ (Object) boolean_attr(name, options = {})

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.



98
99
100
101
102
103
104
105
106
107
# File 'lib/aws/record/hash_model/attributes.rb', line 98

def boolean_attr name, options = {}

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

  # add the boolean question mark method
  define_method("#{attr.name}?") do
    !!__send__(attr.name)
  end

end

+ (Integer) count(options = {}) Also known as: size

Counts records Amazon DynamoDB.

class Product < AWS::Record::HashModel
end

# returns the count of records in the 'Product' table
Product.count

You can specify the table via #shard

# returns the count of records in the 'products-1' table
Product.shard('products-1').count

You can also specify the shard as an option to #count.

Product.count(:shard => 'table-name')

Chaining #count with #limit has no effect on the count.

Product.limit(10).count # same as Product.count, limit ignored

Parameters:

  • options (Hash) (defaults to: {})
  • [String] (Hash)

    a customizable set of options

Returns:

  • (Integer)

    The count of records in the table.



145
146
147
# File 'lib/aws/record/hash_model/finder_methods.rb', line 145

def count options = {}
  new_scope.count(options)
end

+ (DynamoDB::Table) create_table(read_capacity_units, write_capacity_units, options = {})

Creates the DynamoDB table that is configured for this class.

class Product < AWS::Record::HashModel
end

# create the table 'Product' with 10 read/write capacity units
Product.create_table 10, 10

If you shard you data across multiple tables, you can specify the shard name:

# create two tables, with the given names
Product.create_table 500, 10, :shard_name => 'products-1'
Product.create_table 500, 10, :shard_name => 'products-2'

If you share a single AWS account with multiple applications, you can provide a table prefix to group tables and to avoid name collisions:

AWS::Record.table_prefix = 'myapp-'

# creates the table 'myapp-Product'
Product.create_table 250, 50

# creates the table 'myapp-products-1'
Product.create_table 250, 50, :shard_name => 'products-1'

Parameters:

Options Hash (options):

  • :shard_name (String)

    Defaults to the class name. The shard name will be prefixed with AWS::Record.table_prefix, and that becomes the table name.

Returns:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/aws/record/hash_model.rb', line 78

def create_table read_capacity_units, write_capacity_units, options = {}

  table_name = dynamo_db_table_name(options[:shard_name])

  create_opts = {}
  create_opts[:hash_key] = { hash_key => :string }

  dynamo_db.tables.create(
    table_name,
    read_capacity_units,
    write_capacity_units,
    create_opts)

end

+ (Object) date_attr(name, options = {})

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.



153
154
155
# File 'lib/aws/record/hash_model/attributes.rb', line 153

def date_attr name, options = {}
  add_attribute(Attributes::DateAttr.new(name, options))
end

+ (Object) datetime_attr(name, options = {})

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.



130
131
132
# File 'lib/aws/record/hash_model/attributes.rb', line 130

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

+ (Object) each(&block)

Yields once for each record.



114
115
116
# File 'lib/aws/record/hash_model/finder_methods.rb', line 114

def each &block
  all.each(&block)
end

+ (Object) find(id) + (Object) find(mode, options = {})

Finds records in Amazon DynamoDB and returns them as objects of the current class.

Finding :all returns an enumerable scope object

People.find(:all, :limit => 10).each do |person|
  puts person.name
end

Finding :first returns a single record (or nil)

boss = People.find(:first)

Find accepts a hash of find modifiers (:shard and :limit). You can also choose to omit these modifiers and chain them on the scope object returned. In the following example only one request is made to SimpleDB (when #each is called)

people = People.find(:all, :limit => 10)

people = people.limit(10).find(:all)

Overloads:

  • + (Object) find(id)

    Parameters:

    • id

      The record to find, raises an exception if the record is not found.

  • + (Object) find(mode, options = {})

    Parameters:

    • mode (:all, :first)

      (:all) When finding :all matching records and array is returned of records. When finding :first then nil or a single record will be returned.

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

    Options Hash (options):

    • :shard (Integer)

      The shard name of the Amazon DynamoDB table to search.

    • :limit (Integer)

      The max number of records to fetch.



76
77
78
# File 'lib/aws/record/hash_model/finder_methods.rb', line 76

def find *args
  new_scope.find(*args)
end

+ (Record::HashModel) find_by_id(id, options = {}) Also known as: []

Returns the record with the given id.

Parameters:

  • id (String)

    The id of the record to load.

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

Options Hash (options):

  • :shard (String)

    Specifies what shard (i.e. table) should be searched.

Returns:

Raises:

  • (RecordNotFound)

    Raises a record not found exception if there was no data found for the given id.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/aws/record/hash_model/finder_methods.rb', line 26

def find_by_id id, options = {}

  table = dynamo_db_table(options[:shard])

  data = table.items[id].attributes.to_h

  raise RecordNotFound, "no data found for id: #{id}" if data.empty?

  obj = self.new(:shard => table)
  obj.send(:hydrate, id, data)
  obj

end

+ (Object?) first(options = {})

Returns the first record found. If there were no records found, nil is returned.

Returns:

  • (Object, nil)

    Returns the first record found. If there were no records found, nil is returned.



152
153
154
# File 'lib/aws/record/hash_model/finder_methods.rb', line 152

def first options = {}
  new_scope.first(options)
end

+ (Object) float_attr(name, options = {})

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.



77
78
79
# File 'lib/aws/record/hash_model/attributes.rb', line 77

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

+ (Object) inherited(sub_class)



24
25
26
27
28
# File 'lib/aws/record/hash_model.rb', line 24

def self.inherited(sub_class)
  sub_class.string_attr :id,
    :hash_key => true,
    :default_hash_key_attribute => true
end

+ (Object) integer_attr(name, options = {})

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.



60
61
62
# File 'lib/aws/record/hash_model/attributes.rb', line 60

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

+ (Object) limit(limit)

The maximum number of records to return. By default, all records matching the where conditions will be returned from a find.

People.limit(10).each {|person| ... }

Limit can be chained with other scope modifiers:

People.where(:age => 40).limit(10).each {|person| ... }


165
166
167
# File 'lib/aws/record/hash_model/finder_methods.rb', line 165

def limit limit
  new_scope.limit(limit)
end

+ (Scope) shard(shard_name) Also known as: domain

Returns a chainable scope object that restricts further scopes to a particular table.

Book.shard('books-2').each do |book|
  # ...
end

Parameters:

  • shard_name (String)

Returns:

  • (Scope)

    Returns a scope for restricting the table searched.



89
90
91
# File 'lib/aws/record/hash_model/finder_methods.rb', line 89

def shard shard_name
  new_scope.shard(shard_name)
end

+ (Object) string_attr(name, options = {})

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.



43
44
45
# File 'lib/aws/record/hash_model/attributes.rb', line 43

def string_attr name, options = {}
  add_attribute(Attributes::StringAttr.new(name, options))
end

+ (Object) timestamps

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


186
187
188
189
190
# File 'lib/aws/record/hash_model/attributes.rb', line 186

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

Instance Method Details

- (String?) hash_key

Returns:

  • (String, nil)


31
32
33
# File 'lib/aws/record/hash_model.rb', line 31

def hash_key
  self[self.class.hash_key]
end