aws-sdk 1.2.6 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/aws.rb +2 -0
- data/lib/aws/api_config/DynamoDB-2011-12-05.yml +721 -0
- data/lib/aws/core.rb +10 -1
- data/lib/aws/core/client.rb +17 -12
- data/lib/aws/core/configuration.rb +13 -3
- data/lib/aws/core/configured_json_client_methods.rb +71 -0
- data/lib/aws/core/lazy_error_classes.rb +7 -2
- data/lib/aws/core/option_grammar.rb +67 -13
- data/lib/aws/core/resource.rb +9 -1
- data/lib/aws/core/session_signer.rb +95 -0
- data/lib/aws/dynamo_db.rb +169 -0
- data/lib/aws/dynamo_db/attribute_collection.rb +460 -0
- data/lib/aws/dynamo_db/batch_get.rb +206 -0
- data/lib/aws/dynamo_db/client.rb +119 -0
- data/lib/aws/dynamo_db/config.rb +20 -0
- data/lib/aws/dynamo_db/errors.rb +57 -0
- data/lib/aws/dynamo_db/expectations.rb +40 -0
- data/lib/aws/dynamo_db/item.rb +130 -0
- data/lib/aws/dynamo_db/item_collection.rb +837 -0
- data/lib/aws/{record/optimistic_locking.rb → dynamo_db/item_data.rb} +9 -12
- data/lib/aws/{record/attributes/boolean.rb → dynamo_db/keys.rb} +15 -23
- data/lib/aws/dynamo_db/primary_key_element.rb +47 -0
- data/lib/aws/dynamo_db/request.rb +78 -0
- data/lib/aws/{record/attributes/float.rb → dynamo_db/resource.rb} +10 -25
- data/lib/aws/dynamo_db/table.rb +418 -0
- data/lib/aws/dynamo_db/table_collection.rb +165 -0
- data/lib/aws/dynamo_db/types.rb +86 -0
- data/lib/aws/ec2/resource_tag_collection.rb +3 -1
- data/lib/aws/record.rb +36 -8
- data/lib/aws/record/abstract_base.rb +642 -0
- data/lib/aws/record/attributes.rb +384 -0
- data/lib/aws/record/dirty_tracking.rb +0 -1
- data/lib/aws/record/errors.rb +0 -8
- data/lib/aws/record/hash_model.rb +163 -0
- data/lib/aws/record/hash_model/attributes.rb +182 -0
- data/lib/aws/record/hash_model/finder_methods.rb +178 -0
- data/lib/aws/record/hash_model/scope.rb +108 -0
- data/lib/aws/record/model.rb +429 -0
- data/lib/aws/record/model/attributes.rb +377 -0
- data/lib/aws/record/model/finder_methods.rb +232 -0
- data/lib/aws/record/model/scope.rb +213 -0
- data/lib/aws/record/scope.rb +43 -169
- data/lib/aws/record/validations.rb +11 -11
- data/lib/aws/s3/client.rb +9 -6
- data/lib/aws/s3/object_collection.rb +1 -1
- data/lib/aws/simple_db/expect_condition_option.rb +1 -1
- data/lib/aws/simple_db/item_collection.rb +5 -3
- data/lib/aws/sts/client.rb +9 -0
- metadata +73 -30
- data/lib/aws/record/attribute.rb +0 -94
- data/lib/aws/record/attribute_macros.rb +0 -312
- data/lib/aws/record/attributes/date.rb +0 -89
- data/lib/aws/record/attributes/datetime.rb +0 -86
- data/lib/aws/record/attributes/integer.rb +0 -68
- data/lib/aws/record/attributes/sortable_float.rb +0 -60
- data/lib/aws/record/attributes/sortable_integer.rb +0 -95
- data/lib/aws/record/attributes/string.rb +0 -69
- data/lib/aws/record/base.rb +0 -828
- data/lib/aws/record/finder_methods.rb +0 -230
- data/lib/aws/record/scopes.rb +0 -55
@@ -0,0 +1,182 @@
|
|
1
|
+
# Copyright 2011-2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License"). You
|
4
|
+
# may not use this file except in compliance with the License. A copy of
|
5
|
+
# the License is located at
|
6
|
+
#
|
7
|
+
# http://aws.amazon.com/apache2.0/
|
8
|
+
#
|
9
|
+
# or in the "license" file accompanying this file. This file is
|
10
|
+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
|
11
|
+
# ANY KIND, either express or implied. See the License for the specific
|
12
|
+
# language governing permissions and limitations under the License.
|
13
|
+
|
14
|
+
require 'aws/record/attributes.rb'
|
15
|
+
|
16
|
+
module AWS
|
17
|
+
module Record
|
18
|
+
class HashModel
|
19
|
+
class << self
|
20
|
+
|
21
|
+
# Adds a string attribute to this class.
|
22
|
+
#
|
23
|
+
# @example A standard string attribute
|
24
|
+
#
|
25
|
+
# class Recipe < AWS::Record::Model
|
26
|
+
# string_attr :name
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# recipe = Recipe.new(:name => "Buttermilk Pancakes")
|
30
|
+
# recipe.name #=> 'Buttermilk Pancakes'
|
31
|
+
#
|
32
|
+
# @example A string attribute with +:set+ set to true
|
33
|
+
#
|
34
|
+
# class Recipe < AWS::Record::Model
|
35
|
+
# string_attr :tags, :set => true
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# recipe = Recipe.new(:tags => %w(popular dessert))
|
39
|
+
# recipe.tags #=> #<Set: {"popular", "desert"}>
|
40
|
+
#
|
41
|
+
# @param [Symbol] name The name of the attribute.
|
42
|
+
# @param [Hash] options
|
43
|
+
# @option options [Boolean] :set (false) When true this attribute
|
44
|
+
# can have multiple values.
|
45
|
+
def string_attr name, options = {}
|
46
|
+
add_attribute(Attributes::StringAttr.new(name, options))
|
47
|
+
end
|
48
|
+
|
49
|
+
# Adds an integer attribute to this class.
|
50
|
+
#
|
51
|
+
# class Recipe < AWS::Record::Model
|
52
|
+
# integer_attr :servings
|
53
|
+
# end
|
54
|
+
#
|
55
|
+
# recipe = Recipe.new(:servings => '10')
|
56
|
+
# recipe.servings #=> 10
|
57
|
+
#
|
58
|
+
# @param [Symbol] name The name of the attribute.
|
59
|
+
# @param [Hash] options
|
60
|
+
# @option options [Boolean] :set (false) When true this attribute
|
61
|
+
# can have multiple values.
|
62
|
+
def integer_attr name, options = {}
|
63
|
+
add_attribute(Attributes::IntegerAttr.new(name, options))
|
64
|
+
end
|
65
|
+
|
66
|
+
# Adds a float attribute to this class.
|
67
|
+
#
|
68
|
+
# class Listing < AWS::Record::Model
|
69
|
+
# float_attr :score
|
70
|
+
# end
|
71
|
+
#
|
72
|
+
# listing = Listing.new(:score => '123.456')
|
73
|
+
# listing.score # => 123.456
|
74
|
+
#
|
75
|
+
# @param [Symbol] name The name of the attribute.
|
76
|
+
# @param [Hash] options
|
77
|
+
# @option options [Boolean] :set (false) When true this attribute
|
78
|
+
# can have multiple values.
|
79
|
+
def float_attr name, options = {}
|
80
|
+
add_attribute(Attributes::FloatAttr.new(name, options))
|
81
|
+
end
|
82
|
+
|
83
|
+
# Adds a boolean attribute to this class.
|
84
|
+
#
|
85
|
+
# @example
|
86
|
+
#
|
87
|
+
# class Book < AWS::Record::Model
|
88
|
+
# boolean_attr :read
|
89
|
+
# end
|
90
|
+
#
|
91
|
+
# b = Book.new
|
92
|
+
# b.read? # => false
|
93
|
+
# b.read = true
|
94
|
+
# b.read? # => true
|
95
|
+
#
|
96
|
+
# listing = Listing.new(:score => '123.456'
|
97
|
+
# listing.score # => 123.456
|
98
|
+
#
|
99
|
+
# @param [Symbol] name The name of the attribute.
|
100
|
+
def boolean_attr name, options = {}
|
101
|
+
|
102
|
+
attr = add_attribute(Attributes::BooleanAttr.new(name, options))
|
103
|
+
|
104
|
+
# add the boolean question mark method
|
105
|
+
define_method("#{attr.name}?") do
|
106
|
+
!!__send__(attr.name)
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
# Adds a datetime attribute to this class.
|
112
|
+
#
|
113
|
+
# @example A standard datetime attribute
|
114
|
+
#
|
115
|
+
# class Recipe < AWS::Record::Model
|
116
|
+
# datetime_attr :invented
|
117
|
+
# end
|
118
|
+
#
|
119
|
+
# recipe = Recipe.new(:invented => Time.now)
|
120
|
+
# recipe.invented #=> <DateTime ...>
|
121
|
+
#
|
122
|
+
# If you add a datetime_attr for +:created_at+ and/or +:updated_at+ those
|
123
|
+
# will be automanaged.
|
124
|
+
#
|
125
|
+
# @param [Symbol] name The name of the attribute.
|
126
|
+
#
|
127
|
+
# @param [Hash] options
|
128
|
+
#
|
129
|
+
# @option options [Boolean] :set (false) When true this attribute
|
130
|
+
# can have multiple date times.
|
131
|
+
#
|
132
|
+
def datetime_attr name, options = {}
|
133
|
+
add_attribute(Attributes::DateTimeAttr.new(name, options))
|
134
|
+
end
|
135
|
+
|
136
|
+
# Adds a date attribute to this class.
|
137
|
+
#
|
138
|
+
# @example A standard date attribute
|
139
|
+
#
|
140
|
+
# class Person < AWS::Record::Model
|
141
|
+
# date_attr :birthdate
|
142
|
+
# end
|
143
|
+
#
|
144
|
+
# baby = Person.new
|
145
|
+
# baby.birthdate = Time.now
|
146
|
+
# baby.birthdate #=> <Date: ....>
|
147
|
+
#
|
148
|
+
# @param [Symbol] name The name of the attribute.
|
149
|
+
#
|
150
|
+
# @param [Hash] options
|
151
|
+
#
|
152
|
+
# @option options [Boolean] :set (false) When true this attribute
|
153
|
+
# can have multiple dates.
|
154
|
+
#
|
155
|
+
def date_attr name, options = {}
|
156
|
+
add_attribute(Attributes::DateAttr.new(name, options))
|
157
|
+
end
|
158
|
+
|
159
|
+
# A convenience method for adding the standard two datetime attributes
|
160
|
+
# +:created_at+ and +:updated_at+.
|
161
|
+
#
|
162
|
+
# @example
|
163
|
+
#
|
164
|
+
# class Recipe < AWS::Record::Model
|
165
|
+
# timestamps
|
166
|
+
# end
|
167
|
+
#
|
168
|
+
# recipe = Recipe.new
|
169
|
+
# recipe.save
|
170
|
+
# recipe.created_at #=> <DateTime ...>
|
171
|
+
# recipe.updated_at #=> <DateTime ...>
|
172
|
+
#
|
173
|
+
def timestamps
|
174
|
+
c = datetime_attr :created_at
|
175
|
+
u = datetime_attr :updated_at
|
176
|
+
[c, u]
|
177
|
+
end
|
178
|
+
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
@@ -0,0 +1,178 @@
|
|
1
|
+
# Copyright 2011-2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License"). You
|
4
|
+
# may not use this file except in compliance with the License. A copy of
|
5
|
+
# the License is located at
|
6
|
+
#
|
7
|
+
# http://aws.amazon.com/apache2.0/
|
8
|
+
#
|
9
|
+
# or in the "license" file accompanying this file. This file is
|
10
|
+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
|
11
|
+
# ANY KIND, either express or implied. See the License for the specific
|
12
|
+
# language governing permissions and limitations under the License.
|
13
|
+
|
14
|
+
module AWS
|
15
|
+
module Record
|
16
|
+
class HashModel
|
17
|
+
class << self
|
18
|
+
|
19
|
+
# @param [String] id The id of the record to load.
|
20
|
+
# @param [Hash] options
|
21
|
+
# @option options [String] :shard Specifies what shard (i.e. table)
|
22
|
+
# should be searched.
|
23
|
+
# @raise [RecordNotFound] Raises a record not found exception if there
|
24
|
+
# was no data found for the given id.
|
25
|
+
# @return [Record::HashModel] Returns the record with the given id.
|
26
|
+
def find_by_id id, options = {}
|
27
|
+
|
28
|
+
table = dynamo_db_table(options[:shard])
|
29
|
+
|
30
|
+
data = table.items[id].attributes.to_h
|
31
|
+
|
32
|
+
raise RecordNotFound, "no data found for id: #{id}" if data.empty?
|
33
|
+
|
34
|
+
obj = self.new(:shard => table)
|
35
|
+
obj.send(:hydrate, id, data)
|
36
|
+
obj
|
37
|
+
|
38
|
+
end
|
39
|
+
alias_method :[], :find_by_id
|
40
|
+
|
41
|
+
# Finds records in SimpleDB and returns them as objects of the
|
42
|
+
# current class.
|
43
|
+
#
|
44
|
+
# Finding +:all+ returns an enumerable scope object
|
45
|
+
#
|
46
|
+
# People.find(:all, :order => [:age, :desc], :limit => 10).each do |person|
|
47
|
+
# puts person.name
|
48
|
+
# end
|
49
|
+
#
|
50
|
+
# Finding +:first+ returns a single record (or nil)
|
51
|
+
#
|
52
|
+
# boss = People.find(:first, :where => { :boss => true })
|
53
|
+
#
|
54
|
+
# Find accepts a hash of find modifiers (+:where+, +:order+ and
|
55
|
+
# +:limit+). You can also choose to omit these modifiers and
|
56
|
+
# chain them on the scope object returned. In the following
|
57
|
+
# example only one request is made to SimpleDB (when #each is
|
58
|
+
# called)
|
59
|
+
#
|
60
|
+
# people = People.find(:all)
|
61
|
+
#
|
62
|
+
# johns = people.where(:name => 'John Doe')
|
63
|
+
#
|
64
|
+
# johns.order(:age, :desc).limit(10).each do |suspects|
|
65
|
+
# # ...
|
66
|
+
# end
|
67
|
+
#
|
68
|
+
# See also {#where}, {#order} and {#limit} for more
|
69
|
+
# information and options.
|
70
|
+
#
|
71
|
+
# @overload find(id)
|
72
|
+
# @param id The record to find, raises an exception if the record is
|
73
|
+
# not found.
|
74
|
+
#
|
75
|
+
# @overload find(mode, options = {})
|
76
|
+
# @param [:all,:first] mode (:all) When finding +:all+ matching records
|
77
|
+
# and array is returned of records. When finding +:first+ then
|
78
|
+
# +nil+ or a single record will be returned.
|
79
|
+
# @param [Hash] options
|
80
|
+
# @option options [Integer] :limit The max number of records to fetch.
|
81
|
+
def find *args
|
82
|
+
new_scope.find(*args)
|
83
|
+
end
|
84
|
+
|
85
|
+
# Returns a chainable scope object that restricts further scopes to a
|
86
|
+
# particular table.
|
87
|
+
#
|
88
|
+
# Book.shard('books-2').each do |book|
|
89
|
+
# # ...
|
90
|
+
# end
|
91
|
+
#
|
92
|
+
# @param [String] shard_name
|
93
|
+
# @return [Scope] Returns a scope for restricting the table searched.
|
94
|
+
def shard shard_name
|
95
|
+
new_scope.shard(shard_name)
|
96
|
+
end
|
97
|
+
alias_method :domain, :shard # backwards compat
|
98
|
+
|
99
|
+
# Returns an enumerable scope object represents all records.
|
100
|
+
#
|
101
|
+
# Book.all.each do |book|
|
102
|
+
# # ...
|
103
|
+
# end
|
104
|
+
#
|
105
|
+
# This method is equivalent to +find(:all)+, and therefore you can also
|
106
|
+
# pass aditional options. See {#find} for more information on what
|
107
|
+
# options you can pass.
|
108
|
+
#
|
109
|
+
# Book.all(:where => { :author' => 'me' }).each do |my_book|
|
110
|
+
# # ...
|
111
|
+
# end
|
112
|
+
#
|
113
|
+
# @return [Scope] Returns an enumerable scope object.
|
114
|
+
#
|
115
|
+
def all options = {}
|
116
|
+
new_scope.find(:all, options)
|
117
|
+
end
|
118
|
+
|
119
|
+
# Yields once for each record.
|
120
|
+
def each &block
|
121
|
+
all.each(&block)
|
122
|
+
end
|
123
|
+
|
124
|
+
# Counts records Amazon DynamoDB.
|
125
|
+
#
|
126
|
+
# class Product < AWS::Record::HashModel
|
127
|
+
# end
|
128
|
+
#
|
129
|
+
# # returns the count of records in the 'Product' table
|
130
|
+
# Product.count
|
131
|
+
#
|
132
|
+
# You can specify the table via #shard
|
133
|
+
#
|
134
|
+
# # returns the count of records in the 'products-1' table
|
135
|
+
# Product.shard('products-1').count
|
136
|
+
#
|
137
|
+
# You can also specify the shard as an option to #count.
|
138
|
+
#
|
139
|
+
# Product.count(:shard => 'table-name')
|
140
|
+
#
|
141
|
+
# Chaining #count with #limit has no effect on the count.
|
142
|
+
#
|
143
|
+
# Product.limit(10).count # same as Product.count, limit ignored
|
144
|
+
#
|
145
|
+
# @param [Hash] options
|
146
|
+
#
|
147
|
+
# @option [String] :shard Which shard to count records in.
|
148
|
+
#
|
149
|
+
# @return [Integer] The count of records in the table.
|
150
|
+
#
|
151
|
+
def count
|
152
|
+
new_scope.count(options)
|
153
|
+
end
|
154
|
+
alias_method :size, :count
|
155
|
+
|
156
|
+
# @return [Object,nil] Returns the first record found. If there were
|
157
|
+
# no records found, nil is returned.
|
158
|
+
def first options = {}
|
159
|
+
new_scope.first(options)
|
160
|
+
end
|
161
|
+
|
162
|
+
# The maximum number of records to return. By default, all records
|
163
|
+
# matching the where conditions will be returned from a find.
|
164
|
+
#
|
165
|
+
# People.limit(10).each {|person| ... }
|
166
|
+
#
|
167
|
+
# Limit can be chained with other scope modifiers:
|
168
|
+
#
|
169
|
+
# People.where(:age => 40).limit(10).each {|person| ... }
|
170
|
+
#
|
171
|
+
def limit limit
|
172
|
+
new_scope.limit(limit)
|
173
|
+
end
|
174
|
+
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
# Copyright 2011-2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License"). You
|
4
|
+
# may not use this file except in compliance with the License. A copy of
|
5
|
+
# the License is located at
|
6
|
+
#
|
7
|
+
# http://aws.amazon.com/apache2.0/
|
8
|
+
#
|
9
|
+
# or in the "license" file accompanying this file. This file is
|
10
|
+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
|
11
|
+
# ANY KIND, either express or implied. See the License for the specific
|
12
|
+
# language governing permissions and limitations under the License.
|
13
|
+
|
14
|
+
module AWS
|
15
|
+
module Record
|
16
|
+
class HashModel
|
17
|
+
|
18
|
+
# The primary interface for finding records with {AWS::Record::HashModel}.
|
19
|
+
#
|
20
|
+
# == Getting a Scope Object
|
21
|
+
#
|
22
|
+
# You should normally never need to construct a Scope object directly.
|
23
|
+
# Scope objects are returned from the AWS::Record::HashModel finder
|
24
|
+
# methods # (e.g. +shard+ and +limit+).
|
25
|
+
#
|
26
|
+
# books = Book.limit(100)
|
27
|
+
# books.class #=> AWS::Record::HashModel::Scope
|
28
|
+
#
|
29
|
+
# Scopes are also returned from methods defined with the +scope+ method.
|
30
|
+
#
|
31
|
+
# class Book < AWS::Record::HashModel
|
32
|
+
# scope :sampling, limit(10)
|
33
|
+
# end
|
34
|
+
#
|
35
|
+
# Book.sampling #=> returns a scope that limits to 10
|
36
|
+
#
|
37
|
+
# == Chaining Scopes
|
38
|
+
#
|
39
|
+
# Scope objects represent a request, but do not actualy make a request
|
40
|
+
# until required. This allows you to chain requests
|
41
|
+
#
|
42
|
+
# # no request made by the following 2 statements
|
43
|
+
# books = Book.shard('books-1') # what table to search
|
44
|
+
# books = books.limit(10) # how many records to fetch
|
45
|
+
#
|
46
|
+
# books.each do |book|
|
47
|
+
# # yields up to 10 books from the table 'books-1'
|
48
|
+
# end
|
49
|
+
#
|
50
|
+
# The following methods returns a scope that can be chained.
|
51
|
+
#
|
52
|
+
# * {#shard}
|
53
|
+
# * {#limit}
|
54
|
+
#
|
55
|
+
# == Terminating Scopes
|
56
|
+
#
|
57
|
+
# To terminate a scope you can enumerate it or call #first.
|
58
|
+
#
|
59
|
+
# # terminate a scope by enumerating
|
60
|
+
# Book.limit(10).each {|book| ... }
|
61
|
+
#
|
62
|
+
# # terminate a scope by getting the first record
|
63
|
+
# Book.shard('books-1').first
|
64
|
+
#
|
65
|
+
class Scope < Record::Scope
|
66
|
+
|
67
|
+
private
|
68
|
+
def _each_object &block
|
69
|
+
|
70
|
+
items = _item_collection
|
71
|
+
|
72
|
+
items.select(:limit => @options[:limit]).each do |item_data|
|
73
|
+
obj = base_class.new(:shard => _shard)
|
74
|
+
obj.send(:hydrate, item_data.attributes['id'], item_data.attributes)
|
75
|
+
yield(obj)
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
def _merge_scope scope
|
82
|
+
merged = self
|
83
|
+
scope.instance_variable_get('@options').each_pair do |opt_name,opt_value|
|
84
|
+
unless [nil, []].include?(opt_value)
|
85
|
+
merged = merged.send(opt_name, *opt_value)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
merged
|
89
|
+
end
|
90
|
+
|
91
|
+
private
|
92
|
+
def _handle_options options
|
93
|
+
scope = self
|
94
|
+
options.each_pair do |method, args|
|
95
|
+
scope = scope.send(method, *args)
|
96
|
+
end
|
97
|
+
scope
|
98
|
+
end
|
99
|
+
|
100
|
+
private
|
101
|
+
def _item_collection
|
102
|
+
base_class.dynamo_db_table(_shard).items
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|