aws-sdk 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.yardopts +6 -0
- data/LICENSE.txt +171 -0
- data/NOTICE.txt +2 -0
- data/README.rdoc +189 -0
- data/lib/aws-sdk.rb +14 -0
- data/lib/aws.rb +63 -0
- data/lib/aws/api_config.rb +45 -0
- data/lib/aws/api_config/.document +0 -0
- data/lib/aws/api_config/EC2-2011-02-28.yml +2314 -0
- data/lib/aws/api_config/SNS-2010-03-31.yml +171 -0
- data/lib/aws/api_config/SQS-2009-02-01.yml +161 -0
- data/lib/aws/api_config/SimpleDB-2009-04-15.yml +278 -0
- data/lib/aws/api_config/SimpleEmailService-2010-12-01.yml +147 -0
- data/lib/aws/api_config_transform.rb +32 -0
- data/lib/aws/async_handle.rb +90 -0
- data/lib/aws/authorize_v2.rb +37 -0
- data/lib/aws/authorize_v3.rb +37 -0
- data/lib/aws/base_client.rb +524 -0
- data/lib/aws/cacheable.rb +92 -0
- data/lib/aws/common.rb +228 -0
- data/lib/aws/configurable.rb +36 -0
- data/lib/aws/configuration.rb +272 -0
- data/lib/aws/configured_client_methods.rb +81 -0
- data/lib/aws/configured_grammars.rb +65 -0
- data/lib/aws/configured_option_grammars.rb +46 -0
- data/lib/aws/configured_xml_grammars.rb +47 -0
- data/lib/aws/default_signer.rb +38 -0
- data/lib/aws/ec2.rb +321 -0
- data/lib/aws/ec2/attachment.rb +149 -0
- data/lib/aws/ec2/attachment_collection.rb +57 -0
- data/lib/aws/ec2/availability_zone.rb +80 -0
- data/lib/aws/ec2/availability_zone_collection.rb +47 -0
- data/lib/aws/ec2/block_device_mappings.rb +53 -0
- data/lib/aws/ec2/client.rb +54 -0
- data/lib/aws/ec2/client/xml.rb +127 -0
- data/lib/aws/ec2/collection.rb +39 -0
- data/lib/aws/ec2/config_transform.rb +63 -0
- data/lib/aws/ec2/elastic_ip.rb +107 -0
- data/lib/aws/ec2/elastic_ip_collection.rb +85 -0
- data/lib/aws/ec2/errors.rb +29 -0
- data/lib/aws/ec2/filtered_collection.rb +65 -0
- data/lib/aws/ec2/has_permissions.rb +46 -0
- data/lib/aws/ec2/image.rb +245 -0
- data/lib/aws/ec2/image_collection.rb +235 -0
- data/lib/aws/ec2/instance.rb +515 -0
- data/lib/aws/ec2/instance_collection.rb +276 -0
- data/lib/aws/ec2/key_pair.rb +86 -0
- data/lib/aws/ec2/key_pair_collection.rb +102 -0
- data/lib/aws/ec2/permission_collection.rb +177 -0
- data/lib/aws/ec2/region.rb +81 -0
- data/lib/aws/ec2/region_collection.rb +55 -0
- data/lib/aws/ec2/request.rb +27 -0
- data/lib/aws/ec2/reserved_instances.rb +50 -0
- data/lib/aws/ec2/reserved_instances_collection.rb +44 -0
- data/lib/aws/ec2/reserved_instances_offering.rb +55 -0
- data/lib/aws/ec2/reserved_instances_offering_collection.rb +43 -0
- data/lib/aws/ec2/resource.rb +340 -0
- data/lib/aws/ec2/resource_tag_collection.rb +218 -0
- data/lib/aws/ec2/security_group.rb +246 -0
- data/lib/aws/ec2/security_group/ip_permission.rb +70 -0
- data/lib/aws/ec2/security_group/ip_permission_collection.rb +59 -0
- data/lib/aws/ec2/security_group_collection.rb +132 -0
- data/lib/aws/ec2/snapshot.rb +138 -0
- data/lib/aws/ec2/snapshot_collection.rb +90 -0
- data/lib/aws/ec2/tag.rb +88 -0
- data/lib/aws/ec2/tag_collection.rb +114 -0
- data/lib/aws/ec2/tagged_collection.rb +48 -0
- data/lib/aws/ec2/tagged_item.rb +87 -0
- data/lib/aws/ec2/volume.rb +190 -0
- data/lib/aws/ec2/volume_collection.rb +95 -0
- data/lib/aws/errors.rb +129 -0
- data/lib/aws/http/builtin_handler.rb +69 -0
- data/lib/aws/http/curb_handler.rb +123 -0
- data/lib/aws/http/handler.rb +77 -0
- data/lib/aws/http/httparty_handler.rb +61 -0
- data/lib/aws/http/request.rb +136 -0
- data/lib/aws/http/request_param.rb +63 -0
- data/lib/aws/http/response.rb +75 -0
- data/lib/aws/ignore_result_element.rb +38 -0
- data/lib/aws/indifferent_hash.rb +86 -0
- data/lib/aws/inflection.rb +46 -0
- data/lib/aws/lazy_error_classes.rb +64 -0
- data/lib/aws/meta_utils.rb +43 -0
- data/lib/aws/model.rb +57 -0
- data/lib/aws/naming.rb +32 -0
- data/lib/aws/option_grammar.rb +544 -0
- data/lib/aws/policy.rb +912 -0
- data/lib/aws/rails.rb +209 -0
- data/lib/aws/record.rb +79 -0
- data/lib/aws/record/attribute.rb +94 -0
- data/lib/aws/record/attribute_macros.rb +288 -0
- data/lib/aws/record/attributes/boolean.rb +49 -0
- data/lib/aws/record/attributes/datetime.rb +86 -0
- data/lib/aws/record/attributes/float.rb +48 -0
- data/lib/aws/record/attributes/integer.rb +68 -0
- data/lib/aws/record/attributes/sortable_float.rb +60 -0
- data/lib/aws/record/attributes/sortable_integer.rb +95 -0
- data/lib/aws/record/attributes/string.rb +69 -0
- data/lib/aws/record/base.rb +728 -0
- data/lib/aws/record/conversion.rb +38 -0
- data/lib/aws/record/dirty_tracking.rb +286 -0
- data/lib/aws/record/errors.rb +153 -0
- data/lib/aws/record/exceptions.rb +48 -0
- data/lib/aws/record/finder_methods.rb +262 -0
- data/lib/aws/record/naming.rb +31 -0
- data/lib/aws/record/scope.rb +157 -0
- data/lib/aws/record/validations.rb +653 -0
- data/lib/aws/record/validator.rb +237 -0
- data/lib/aws/record/validators/acceptance.rb +51 -0
- data/lib/aws/record/validators/block.rb +38 -0
- data/lib/aws/record/validators/confirmation.rb +43 -0
- data/lib/aws/record/validators/count.rb +108 -0
- data/lib/aws/record/validators/exclusion.rb +43 -0
- data/lib/aws/record/validators/format.rb +57 -0
- data/lib/aws/record/validators/inclusion.rb +56 -0
- data/lib/aws/record/validators/length.rb +107 -0
- data/lib/aws/record/validators/numericality.rb +138 -0
- data/lib/aws/record/validators/presence.rb +45 -0
- data/lib/aws/resource_cache.rb +39 -0
- data/lib/aws/response.rb +113 -0
- data/lib/aws/response_cache.rb +50 -0
- data/lib/aws/s3.rb +109 -0
- data/lib/aws/s3/access_control_list.rb +252 -0
- data/lib/aws/s3/acl_object.rb +266 -0
- data/lib/aws/s3/bucket.rb +320 -0
- data/lib/aws/s3/bucket_collection.rb +122 -0
- data/lib/aws/s3/bucket_version_collection.rb +85 -0
- data/lib/aws/s3/client.rb +999 -0
- data/lib/aws/s3/client/xml.rb +190 -0
- data/lib/aws/s3/data_options.rb +99 -0
- data/lib/aws/s3/errors.rb +43 -0
- data/lib/aws/s3/multipart_upload.rb +318 -0
- data/lib/aws/s3/multipart_upload_collection.rb +78 -0
- data/lib/aws/s3/object_collection.rb +159 -0
- data/lib/aws/s3/object_metadata.rb +67 -0
- data/lib/aws/s3/object_upload_collection.rb +83 -0
- data/lib/aws/s3/object_version.rb +141 -0
- data/lib/aws/s3/object_version_collection.rb +78 -0
- data/lib/aws/s3/paginated_collection.rb +94 -0
- data/lib/aws/s3/policy.rb +76 -0
- data/lib/aws/s3/prefix_and_delimiter_collection.rb +56 -0
- data/lib/aws/s3/prefixed_collection.rb +84 -0
- data/lib/aws/s3/presigned_post.rb +504 -0
- data/lib/aws/s3/request.rb +198 -0
- data/lib/aws/s3/s3_object.rb +794 -0
- data/lib/aws/s3/tree.rb +116 -0
- data/lib/aws/s3/tree/branch_node.rb +71 -0
- data/lib/aws/s3/tree/child_collection.rb +108 -0
- data/lib/aws/s3/tree/leaf_node.rb +99 -0
- data/lib/aws/s3/tree/node.rb +22 -0
- data/lib/aws/s3/tree/parent.rb +90 -0
- data/lib/aws/s3/uploaded_part.rb +82 -0
- data/lib/aws/s3/uploaded_part_collection.rb +86 -0
- data/lib/aws/service_interface.rb +60 -0
- data/lib/aws/simple_db.rb +202 -0
- data/lib/aws/simple_db/attribute.rb +159 -0
- data/lib/aws/simple_db/attribute_collection.rb +227 -0
- data/lib/aws/simple_db/client.rb +52 -0
- data/lib/aws/simple_db/client/options.rb +34 -0
- data/lib/aws/simple_db/client/xml.rb +68 -0
- data/lib/aws/simple_db/consistent_read_option.rb +42 -0
- data/lib/aws/simple_db/delete_attributes.rb +64 -0
- data/lib/aws/simple_db/domain.rb +118 -0
- data/lib/aws/simple_db/domain_collection.rb +116 -0
- data/lib/aws/simple_db/domain_metadata.rb +112 -0
- data/lib/aws/simple_db/errors.rb +46 -0
- data/lib/aws/simple_db/expect_condition_option.rb +45 -0
- data/lib/aws/simple_db/item.rb +84 -0
- data/lib/aws/simple_db/item_collection.rb +594 -0
- data/lib/aws/simple_db/item_data.rb +70 -0
- data/lib/aws/simple_db/put_attributes.rb +62 -0
- data/lib/aws/simple_db/request.rb +27 -0
- data/lib/aws/simple_email_service.rb +373 -0
- data/lib/aws/simple_email_service/client.rb +39 -0
- data/lib/aws/simple_email_service/client/options.rb +24 -0
- data/lib/aws/simple_email_service/client/xml.rb +38 -0
- data/lib/aws/simple_email_service/email_address_collection.rb +66 -0
- data/lib/aws/simple_email_service/errors.rb +29 -0
- data/lib/aws/simple_email_service/quotas.rb +64 -0
- data/lib/aws/simple_email_service/request.rb +27 -0
- data/lib/aws/sns.rb +69 -0
- data/lib/aws/sns/client.rb +37 -0
- data/lib/aws/sns/client/options.rb +24 -0
- data/lib/aws/sns/client/xml.rb +38 -0
- data/lib/aws/sns/errors.rb +29 -0
- data/lib/aws/sns/policy.rb +49 -0
- data/lib/aws/sns/request.rb +27 -0
- data/lib/aws/sns/subscription.rb +100 -0
- data/lib/aws/sns/subscription_collection.rb +84 -0
- data/lib/aws/sns/topic.rb +384 -0
- data/lib/aws/sns/topic_collection.rb +70 -0
- data/lib/aws/sns/topic_subscription_collection.rb +58 -0
- data/lib/aws/sqs.rb +70 -0
- data/lib/aws/sqs/client.rb +38 -0
- data/lib/aws/sqs/client/xml.rb +36 -0
- data/lib/aws/sqs/errors.rb +33 -0
- data/lib/aws/sqs/policy.rb +50 -0
- data/lib/aws/sqs/queue.rb +507 -0
- data/lib/aws/sqs/queue_collection.rb +105 -0
- data/lib/aws/sqs/received_message.rb +184 -0
- data/lib/aws/sqs/received_sns_message.rb +112 -0
- data/lib/aws/sqs/request.rb +44 -0
- data/lib/aws/xml_grammar.rb +923 -0
- data/rails/init.rb +15 -0
- metadata +298 -0
@@ -0,0 +1,48 @@
|
|
1
|
+
# Copyright 2011 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
|
+
|
17
|
+
# Raised when trying to access an attribute that does not exist.
|
18
|
+
# @private
|
19
|
+
class UndefinedAttributeError < StandardError
|
20
|
+
def initalize attribute_name
|
21
|
+
super("undefined attribute `#{attribute_name}`")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Raised when calling #save! or #update_attributes! on a record that
|
26
|
+
# has validation errors.
|
27
|
+
# @private
|
28
|
+
class InvalidRecordError < StandardError
|
29
|
+
def initialize record
|
30
|
+
@record = record
|
31
|
+
super(record.errors.full_messages.join(', '))
|
32
|
+
end
|
33
|
+
attr_reader :record
|
34
|
+
end
|
35
|
+
|
36
|
+
# Raised when trying to persist a record that has no attribute values
|
37
|
+
# to persist.
|
38
|
+
# @private
|
39
|
+
class EmptyRecordError < StandardError
|
40
|
+
def initialize record
|
41
|
+
@record = record
|
42
|
+
super('unable persist empty records')
|
43
|
+
end
|
44
|
+
attr_reader :record
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,262 @@
|
|
1
|
+
# Copyright 2011 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/simple_db'
|
15
|
+
require 'aws/record/scope'
|
16
|
+
|
17
|
+
module AWS
|
18
|
+
module Record
|
19
|
+
module FinderMethods
|
20
|
+
|
21
|
+
# @param [String] id The id of the record to load.
|
22
|
+
# @raise [RecordNotFound] Raises a record not found exception if there
|
23
|
+
# was no data found for the given id.
|
24
|
+
# @return [Record::Base] Returns the record, as a sub-class of
|
25
|
+
# Record::Base
|
26
|
+
def [] id
|
27
|
+
|
28
|
+
data = sdb_domain.items[id].data.attributes
|
29
|
+
|
30
|
+
raise RecordNotFound, "no data found for id: #{id}" if data.empty?
|
31
|
+
|
32
|
+
obj = self.new
|
33
|
+
obj.send(:hydrate, id, data)
|
34
|
+
obj
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
# Finds records in SimpleDB and returns them as objects of the
|
39
|
+
# current class.
|
40
|
+
#
|
41
|
+
# Finding +:all+ returns an enumerable scope object
|
42
|
+
#
|
43
|
+
# People.find(:all, :order => [:age, :desc], :limit => 10).each do |person|
|
44
|
+
# puts person.name
|
45
|
+
# end
|
46
|
+
#
|
47
|
+
# Finding +:first+ returns a single record (or nil)
|
48
|
+
#
|
49
|
+
# boss = People.find(:first, :where => { :boss => true })
|
50
|
+
#
|
51
|
+
# Find accepts a hash of find modifiers (+:where+, +:order+ and
|
52
|
+
# +:limit+). You can also choose to omit these modifiers and
|
53
|
+
# chain them on the scope object returned. In the following
|
54
|
+
# example only one request is made to SimpleDB (when #each is
|
55
|
+
# called)
|
56
|
+
#
|
57
|
+
# people = People.find(:all)
|
58
|
+
#
|
59
|
+
# johns = people.where(:name => 'John Doe')
|
60
|
+
#
|
61
|
+
# johns.order(:age, :desc).limit(10).each do |suspects|
|
62
|
+
# # ...
|
63
|
+
# end
|
64
|
+
#
|
65
|
+
# See also {#where}, {#order} and {#limit} for more
|
66
|
+
# information and options.
|
67
|
+
#
|
68
|
+
# @overload find(id)
|
69
|
+
# @param id The record to find, raises an exception if the record is
|
70
|
+
# not found.
|
71
|
+
#
|
72
|
+
# @overload find(mode, options = {})
|
73
|
+
# @param [:all,:first] mode (:all) When finding +:all+ matching records
|
74
|
+
# and array is returned of records. When finding +:first+ then
|
75
|
+
# +nil+ or a single record will be returned.
|
76
|
+
# @param [Hash] options
|
77
|
+
# @option options [Mixed] :where Conditions that determine what
|
78
|
+
# records are returned.
|
79
|
+
# @option options [String,Array] :sort The order records should be
|
80
|
+
# returned in.
|
81
|
+
# @option options [Integer] :limit The max number of records to fetch.
|
82
|
+
def find *args
|
83
|
+
_new_scope.find(*args)
|
84
|
+
end
|
85
|
+
|
86
|
+
# Equivalent to +find(:all)+
|
87
|
+
def all(options = {})
|
88
|
+
find(:all, options)
|
89
|
+
end
|
90
|
+
|
91
|
+
# Counts records in SimpleDB.
|
92
|
+
#
|
93
|
+
# With no arguments, counts all records:
|
94
|
+
#
|
95
|
+
# People.count
|
96
|
+
#
|
97
|
+
# Accepts query options to count a subset of records:
|
98
|
+
#
|
99
|
+
# People.count(:where => { :boss => true })
|
100
|
+
#
|
101
|
+
# You can also count records on a scope object:
|
102
|
+
#
|
103
|
+
# People.find(:all).where(:boss => true).count
|
104
|
+
#
|
105
|
+
# See {#find} and {Scope#count} for more details.
|
106
|
+
#
|
107
|
+
# @param [Hash] options (<code>{}</code>) Options for counting
|
108
|
+
# records.
|
109
|
+
#
|
110
|
+
# @option options [Mixed] :where Conditions that determine what
|
111
|
+
# records are counted.
|
112
|
+
# @option options [Integer] :limit The max number of records to count.
|
113
|
+
def count(options = {})
|
114
|
+
find(:all).count(options)
|
115
|
+
end
|
116
|
+
alias_method :size, :count
|
117
|
+
|
118
|
+
# @return [Object,nil] Returns the first record found for the current
|
119
|
+
# class. If there are no records in the current classes domain, then
|
120
|
+
# nil is returned.
|
121
|
+
def first options = {}
|
122
|
+
_new_scope.find(:first, options)
|
123
|
+
end
|
124
|
+
|
125
|
+
# Limits which records are retried from SimpleDB when performing a find.
|
126
|
+
#
|
127
|
+
# Simple string condition
|
128
|
+
#
|
129
|
+
# Car.where('color = "red" or color = "blue"').each {|car| ... }
|
130
|
+
#
|
131
|
+
# String with placeholders for quoting params
|
132
|
+
#
|
133
|
+
# Car.where('color = ?', 'red')
|
134
|
+
#
|
135
|
+
# Car.where('color = ? OR style = ?', 'red', 'compact')
|
136
|
+
#
|
137
|
+
# # produces a condition using in, like: WHERE color IN ('red', 'blue')
|
138
|
+
# Car.where('color = ?', ['red','blue'])
|
139
|
+
#
|
140
|
+
# Hash arguments
|
141
|
+
#
|
142
|
+
# # WHERE age = '40' AND gender = 'male'
|
143
|
+
# People.where(:age => 40, :gender => 'male').each {|person| ... }
|
144
|
+
#
|
145
|
+
# Chaining where with other scope modifiers
|
146
|
+
#
|
147
|
+
# # 10 most expensive red cars
|
148
|
+
# Car.where(:color => 'red').order(:price, :desc).limit(10)
|
149
|
+
#
|
150
|
+
# @overload where(conditions_hash)
|
151
|
+
# @overload where(sql_fragment[, quote_params, ...])
|
152
|
+
#
|
153
|
+
# @param [Hash] conditions_hash A hash of attributes to values. Each
|
154
|
+
# key/value pair from the hash becomes a find condition. All conditions
|
155
|
+
# are joined by AND.
|
156
|
+
def where *args
|
157
|
+
_new_scope.where(*args)
|
158
|
+
end
|
159
|
+
|
160
|
+
# Defines the order in which records are returned when performing a find.
|
161
|
+
# SimpleDB only allows sorting by one attribute per request.
|
162
|
+
#
|
163
|
+
# # oldest to youngest
|
164
|
+
# People.order(:age, :desc).each {|person| ... }
|
165
|
+
#
|
166
|
+
# You can chain order with the other scope modifiers:
|
167
|
+
#
|
168
|
+
# Pepole.order(:age, :desc).limit(10).each {|person| ... }
|
169
|
+
#
|
170
|
+
# @overload order(attribute, direction = :asc)
|
171
|
+
# @param [String,Symbol] attribute The attribute in SimpleDB to sort by.
|
172
|
+
# @param [:asc,:desc] direction (:asc) The direction to sort, ascending
|
173
|
+
# or descending order.
|
174
|
+
def order *args
|
175
|
+
_new_scope.order(*args)
|
176
|
+
end
|
177
|
+
|
178
|
+
# The maximum number of records to return. By default, all records
|
179
|
+
# matching the where conditions will be returned from a find.
|
180
|
+
#
|
181
|
+
# People.limit(10).each {|person| ... }
|
182
|
+
#
|
183
|
+
# Limit can be chained with other scope modifiers:
|
184
|
+
#
|
185
|
+
# People.where(:age => 40).limit(10).each {|person| ... }
|
186
|
+
#
|
187
|
+
def limit limit
|
188
|
+
_new_scope.limit(limit)
|
189
|
+
end
|
190
|
+
|
191
|
+
# @private
|
192
|
+
def _new_scope
|
193
|
+
Scope.new(self)
|
194
|
+
end
|
195
|
+
private :_new_scope
|
196
|
+
|
197
|
+
# A configuration method to override the default domain name.
|
198
|
+
# @param [String] The domain name that should be used for this class.
|
199
|
+
def set_domain_name name
|
200
|
+
@_domain_name = name
|
201
|
+
end
|
202
|
+
|
203
|
+
# @return [String] Returns the full prefixed domain name for this class.
|
204
|
+
#
|
205
|
+
def domain_name
|
206
|
+
@_domain_name ||= self.to_s
|
207
|
+
"#{Record.domain_prefix}#{@_domain_name}"
|
208
|
+
end
|
209
|
+
|
210
|
+
# @return [AWS::SimpleDB::Domain] A reference to the domain
|
211
|
+
# this class will save data to.
|
212
|
+
def sdb_domain
|
213
|
+
AWS::SimpleDB.new.domains[domain_name]
|
214
|
+
end
|
215
|
+
|
216
|
+
# Creates the SimpleDB domain that is configured for this class.
|
217
|
+
def create_domain
|
218
|
+
AWS::SimpleDB.new.domains.create(domain_name)
|
219
|
+
end
|
220
|
+
|
221
|
+
# @return [Hash] A hash of Attribute objects that represent the
|
222
|
+
# "columns" objects in this domain use.
|
223
|
+
def attributes
|
224
|
+
@attributes ||= {}
|
225
|
+
end
|
226
|
+
|
227
|
+
# @param [Symbol] name The name of the scope. Scope names should be
|
228
|
+
# method-safe and should not conflict with any of the class
|
229
|
+
# methods of {Record::Base} or {Record::Scope}.
|
230
|
+
#
|
231
|
+
# Adding a scope using built-in scope modifiers:
|
232
|
+
#
|
233
|
+
# scope :top_10, order(:rating, :desc).limit(10)
|
234
|
+
#
|
235
|
+
def scope name, scope = nil, &block
|
236
|
+
|
237
|
+
raise ArgumentError, "only a scope or block may be passed, not both" if
|
238
|
+
scope and block_given?
|
239
|
+
|
240
|
+
if scope
|
241
|
+
method_definition = lambda { scope }
|
242
|
+
else
|
243
|
+
method_definition = block
|
244
|
+
end
|
245
|
+
|
246
|
+
extend(Module.new { define_method(name, &method_definition) })
|
247
|
+
|
248
|
+
end
|
249
|
+
|
250
|
+
def optimistic_locking attribute_name = :version_id
|
251
|
+
attribute = integer_attr(attribute_name)
|
252
|
+
@optimistic_locking_attr = attribute
|
253
|
+
end
|
254
|
+
|
255
|
+
# @private
|
256
|
+
def optimistic_locking_attr
|
257
|
+
@optimistic_locking_attr
|
258
|
+
end
|
259
|
+
|
260
|
+
end
|
261
|
+
end
|
262
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# Copyright 2011 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
|
+
|
17
|
+
# @private
|
18
|
+
module Naming
|
19
|
+
|
20
|
+
# This method should only ever get called in a Rails 3+ context
|
21
|
+
# where active model and active support have been loaded. Rails 2
|
22
|
+
# does not call model name on object.
|
23
|
+
# @private
|
24
|
+
def model_name
|
25
|
+
@_model_name ||=
|
26
|
+
ActiveModel::Name.new(self.kind_of?(Class) ? self : self.class)
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,157 @@
|
|
1
|
+
# Copyright 2011 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 Scope
|
17
|
+
|
18
|
+
include Enumerable
|
19
|
+
|
20
|
+
attr_reader :base_class
|
21
|
+
|
22
|
+
def initialize base_class, options = {}
|
23
|
+
@base_class = base_class
|
24
|
+
@options = options
|
25
|
+
end
|
26
|
+
|
27
|
+
def find id_or_mode, options = {}
|
28
|
+
|
29
|
+
scope = _handle_options(options)
|
30
|
+
|
31
|
+
case
|
32
|
+
when id_or_mode == :all then scope
|
33
|
+
when id_or_mode == :first then scope.limit(1).first
|
34
|
+
when scope.send(:_empty?) then base_class[id_or_mode]
|
35
|
+
else
|
36
|
+
object = scope.where('itemName() = ?', id_or_mode).limit(1).first
|
37
|
+
if object.nil?
|
38
|
+
raise RecordNotFound, "no data found for id: #{id_or_mode}"
|
39
|
+
end
|
40
|
+
object
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
def count(options = {})
|
46
|
+
if scope = _handle_options(options) and
|
47
|
+
scope != self
|
48
|
+
scope.count
|
49
|
+
else
|
50
|
+
_item_collection.count
|
51
|
+
end
|
52
|
+
end
|
53
|
+
alias_method :size, :count
|
54
|
+
|
55
|
+
def where *conditions
|
56
|
+
if conditions.empty?
|
57
|
+
raise ArgumentError, 'missing required condition'
|
58
|
+
end
|
59
|
+
_with(:where => Record.as_array(@options[:where]) + [conditions])
|
60
|
+
end
|
61
|
+
|
62
|
+
def order attribute, order = :asc
|
63
|
+
_with(:order => [attribute, order])
|
64
|
+
end
|
65
|
+
|
66
|
+
def limit limit
|
67
|
+
_with(:limit => limit)
|
68
|
+
end
|
69
|
+
|
70
|
+
def each &block
|
71
|
+
if block_given?
|
72
|
+
_each_object(&block)
|
73
|
+
else
|
74
|
+
Enumerator.new(self, :"_each_object")
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# @private
|
79
|
+
def _empty?
|
80
|
+
@options == {}
|
81
|
+
end
|
82
|
+
private :_empty?
|
83
|
+
|
84
|
+
# @private
|
85
|
+
def _each_object &block
|
86
|
+
|
87
|
+
items = _item_collection
|
88
|
+
|
89
|
+
items.select.each do |item_data|
|
90
|
+
obj = base_class.new
|
91
|
+
obj.send(:hydrate, item_data.name, item_data.attributes)
|
92
|
+
yield(obj)
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
private :_each_object
|
97
|
+
|
98
|
+
# @private
|
99
|
+
def _with options
|
100
|
+
Scope.new(base_class, @options.merge(options))
|
101
|
+
end
|
102
|
+
private :_with
|
103
|
+
|
104
|
+
# @private
|
105
|
+
def method_missing scope_name, *args
|
106
|
+
# @todo only proxy named scope methods
|
107
|
+
_merge_scope(base_class.send(scope_name, *args))
|
108
|
+
end
|
109
|
+
private :method_missing
|
110
|
+
|
111
|
+
# @private
|
112
|
+
def _merge_scope scope
|
113
|
+
merged = self
|
114
|
+
scope.instance_variable_get('@options').each_pair do |opt_name,opt_value|
|
115
|
+
unless [nil, []].include?(opt_value)
|
116
|
+
if opt_name == :where
|
117
|
+
opt_value.each do |condition|
|
118
|
+
merged = merged.where(*condition)
|
119
|
+
end
|
120
|
+
else
|
121
|
+
merged = merged.send(opt_name, *opt_value)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
merged
|
126
|
+
end
|
127
|
+
private :_merge_scope
|
128
|
+
|
129
|
+
# @private
|
130
|
+
def _handle_options(options)
|
131
|
+
scope = self
|
132
|
+
options.each_pair do |method, args|
|
133
|
+
if method == :where and args.is_a?(Hash)
|
134
|
+
# splatting a hash turns it into an array, bad juju
|
135
|
+
scope = scope.send(method, args)
|
136
|
+
else
|
137
|
+
scope = scope.send(method, *args)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
scope
|
141
|
+
end
|
142
|
+
private :_handle_options
|
143
|
+
|
144
|
+
# @private
|
145
|
+
def _item_collection
|
146
|
+
items = base_class.sdb_domain.items
|
147
|
+
items = items.order(*@options[:order]) if @options[:order]
|
148
|
+
items = items.limit(*@options[:limit]) if @options[:limit]
|
149
|
+
Record.as_array(@options[:where]).each do |where_condition|
|
150
|
+
items = items.where(*where_condition)
|
151
|
+
end
|
152
|
+
items
|
153
|
+
end
|
154
|
+
private :_item_collection
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|