aws-sdk 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (205) hide show
  1. data/.yardopts +6 -0
  2. data/LICENSE.txt +171 -0
  3. data/NOTICE.txt +2 -0
  4. data/README.rdoc +189 -0
  5. data/lib/aws-sdk.rb +14 -0
  6. data/lib/aws.rb +63 -0
  7. data/lib/aws/api_config.rb +45 -0
  8. data/lib/aws/api_config/.document +0 -0
  9. data/lib/aws/api_config/EC2-2011-02-28.yml +2314 -0
  10. data/lib/aws/api_config/SNS-2010-03-31.yml +171 -0
  11. data/lib/aws/api_config/SQS-2009-02-01.yml +161 -0
  12. data/lib/aws/api_config/SimpleDB-2009-04-15.yml +278 -0
  13. data/lib/aws/api_config/SimpleEmailService-2010-12-01.yml +147 -0
  14. data/lib/aws/api_config_transform.rb +32 -0
  15. data/lib/aws/async_handle.rb +90 -0
  16. data/lib/aws/authorize_v2.rb +37 -0
  17. data/lib/aws/authorize_v3.rb +37 -0
  18. data/lib/aws/base_client.rb +524 -0
  19. data/lib/aws/cacheable.rb +92 -0
  20. data/lib/aws/common.rb +228 -0
  21. data/lib/aws/configurable.rb +36 -0
  22. data/lib/aws/configuration.rb +272 -0
  23. data/lib/aws/configured_client_methods.rb +81 -0
  24. data/lib/aws/configured_grammars.rb +65 -0
  25. data/lib/aws/configured_option_grammars.rb +46 -0
  26. data/lib/aws/configured_xml_grammars.rb +47 -0
  27. data/lib/aws/default_signer.rb +38 -0
  28. data/lib/aws/ec2.rb +321 -0
  29. data/lib/aws/ec2/attachment.rb +149 -0
  30. data/lib/aws/ec2/attachment_collection.rb +57 -0
  31. data/lib/aws/ec2/availability_zone.rb +80 -0
  32. data/lib/aws/ec2/availability_zone_collection.rb +47 -0
  33. data/lib/aws/ec2/block_device_mappings.rb +53 -0
  34. data/lib/aws/ec2/client.rb +54 -0
  35. data/lib/aws/ec2/client/xml.rb +127 -0
  36. data/lib/aws/ec2/collection.rb +39 -0
  37. data/lib/aws/ec2/config_transform.rb +63 -0
  38. data/lib/aws/ec2/elastic_ip.rb +107 -0
  39. data/lib/aws/ec2/elastic_ip_collection.rb +85 -0
  40. data/lib/aws/ec2/errors.rb +29 -0
  41. data/lib/aws/ec2/filtered_collection.rb +65 -0
  42. data/lib/aws/ec2/has_permissions.rb +46 -0
  43. data/lib/aws/ec2/image.rb +245 -0
  44. data/lib/aws/ec2/image_collection.rb +235 -0
  45. data/lib/aws/ec2/instance.rb +515 -0
  46. data/lib/aws/ec2/instance_collection.rb +276 -0
  47. data/lib/aws/ec2/key_pair.rb +86 -0
  48. data/lib/aws/ec2/key_pair_collection.rb +102 -0
  49. data/lib/aws/ec2/permission_collection.rb +177 -0
  50. data/lib/aws/ec2/region.rb +81 -0
  51. data/lib/aws/ec2/region_collection.rb +55 -0
  52. data/lib/aws/ec2/request.rb +27 -0
  53. data/lib/aws/ec2/reserved_instances.rb +50 -0
  54. data/lib/aws/ec2/reserved_instances_collection.rb +44 -0
  55. data/lib/aws/ec2/reserved_instances_offering.rb +55 -0
  56. data/lib/aws/ec2/reserved_instances_offering_collection.rb +43 -0
  57. data/lib/aws/ec2/resource.rb +340 -0
  58. data/lib/aws/ec2/resource_tag_collection.rb +218 -0
  59. data/lib/aws/ec2/security_group.rb +246 -0
  60. data/lib/aws/ec2/security_group/ip_permission.rb +70 -0
  61. data/lib/aws/ec2/security_group/ip_permission_collection.rb +59 -0
  62. data/lib/aws/ec2/security_group_collection.rb +132 -0
  63. data/lib/aws/ec2/snapshot.rb +138 -0
  64. data/lib/aws/ec2/snapshot_collection.rb +90 -0
  65. data/lib/aws/ec2/tag.rb +88 -0
  66. data/lib/aws/ec2/tag_collection.rb +114 -0
  67. data/lib/aws/ec2/tagged_collection.rb +48 -0
  68. data/lib/aws/ec2/tagged_item.rb +87 -0
  69. data/lib/aws/ec2/volume.rb +190 -0
  70. data/lib/aws/ec2/volume_collection.rb +95 -0
  71. data/lib/aws/errors.rb +129 -0
  72. data/lib/aws/http/builtin_handler.rb +69 -0
  73. data/lib/aws/http/curb_handler.rb +123 -0
  74. data/lib/aws/http/handler.rb +77 -0
  75. data/lib/aws/http/httparty_handler.rb +61 -0
  76. data/lib/aws/http/request.rb +136 -0
  77. data/lib/aws/http/request_param.rb +63 -0
  78. data/lib/aws/http/response.rb +75 -0
  79. data/lib/aws/ignore_result_element.rb +38 -0
  80. data/lib/aws/indifferent_hash.rb +86 -0
  81. data/lib/aws/inflection.rb +46 -0
  82. data/lib/aws/lazy_error_classes.rb +64 -0
  83. data/lib/aws/meta_utils.rb +43 -0
  84. data/lib/aws/model.rb +57 -0
  85. data/lib/aws/naming.rb +32 -0
  86. data/lib/aws/option_grammar.rb +544 -0
  87. data/lib/aws/policy.rb +912 -0
  88. data/lib/aws/rails.rb +209 -0
  89. data/lib/aws/record.rb +79 -0
  90. data/lib/aws/record/attribute.rb +94 -0
  91. data/lib/aws/record/attribute_macros.rb +288 -0
  92. data/lib/aws/record/attributes/boolean.rb +49 -0
  93. data/lib/aws/record/attributes/datetime.rb +86 -0
  94. data/lib/aws/record/attributes/float.rb +48 -0
  95. data/lib/aws/record/attributes/integer.rb +68 -0
  96. data/lib/aws/record/attributes/sortable_float.rb +60 -0
  97. data/lib/aws/record/attributes/sortable_integer.rb +95 -0
  98. data/lib/aws/record/attributes/string.rb +69 -0
  99. data/lib/aws/record/base.rb +728 -0
  100. data/lib/aws/record/conversion.rb +38 -0
  101. data/lib/aws/record/dirty_tracking.rb +286 -0
  102. data/lib/aws/record/errors.rb +153 -0
  103. data/lib/aws/record/exceptions.rb +48 -0
  104. data/lib/aws/record/finder_methods.rb +262 -0
  105. data/lib/aws/record/naming.rb +31 -0
  106. data/lib/aws/record/scope.rb +157 -0
  107. data/lib/aws/record/validations.rb +653 -0
  108. data/lib/aws/record/validator.rb +237 -0
  109. data/lib/aws/record/validators/acceptance.rb +51 -0
  110. data/lib/aws/record/validators/block.rb +38 -0
  111. data/lib/aws/record/validators/confirmation.rb +43 -0
  112. data/lib/aws/record/validators/count.rb +108 -0
  113. data/lib/aws/record/validators/exclusion.rb +43 -0
  114. data/lib/aws/record/validators/format.rb +57 -0
  115. data/lib/aws/record/validators/inclusion.rb +56 -0
  116. data/lib/aws/record/validators/length.rb +107 -0
  117. data/lib/aws/record/validators/numericality.rb +138 -0
  118. data/lib/aws/record/validators/presence.rb +45 -0
  119. data/lib/aws/resource_cache.rb +39 -0
  120. data/lib/aws/response.rb +113 -0
  121. data/lib/aws/response_cache.rb +50 -0
  122. data/lib/aws/s3.rb +109 -0
  123. data/lib/aws/s3/access_control_list.rb +252 -0
  124. data/lib/aws/s3/acl_object.rb +266 -0
  125. data/lib/aws/s3/bucket.rb +320 -0
  126. data/lib/aws/s3/bucket_collection.rb +122 -0
  127. data/lib/aws/s3/bucket_version_collection.rb +85 -0
  128. data/lib/aws/s3/client.rb +999 -0
  129. data/lib/aws/s3/client/xml.rb +190 -0
  130. data/lib/aws/s3/data_options.rb +99 -0
  131. data/lib/aws/s3/errors.rb +43 -0
  132. data/lib/aws/s3/multipart_upload.rb +318 -0
  133. data/lib/aws/s3/multipart_upload_collection.rb +78 -0
  134. data/lib/aws/s3/object_collection.rb +159 -0
  135. data/lib/aws/s3/object_metadata.rb +67 -0
  136. data/lib/aws/s3/object_upload_collection.rb +83 -0
  137. data/lib/aws/s3/object_version.rb +141 -0
  138. data/lib/aws/s3/object_version_collection.rb +78 -0
  139. data/lib/aws/s3/paginated_collection.rb +94 -0
  140. data/lib/aws/s3/policy.rb +76 -0
  141. data/lib/aws/s3/prefix_and_delimiter_collection.rb +56 -0
  142. data/lib/aws/s3/prefixed_collection.rb +84 -0
  143. data/lib/aws/s3/presigned_post.rb +504 -0
  144. data/lib/aws/s3/request.rb +198 -0
  145. data/lib/aws/s3/s3_object.rb +794 -0
  146. data/lib/aws/s3/tree.rb +116 -0
  147. data/lib/aws/s3/tree/branch_node.rb +71 -0
  148. data/lib/aws/s3/tree/child_collection.rb +108 -0
  149. data/lib/aws/s3/tree/leaf_node.rb +99 -0
  150. data/lib/aws/s3/tree/node.rb +22 -0
  151. data/lib/aws/s3/tree/parent.rb +90 -0
  152. data/lib/aws/s3/uploaded_part.rb +82 -0
  153. data/lib/aws/s3/uploaded_part_collection.rb +86 -0
  154. data/lib/aws/service_interface.rb +60 -0
  155. data/lib/aws/simple_db.rb +202 -0
  156. data/lib/aws/simple_db/attribute.rb +159 -0
  157. data/lib/aws/simple_db/attribute_collection.rb +227 -0
  158. data/lib/aws/simple_db/client.rb +52 -0
  159. data/lib/aws/simple_db/client/options.rb +34 -0
  160. data/lib/aws/simple_db/client/xml.rb +68 -0
  161. data/lib/aws/simple_db/consistent_read_option.rb +42 -0
  162. data/lib/aws/simple_db/delete_attributes.rb +64 -0
  163. data/lib/aws/simple_db/domain.rb +118 -0
  164. data/lib/aws/simple_db/domain_collection.rb +116 -0
  165. data/lib/aws/simple_db/domain_metadata.rb +112 -0
  166. data/lib/aws/simple_db/errors.rb +46 -0
  167. data/lib/aws/simple_db/expect_condition_option.rb +45 -0
  168. data/lib/aws/simple_db/item.rb +84 -0
  169. data/lib/aws/simple_db/item_collection.rb +594 -0
  170. data/lib/aws/simple_db/item_data.rb +70 -0
  171. data/lib/aws/simple_db/put_attributes.rb +62 -0
  172. data/lib/aws/simple_db/request.rb +27 -0
  173. data/lib/aws/simple_email_service.rb +373 -0
  174. data/lib/aws/simple_email_service/client.rb +39 -0
  175. data/lib/aws/simple_email_service/client/options.rb +24 -0
  176. data/lib/aws/simple_email_service/client/xml.rb +38 -0
  177. data/lib/aws/simple_email_service/email_address_collection.rb +66 -0
  178. data/lib/aws/simple_email_service/errors.rb +29 -0
  179. data/lib/aws/simple_email_service/quotas.rb +64 -0
  180. data/lib/aws/simple_email_service/request.rb +27 -0
  181. data/lib/aws/sns.rb +69 -0
  182. data/lib/aws/sns/client.rb +37 -0
  183. data/lib/aws/sns/client/options.rb +24 -0
  184. data/lib/aws/sns/client/xml.rb +38 -0
  185. data/lib/aws/sns/errors.rb +29 -0
  186. data/lib/aws/sns/policy.rb +49 -0
  187. data/lib/aws/sns/request.rb +27 -0
  188. data/lib/aws/sns/subscription.rb +100 -0
  189. data/lib/aws/sns/subscription_collection.rb +84 -0
  190. data/lib/aws/sns/topic.rb +384 -0
  191. data/lib/aws/sns/topic_collection.rb +70 -0
  192. data/lib/aws/sns/topic_subscription_collection.rb +58 -0
  193. data/lib/aws/sqs.rb +70 -0
  194. data/lib/aws/sqs/client.rb +38 -0
  195. data/lib/aws/sqs/client/xml.rb +36 -0
  196. data/lib/aws/sqs/errors.rb +33 -0
  197. data/lib/aws/sqs/policy.rb +50 -0
  198. data/lib/aws/sqs/queue.rb +507 -0
  199. data/lib/aws/sqs/queue_collection.rb +105 -0
  200. data/lib/aws/sqs/received_message.rb +184 -0
  201. data/lib/aws/sqs/received_sns_message.rb +112 -0
  202. data/lib/aws/sqs/request.rb +44 -0
  203. data/lib/aws/xml_grammar.rb +923 -0
  204. data/rails/init.rb +15 -0
  205. metadata +298 -0
