Class: AWS::Record::HashModel
- Inherits:
-
Object
- Object
- AWS::Record::HashModel
- 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
Direct Known Subclasses
Defined Under Namespace
Classes: Scope
Class Method Summary collapse
-
.all(options = {}) ⇒ Scope
Returns an enumerable scope object represents all records.
-
.boolean_attr(name, options = {}) ⇒ Object
Adds a boolean attribute to this class.
-
.count(options = {}) ⇒ Integer
(also: size)
Counts records Amazon DynamoDB.
-
.create_table(read_capacity_units, write_capacity_units, options = {}) ⇒ DynamoDB::Table
Creates the DynamoDB table that is configured for 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.
- .dynamo_db_table(shard_name = nil) ⇒ DynamoDB::Table
-
.each(&block) ⇒ Object
Yields once for each record.
-
.find(*args) ⇒ Object
Finds records in Amazon DynamoDB and returns them as objects of the current class.
-
.find_by_id(id, options = {}) ⇒ Record::HashModel
(also: [])
Returns the record with the given id.
-
.first(options = {}) ⇒ Object?
Returns the first record found.
-
.float_attr(name, options = {}) ⇒ Object
Adds a float attribute to this class.
-
.integer_attr(name, options = {}) ⇒ Object
Adds an integer attribute to this class.
-
.limit(limit) ⇒ Object
The maximum number of records to return.
-
.shard(shard_name) ⇒ Scope
(also: domain)
Returns a chainable scope object that restricts further scopes to a particular table.
-
.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
.
Methods included from AbstractBase
Class Method Details
.all(options = {}) ⇒ Scope
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
109 110 111 |
# File 'lib/aws/record/hash_model/finder_methods.rb', line 109 def all = {} new_scope.find(:all, ) end |
.boolean_attr(name, options = {}) ⇒ Object
Adds a boolean attribute to this class.
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/aws/record/hash_model/attributes.rb', line 100 def boolean_attr name, = {} attr = add_attribute(Attributes::BooleanAttr.new(name, )) # add the boolean question mark method define_method("#{attr.name}?") do !!__send__(attr.name) end end |
.count(options = {}) ⇒ Integer 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
145 146 147 |
# File 'lib/aws/record/hash_model/finder_methods.rb', line 145 def count = {} new_scope.count() end |
.create_table(read_capacity_units, write_capacity_units, options = {}) ⇒ DynamoDB::Table
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'
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/aws/record/hash_model.rb', line 68 def create_table read_capacity_units, write_capacity_units, = {} table_name = dynamo_db_table_name([:shard_name]) create_opts = {} create_opts[:hash_key] = { :id => :string } dynamo_db.tables.create( table_name, read_capacity_units, write_capacity_units, create_opts) end |
.date_attr(name, options = {}) ⇒ Object
Adds a date attribute to this class.
155 156 157 |
# File 'lib/aws/record/hash_model/attributes.rb', line 155 def date_attr name, = {} add_attribute(Attributes::DateAttr.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.
132 133 134 |
# File 'lib/aws/record/hash_model/attributes.rb', line 132 def datetime_attr name, = {} add_attribute(Attributes::DateTimeAttr.new(name, )) end |
.dynamo_db_table(shard_name = nil) ⇒ DynamoDB::Table
85 86 87 88 89 |
# File 'lib/aws/record/hash_model.rb', line 85 def dynamo_db_table shard_name = nil table = dynamo_db.tables[dynamo_db_table_name(shard_name)] table.hash_key = [:id, :string] table end |
.each(&block) ⇒ Object
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 |
.find(id) ⇒ Object .find(mode, options = {}) ⇒ Object
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)
76 77 78 |
# File 'lib/aws/record/hash_model/finder_methods.rb', line 76 def find *args new_scope.find(*args) end |
.find_by_id(id, options = {}) ⇒ Record::HashModel Also known as: []
Returns the record with 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, = {} table = dynamo_db_table([: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 |
.first(options = {}) ⇒ Object?
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 = {} new_scope.first() end |
.float_attr(name, options = {}) ⇒ Object
79 80 81 |
# File 'lib/aws/record/hash_model/attributes.rb', line 79 def float_attr name, = {} add_attribute(Attributes::FloatAttr.new(name, )) end |
.integer_attr(name, options = {}) ⇒ Object
62 63 64 |
# File 'lib/aws/record/hash_model/attributes.rb', line 62 def integer_attr name, = {} add_attribute(Attributes::IntegerAttr.new(name, )) end |
.limit(limit) ⇒ Object
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 |
.shard(shard_name) ⇒ Scope 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
89 90 91 |
# File 'lib/aws/record/hash_model/finder_methods.rb', line 89 def shard shard_name new_scope.shard(shard_name) end |
.string_attr(name, options = {}) ⇒ Object
Adds a string attribute to this class.
45 46 47 |
# File 'lib/aws/record/hash_model/attributes.rb', line 45 def string_attr name, = {} add_attribute(Attributes::StringAttr.new(name, )) end |
.timestamps ⇒ Object
A convenience method for adding the standard two datetime attributes :created_at
and :updated_at
.
173 174 175 176 177 |
# File 'lib/aws/record/hash_model/attributes.rb', line 173 def c = datetime_attr :created_at u = datetime_attr :updated_at [c, u] end |