@@ -0,0 +1,38 @@
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 Conversion
19
+
20
+ # @private
21
+ def to_model
22
+ self
23
+ end
24
+
25
+ # @private
26
+ def to_key
27
+ persisted? ? [id] : nil
28
+ end
29
+
30
+ # @private
31
+ def to_param
32
+ persisted? ? to_key.join('-') : nil
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,286 @@
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
+
18
+ # Provides a way to track changes in your records.
19
+ #
20
+ # my_book = Book['bookid']
21
+ #
22
+ # my_book.changed? #=> false
23
+ # my_book.title #=> "My Book"
24
+ # my_book.title = "My Awesome Book"
25
+ # my_book.changed? #=> true
26
+ #
27
+ # You can inspect further and get a list of changed attributes
28
+ #
29
+ # my_book.changed #=> ['title']
30
+ #
31
+ # Or you can get a more detailed description of the changes. {#changes}
32
+ # returns a hash of changed attributes (keys) with their old and new
33
+ # values.
34
+ #
35
+ # my_book.changes
36
+ # #=> { 'title' => ['My Book', 'My Awesome Book']
37
+ #
38
+ # For every configured attribute you also get a handful of methods
39
+ # for inspecting changes on that attribute. Given the following
40
+ # attribute:
41
+ #
42
+ # string_attr :title
43
+ #
44
+ # You can now call any of the following methods:
45
+ #
46
+ # * title_changed?
47
+ # * title_change
48
+ # * title_was
49
+ # * reset_title!
50
+ # * title_will_change!
51
+ #
52
+ # Given the title change from above:
53
+ #
54
+ # my_book.title_changed? #=> true
55
+ # my_book.title_change #=> ['My Book', 'My Awesome Book']
56
+ # my_book.title_was #=> ['My Book']
57
+ #
58
+ # my_book.reset_title!
59
+ # my_book.title #=> 'My Book'
60
+ #
61
+ # == In-Place Editing
62
+ #
63
+ # Dirty tracking works by comparing incoming attribute values upon
64
+ # assignment against the value that was there previously. If you
65
+ # use functions against the value that modify it (like gsub!)
66
+ # you must notify your record about the coming change.
67
+ #
68
+ # my_book.title #=> 'My Book'
69
+ # my_book.title_will_change!
70
+ # my_book.title.gsub!(/My/, 'Your')
71
+ # my_book.title_change #=> ['My Book', 'Your Book']
72
+ #
73
+ # == Partial Updates
74
+ #
75
+ # Dirty tracking makes it possible to only persist those attributes
76
+ # that have changed since they were loaded. This speeds up requests
77
+ # against AWS when saving data.
78
+ #
79
+ module DirtyTracking
80
+
81
+ # Returns true if this model has unsaved changes.
82
+ #
83
+ # b = Book.new(:title => 'My Book')
84
+ # b.changed?
85
+ # #=> true
86
+ #
87
+ # New objects and objects freshly loaded should not have any changes:
88
+ #
89
+ # b = Book.new
90
+ # b.changed? #=> false
91
+ #
92
+ # b = Book.first
93
+ # b.changed? #=> false
94
+ #
95
+ # @return [Boolean] Returns true if any of the attributes have
96
+ # unsaved changes.
97
+ def changed?
98
+ !orig_values.empty?
99
+ end
100
+
101
+ # Returns an array of attribute names that have changes.
102
+ #
103
+ # book.changed #=> []
104
+ # person.title = 'New Title'
105
+ # book.changed #=> ['title']
106
+ #
107
+ # @return [Array] Returns an array of attribute names that have
108
+ # unsaved changes.
109
+ def changed
110
+ orig_values.keys
111
+ end
112
+
113
+ # Returns the changed attributes in a hash. Keys are attribute names,
114
+ # values are two value arrays. The first value is the previous
115
+ # attribute value, the second is the current attribute value.
116
+ #
117
+ # book.title = 'New Title'
118
+ # book.changes
119
+ # #=> { 'title' => ['Old Title', 'New Title'] }
120
+ #
121
+ # @return [Hash] Returns a hash of attribute changes.
122
+ def changes
123
+ changed.inject({}) do |changes, attr_name|
124
+ changes[attr_name] = attribute_change(attr_name)
125
+ changes
126
+ end
127
+ end
128
+
129
+ # Returns true if the named attribute has unsaved changes.
130
+ #
131
+ # This is an attribute method. The following two expressions
132
+ # are equivilent:
133
+ #
134
+ # book.title_changed?
135
+ # book.attribute_changed?(:title)
136
+ #
137
+ # @param [String] attribute_name Name of the attribute to check
138
+ # for changes.
139
+ #
140
+ # @return [Boolean] Returns true if the named attribute
141
+ # has unsaved changes.
142
+ # @private
143
+ private
144
+ def attribute_changed? attribute_name
145
+ orig_values.keys.include?(attribute_name)
146
+ end
147
+
148
+ # Returns an array of the old value and the new value for
149
+ # attributes that have unsaved changes, returns nil otherwise.
150
+ #
151
+ # This is an attribute method. The following two expressions
152
+ # are equivilent:
153
+ #
154
+ # book.title_change
155
+ # book.attribute_change(:title)
156
+ #
157
+ # @example Asking for changes on an unchanged attribute
158
+ #
159
+ # book = Book.new
160
+ # book.title_change #=> nil
161
+ #
162
+ # @example Getting changed attributes on a new object
163
+ #
164
+ # book = Book.new(:title => 'My Book')
165
+ # book.title_change #=> [nil, 'My Book']
166
+ #
167
+ # @example Getting changed attributes on a loaded object
168
+ #
169
+ # book = Book.first
170
+ # book.title = 'New Title'
171
+ # book.title_change #=> ['Old Title', 'New Title']
172
+ #
173
+ # @param [String] attribute_name Name of the attribute to fetch
174
+ # a change for.
175
+ # @return [Boolean] Returns true if the named attribute
176
+ # has unsaved changes.
177
+ # @private
178
+ private
179
+ def attribute_change attribute_name
180
+ self.class.attribute_for(attribute_name) do |attribute|
181
+ if orig_values.has_key?(attribute.name)
182
+ [orig_values[attribute.name], __send__(attribute.name)]
183
+ else
184
+ nil
185
+ end
186
+ end
187
+ end
188
+
189
+ # Returns the previous value for changed attributes, or the current
190
+ # value for unchanged attributes.
191
+ #
192
+ # This is an attribute method. The following two expressions
193
+ # are equivilent:
194
+ #
195
+ # book.title_was
196
+ # book.attribute_was(:title)
197
+ #
198
+ # @example Returns the previous value for changed attributes:
199
+ #
200
+ # book = Book.where(:title => 'My Book').first
201
+ # book.title = 'New Title'
202
+ # book.title_was #=> 'My Book'
203
+ #
204
+ # @example Returns the current value for unchanged attributes:
205
+ #
206
+ # book = Book.where(:title => 'My Book').first
207
+ # book.title_was #=> 'My Book'
208
+ #
209
+ # @return Returns the previous value for changed attributes
210
+ # or the current value for unchanged attributes.
211
+ # @private
212
+ private
213
+ def attribute_was attribute_name
214
+ self.class.attribute_for(attribute_name) do |attribute|
215
+ name = attribute.name
216
+ orig_values.has_key?(name) ? orig_values[name] : __send__(name)
217
+ end
218
+ end
219
+
220
+ # Reverts any changes to the attribute, restoring its original value.
221
+ # @param [String] attribute_name Name of the attribute to reset.
222
+ # @return [nil]
223
+ # @private
224
+ private
225
+ def reset_attribute! attribute_name
226
+ __send__("#{attribute_name}=", attribute_was(attribute_name))
227
+ nil
228
+ end
229
+
230
+ # Indicate to the record that you are about to edit an attribute
231
+ # in place.
232
+ # @param [String] attribute_name Name of the attribute that will
233
+ # be changed.
234
+ # @return [nil]
235
+ # @private
236
+ private
237
+ def attribute_will_change! attribute_name
238
+ self.class.attribute_for(attribute_name) do |attribute|
239
+ name = attribute.name
240
+ unless orig_values.has_key?(name)
241
+ was = __send__(name)
242
+ begin
243
+ # booleans, nil, etc all #respond_to?(:clone), but they raise
244
+ # a TypeError when you attempt to dup them.
245
+ orig_values[name] = was.clone
246
+ rescue TypeError
247
+ orig_values[name] = was
248
+ end
249
+ end
250
+ end
251
+ nil
252
+ end
253
+
254
+ private
255
+ def orig_values
256
+ @_orig_values ||= {}
257
+ end
258
+
259
+ private
260
+ def clear_change! attribute_name
261
+ orig_values.delete(attribute_name)
262
+ end
263
+
264
+ private
265
+ def ignore_changes &block
266
+ begin
267
+ @_ignore_changes = true
268
+ yield
269
+ ensure
270
+ @_ignore_changes = false
271
+ end
272
+ end
273
+
274
+ private
275
+ def if_tracking_changes &block
276
+ yield unless @_ignore_changes
277
+ end
278
+
279
+ private
280
+ def clear_changes!
281
+ orig_values.clear
282
+ end
283
+
284
+ end
285
+ end
286
+ end
@@ -0,0 +1,153 @@
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/indifferent_hash'
15
+
16
+ module AWS
17
+ module Record
18
+
19
+ class Base
20
+
21
+ def errors
22
+ @errors ||= Errors.new
23
+ end
24
+
25
+ end
26
+
27
+ class Errors < IndifferentHash
28
+
29
+ include Enumerable
30
+
31
+ # Returns the errors for the atttibute in an array.
32
+ #
33
+ # errors.add(:name, 'may not be blank')
34
+ # errors.add(:name, 'must be less than 30 characters')
35
+ # errors[:name]
36
+ # #=> ['may not be blank', 'must be less than 30 characters']
37
+ #
38
+ # @param [String,Symbol] attribute_name The name of the attribute to retnr
39
+ # errors for. You can pass the string or symbol version.
40
+ # @return [Array<String>] Returns the error messages for the given
41
+ # +attribute_name+. If there are no errors on the attribute then
42
+ # an empty array is returned.
43
+ def [] attribute_name
44
+ super(attribute_name) || []
45
+ end
46
+ alias_method :on, :[]
47
+
48
+ # Adds an error message to the named attribute.
49
+ #
50
+ # errors.add(:name, 'may not be blank')
51
+ # errors.on(:name)
52
+ # #=> ['may not be blank']
53
+ #
54
+ # If you want to add a general error message, then pass +:base+
55
+ # for +attribute_name+, or call {#add_to_base}.
56
+ # @param [String,Symbol] attribute_name The name of the attribute
57
+ # that you are adding an error to.
58
+ # @param [String] message ('is invalid') The error message (should
59
+ # not contain the attribute name).
60
+ # @return [String] Returns the message.
61
+ def []= attribute_name, message = 'is invalid'
62
+ if has_key?(attribute_name)
63
+ self[attribute_name] << message
64
+ else
65
+ super(attribute_name, [message])
66
+ end
67
+ self[attribute_name]
68
+ end
69
+ alias_method :add, :[]=
70
+
71
+ # Adds a general error message (not associated with any particular
72
+ # attribute).
73
+ # @param [String] message ('is invalid') The error message (should
74
+ # not contain the attribute name).
75
+ # @return [String] Returns the message.
76
+ def add_to_base message
77
+ add(:base, message)
78
+ end
79
+
80
+ # @return [Integer] Returns the number of error messages.
81
+ def count
82
+ values.flatten.length
83
+ end
84
+ alias_method :size, :count
85
+
86
+ # Yields once for each error message added.
87
+ #
88
+ # An attribute_name may yield more than once if there are more than
89
+ # one errors associated with that attirbute.
90
+ #
91
+ # @yield [attribute_name, error_message]
92
+ # @yieldparam [String] attribute_name The name of the attribute
93
+ # @yieldparam [String] error_message The error message associated the
94
+ # the named attribute.
95
+ def each &block
96
+ super do |attribute_name, error_messages|
97
+ error_messages.each do |error_message|
98
+ yield(attribute_name, error_message)
99
+ end
100
+ end
101
+ end
102
+
103
+ # Returns the errors prefixed by a humanized version of the attribute
104
+ # name.
105
+ #
106
+ # errors.add(:name, 'may not be blank')
107
+ # errors.full_messages
108
+ # #=> ['Name may not be blank']
109
+ #
110
+ # @return [Array of Strings] Returns an array of error messages.
111
+ def full_messages
112
+ messages = []
113
+ each do |attr_name, error_message|
114
+ messages << case attr_name
115
+ when 'base' then error_message.dup
116
+ else "#{attr_name.capitalize.gsub(/_/, ' ')} #{error_message}"
117
+ end
118
+ end
119
+ messages
120
+ end
121
+ alias_method :to_a, :full_messages
122
+
123
+ # Returns a hash of of errors messages. Keys are attribute names
124
+ # and values are arrays of error messages.
125
+ #
126
+ # errors.add(:name, 'may not be blank')
127
+ # errors.to_hash
128
+ # #=> { 'name' => ['may not be blank'] }
129
+ #
130
+ # Please note that the hash values are always arrays, even if there
131
+ # is only one error message for the attribute.
132
+ def to_hash
133
+ hash = {}
134
+ each do |attr_name, message|
135
+ hash[attr_name] ||= []
136
+ hash[attr_name] << message.dup
137
+ end
138
+ hash
139
+ end
140
+
141
+ # Removes all error messages.
142
+ # @return [nil]
143
+ def clear!
144
+ keys.each do |key|
145
+ delete(key)
146
+ end
147
+ nil
148
+ end
149
+
150
+ end
151
+
152
+ end
153
+ end