aws-sdk 1.0.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.
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,653 @@
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/record/validators/acceptance'
15
+ require 'aws/record/validators/block'
16
+ require 'aws/record/validators/confirmation'
17
+ require 'aws/record/validators/count'
18
+ require 'aws/record/validators/exclusion'
19
+ require 'aws/record/validators/format'
20
+ require 'aws/record/validators/inclusion'
21
+ require 'aws/record/validators/length'
22
+ require 'aws/record/validators/numericality'
23
+ require 'aws/record/validators/presence'
24
+
25
+ module AWS
26
+ module Record
27
+
28
+ # Validation methods to be used with subclasses of AWS::Record::Base.
29
+ #
30
+ # = General Usage
31
+ #
32
+ # All standard validation methods follow the same basic usage.
33
+ # Call the validation method followed by one more attribute names
34
+ # and then an optional hash of modifiers.
35
+ #
36
+ # class Book < AWS::Record::Base
37
+ #
38
+ # # ...
39
+ #
40
+ # validates_presence_of :title, :author
41
+ #
42
+ # validates_length_of :summary,
43
+ # :max => 500,
44
+ # :allow_nil => true
45
+ #
46
+ # end
47
+ #
48
+ # = Conditional Validations
49
+ #
50
+ # Sometimes you only want to validate an attribute under certain
51
+ # conditions. To make this simple, all validation methods accept the
52
+ # following 3 options:
53
+ #
54
+ # * +:on+
55
+ # * +:if+
56
+ # * +:unless+
57
+ #
58
+ # You may mix and match all 3 of the above options.
59
+ #
60
+ # === Validate on :create or :update
61
+ #
62
+ # By default validations are run on create and update, but you can
63
+ # specify them to run for only create (initial save) or updates.
64
+ #
65
+ # validates_presence_of :created_at, :on => :create
66
+ #
67
+ # validates_presence_of :updated_at, :on => :update
68
+ #
69
+ # === Validate :if or :unless
70
+ #
71
+ # Sometimes you have more complex requirements to determine if/when a
72
+ # validation should run. +:if+ and +:unless+: both accept either
73
+ # a method name or proc.
74
+ #
75
+ # class Person
76
+ #
77
+ # # ...
78
+ #
79
+ # validates_presence_of :job_title, :if => :employee?
80
+ #
81
+ # validates_presence_of :nickname, :if => lambda {|person|
82
+ # person.is_family? or person.is_friend? }
83
+ #
84
+ # end
85
+ #
86
+ # = Validating Virtual (Non-persisted) Attributes
87
+ #
88
+ # All of the validators can be used with configured attributes, but they
89
+ # can also be used with any attribute that has a setter and a getter.
90
+ #
91
+ # Class Book < AWS::Record::Base
92
+ #
93
+ # attr_accessor :title
94
+ #
95
+ # validates_presence_of :title
96
+ #
97
+ # end
98
+ #
99
+ module Validations
100
+
101
+ def self.extended base
102
+
103
+ base.send(:define_method, :validate) do
104
+ errors.clear!
105
+ self.class.send(:validators).each do |validator|
106
+ validator.validate(self)
107
+ end
108
+ end
109
+
110
+ base.send(:private, :validate)
111
+
112
+ end
113
+
114
+ # This validation method is primariliy intended for ensuring a form
115
+ # checkbox (like an EULA agreement or terms of service acknowledgement)
116
+ # is checked.
117
+ #
118
+ # class User < AWS::Record::Base
119
+ # boolean_attr :terms_of_service
120
+ # validates_acceptance_of :terms_of_service
121
+ # end
122
+ #
123
+ # === Virtual Attributes
124
+ #
125
+ # If you choose to validate the acceptance of a non-existant attribute
126
+ # then a setter and a getter will be added automtically for you.
127
+ #
128
+ # class User < AWS::Record::Base
129
+ # validates_acceptance_of :terms_of_service
130
+ # end
131
+ #
132
+ # user = User.new
133
+ # user.respond_to?(:terms_of_service) #=> true
134
+ # user.respond_to?(:terms_of_service=) #=> true
135
+ #
136
+ # === Accepted Values
137
+ #
138
+ # The default behavior for +validates_acceptance_of+ is to add
139
+ # an error when the value is '1' or +true+. Also note, this validation
140
+ # method defaults +:allow_nil+ to true.
141
+ #
142
+ # * +nil+ implies the field was omitted from the form and therefore
143
+ # should not be validated
144
+ #
145
+ # class User < AWS::Record::Base
146
+ # validates_acceptance_of :terms_of_service
147
+ # end
148
+ #
149
+ # u = User.new
150
+ # u.terms_of_service #=> nil
151
+ # u.valid? #=> true
152
+ #
153
+ # * '1' is the default value for most checkbox form helpers, and #
154
+ # therefore indicates an accepted value.
155
+ #
156
+ # * +true+ is how boolean attributes typecast '1'. This is helpful
157
+ # when you have your checkbox post its value to a +:boolean_attr+.
158
+ #
159
+ # === Multi-Valued Attributes
160
+ #
161
+ # This validator works only with single-valued attributes. If you need
162
+ # to validate that all of the values in a set are true, then use
163
+ # {#validates_inclusion_of}.
164
+ #
165
+ # @note Most validators default :allow_nil to false, this one defualts to true
166
+ # @note This validator should not be used with multi-valued attributes
167
+ #
168
+ # @overload validates_acceptance_of(*attributes, options = {}, &block)
169
+ # @param attributes A list of attribute names to validate.
170
+ # @param [Hash] options
171
+ # @option options [mixed] :accpet Specify an additional accepted value.
172
+ #
173
+ # validates_acceptance_of :agree, :accept => 'yes'
174
+ #
175
+ # @option options [String] :message A custom error message. The defualt
176
+ # +:message+ is "must be accepted".
177
+ # @option options [Boolean] :allow_nil (true) Skip validation if the
178
+ # attribute value is +nil+.
179
+ # @option options [Symbol] :on (:save) When this validation is run.
180
+ # Valid values include:
181
+ # * +:save:+
182
+ # * +:create:+
183
+ # * +:update:+
184
+ # @option options [Symbol,String,Proc] :if Specifies a method or proc
185
+ # to call. The validation will only be run if the return value is
186
+ # of the method/proc is true (e.g. +:if => :name_changed?+ or
187
+ # +:if => lambda{|book| book.in_stock? }+).
188
+ # @option options [Symbol,String,Proc] :unless Specifies a method or
189
+ # proc to call. The validation will *not* be run if the return value
190
+ # is of the method/proc is false.
191
+ def validates_acceptance_of *args
192
+ validators << AcceptanceValidator.new(self, *args)
193
+ end
194
+
195
+ # Intended primarily for validating a form field was entered correctly
196
+ # by requiring it twice:
197
+ #
198
+ # Model:
199
+ # class User < AWS::Record::Base
200
+ # validates_confirmation_of :password, :if => :password_changed?
201
+ # end
202
+ #
203
+ # View:
204
+ # <%= password_field "user", "password" %>
205
+ # <%= password_field "user", "password_confirmation" %>
206
+ #
207
+ # === Confirmation Value Accessors
208
+ #
209
+ # If your model does not have accessors for the confirmation value
210
+ # then they will be automatically added. In the example above
211
+ # the user class would have an +attr_accessor+ for
212
+ # +:password_confirmation+.
213
+ #
214
+ # === Conditional Validation
215
+ #
216
+ # Mostly commonly you only need to validate confirmation of an
217
+ # attribute when it has changed. It is therefore suggested to
218
+ # pass an +:if+ condition reflecting this:
219
+ #
220
+ # validates_confirmation_of :password, :if => :password_changed?
221
+ #
222
+ # === Multi-Valued Attributes
223
+ #
224
+ # This validator works only with single-valued attributes.
225
+ # It should not be used on attributes that have array or set values.
226
+ #
227
+ # @note This validation method does not accept the +:allow_nil+ option.
228
+ #
229
+ # @overload validates_confirmation_of(*attributes, options = {}, &block)
230
+ # @param attributes A list of attribute names to validate.
231
+ # @param [Hash] options
232
+ # @option options [String] :message A custom error message. The defualt
233
+ # +:message+ is "doesn't match confirmation".
234
+ # @option options [Symbol] :on (:save) When this validation is run.
235
+ # Valid values include:
236
+ # * +:save:+
237
+ # * +:create:+
238
+ # * +:update:+
239
+ # @option options [Symbol,String,Proc] :if Specifies a method or proc
240
+ # to call. The validation will only be run if the return value is
241
+ # of the method/proc is true (e.g. +:if => :name_changed?+ or
242
+ # +:if => lambda{|book| book.in_stock? }+).
243
+ # @option options [Symbol,String,Proc] :unless Specifies a method or
244
+ # proc to call. The validation will *not* be run if the return value
245
+ # is of the method/proc is false.
246
+ def validates_confirmation_of *args
247
+ validators << ConfirmationValidator.new(self, *args)
248
+ end
249
+
250
+ # Validates the number of values for a given attribute.
251
+ #
252
+ # === Length vs Count
253
+ #
254
+ # +validates_count_of+ validates the number of attribute values,
255
+ # whereas +validates_length_of: validates the length of each
256
+ # attribute value instead.
257
+ #
258
+ # If you need to ensure each attribute value is a given length see
259
+ # {#validates_length_of} instead.
260
+ #
261
+ # === Examples
262
+ #
263
+ # You can validate there are a certain number of values:
264
+ #
265
+ # validates_count_of :parents, :exactly => 2
266
+ #
267
+ # You can also specify a range:
268
+ #
269
+ # validates_count_of :tags, :within => (2..10)
270
+ #
271
+ # You can also specify min and max value seperately:
272
+ #
273
+ # validates_count_of :tags, :minimum => 2, :maximum => 10
274
+ #
275
+ # === +nil+ Values
276
+ #
277
+ # If you are validating an array or set that contains +nil+ values,
278
+ # the +nil+ values are counted normally as 1 each.
279
+ #
280
+ # If you are validating a non-enuemrable attribute that only
281
+ # contains a single nil or other scalar value, then nil is
282
+ # counted as 0.
283
+ #
284
+ # === Singular Attributes
285
+ #
286
+ # This validator is intended to for validating attributes that have
287
+ # an array or set of values. If used on an attribute that
288
+ # returns a scalar value (like +nil+ or a string), the count will
289
+ # always be 0 (for +nil+) or 1 (for everything else).
290
+ #
291
+ # It is therefore recomended to use +:validates_presence_of+ in
292
+ # place of +:validates_count_of+ when working with single-valued
293
+ # attributes.
294
+ #
295
+ # @overload validates_count_of(*attributes, options = {}, &block)
296
+ # @param attributes A list of attribute names to validate.
297
+ # @param [Hash] options
298
+ # @option options [Integer] :exactly The exact number of values the
299
+ # attribute should have. If this validation option fails the
300
+ # error message specified by +:wrong_number+ will be added.
301
+ # @option options [Range] :within An range of number of values to
302
+ # accept. If the attribute has a number of values outside this range
303
+ # then the +:too_many+ or +:too_few+ error message will be added.
304
+ # @option options [Integer] :minimum The minimum number of values
305
+ # the attribute should have. If it has fewer, the +:too_few+ error
306
+ # message will be added.
307
+ # @option options [Integer] :maximum The maximum number of values
308
+ # the attribute should have. If it has more, the +:too_many+ error
309
+ # message will be added.
310
+ # @option options [String] :too_many An error message added
311
+ # when the attribute has too many values. Defaults to
312
+ # <code>"has too many values (maximum is %{maximum})"</code>
313
+ # @option options [String] :too_few An error message added
314
+ # when the attribute has too few values. Defaults to
315
+ # <code>"has too few values (minimum is %{minimum})"</code>
316
+ # @option options [String] :wrong_number An error message
317
+ # added when the number of attribute values does not match
318
+ # the +:exactly+ option. Defaults to <code>"has the wrong
319
+ # number of values (should have exactly %{exactly}"</code>
320
+ # @option options [Symbol] :on (:save) When this validation is run.
321
+ # Valid values include:
322
+ # * +:save:+
323
+ # * +:create:+
324
+ # * +:update:+
325
+ # @option options [Symbol,String,Proc] :if Specifies a method or proc
326
+ # to call. The validation will only be run if the return value is
327
+ # of the method/proc is true (e.g. +:if => :name_changed?+ or
328
+ # +:if => lambda{|book| book.in_stock? }+).
329
+ # @option options [Symbol,String,Proc] :unless Specifies a method or
330
+ # proc to call. The validation will *not* be run if the return value
331
+ # is of the method/proc is false.
332
+ def validates_count_of *args
333
+ validators << CountValidator.new(self, *args)
334
+ end
335
+
336
+ # Adds a block validator that is called during record validation.
337
+ #
338
+ # class ExampleClass < AWS::Record::Base
339
+ #
340
+ # string_attr :name
341
+ #
342
+ # validates_each(:name) do |record, attribute_name, value|
343
+ # if value == 'John Doe'
344
+ # record.errors.add(attr_name, 'may not be an alias')
345
+ # end
346
+ # end
347
+ #
348
+ # end
349
+ #
350
+ # @overload validates_each(*attributes, options = {}, &block)
351
+ # @param attributes A list of attribute names to validate.
352
+ # @param [Hash] options
353
+ # @option options [Boolean] :allow_nil (false) Skip validation if the
354
+ # attribute value is +nil+.
355
+ # @option options [Symbol] :on (:save) When this validation is run.
356
+ # Valid values include:
357
+ # * +:save:+
358
+ # * +:create:+
359
+ # * +:update:+
360
+ # @option options [Symbol,String,Proc] :if Specifies a method or proc
361
+ # to call. The validation will only be run if the return value is
362
+ # of the method/proc is true (e.g. +:if => :name_changed?+ or
363
+ # +:if => lambda{|book| book.in_stock? }+).
364
+ # @option options [Symbol,String,Proc] :unless Specifies a method or
365
+ # proc to call. The validation will *not* be run if the return value
366
+ # is of the method/proc is false.
367
+ def validates_each *attributes, &block
368
+ unless block_given?
369
+ raise ArgumentError, 'missing required block for validates_each'
370
+ end
371
+ validators << BlockValidator.new(self, *attributes, &block)
372
+ end
373
+
374
+ # Validates that the attribute value is not included in the given
375
+ # enumerable.
376
+ #
377
+ # validates_exlusion_of :username, :in => %w(admin administrator)
378
+ #
379
+ # === Multi-Valued Attributes
380
+ #
381
+ # You may use this with multi-valued attributes the same way you use it
382
+ # with single-valued attributes:
383
+ #
384
+ # class Product < AWS::Record::Base
385
+ #
386
+ # string_attr :tags, :set => true
387
+ #
388
+ # validates_exlusion_of :tags, :in => four_letter_words
389
+ #
390
+ # end
391
+ #
392
+ # @overload validates_exclusion_of(*attributes, options = {}, &block)
393
+ # @param attributes A list of attribute names to validate.
394
+ # @param [Hash] options
395
+ # @option options [required, Enumerable] :in An enumerable object to
396
+ # ensure the value is not in.
397
+ # @option options [String] :message A custom error message. The defualt
398
+ # +:message+ is "is reserved".
399
+ # @option options [Boolean] :allow_nil (false) Skip validation if the
400
+ # attribute value is +nil+.
401
+ # @option options [Symbol] :on (:save) When this validation is run.
402
+ # Valid values include:
403
+ # * +:save:+
404
+ # * +:create:+
405
+ # * +:update:+
406
+ # @option options [Symbol,String,Proc] :if Specifies a method or proc
407
+ # to call. The validation will only be run if the return value is
408
+ # of the method/proc is true (e.g. +:if => :name_changed?+ or
409
+ # +:if => lambda{|book| book.in_stock? }+).
410
+ # @option options [Symbol,String,Proc] :unless Specifies a method or
411
+ # proc to call. The validation will *not* be run if the return value
412
+ # is of the method/proc is false.
413
+ def validates_exclusion_of *args
414
+ validators << ExclusionValidator.new(self, *args)
415
+ end
416
+
417
+ # Validates the attribute's value matches the given regular exression.
418
+ #
419
+ # validates_format_of :year, :with => /^\d{4}$/
420
+ #
421
+ # You can also perform a not-match using +:without+ instead of +:with+.
422
+ #
423
+ # validates_format_of :username, :without => /\d/
424
+ #
425
+ # === Multi-Valued Attributes
426
+ #
427
+ # You may use this with multi-valued attributes the same way you use it
428
+ # with single-valued attributes:
429
+ #
430
+ # class Product < AWS::Record::Base
431
+ #
432
+ # string_attr :tags, :set => true
433
+ #
434
+ # validates_format_of :tags, :with => /^\w{2,10}$/
435
+ #
436
+ # end
437
+ #
438
+ # @overload validates_format_of(*attributes, options = {}, &block)
439
+ # @param attributes A list of attribute names to validate.
440
+ # @param [Hash] options
441
+ # @option options [Regexp] :with If the value matches the given
442
+ # regex, an error will not be added.
443
+ # @option options [Regexp] :without If the value matches the given
444
+ # regex, an error will be added.
445
+ # must match, or an error is added.
446
+ # @option options [String] :message A custom error message. The defualt
447
+ # +:message+ is "is reserved".
448
+ # @option options [Boolean] :allow_nil (false) Skip validation if the
449
+ # attribute value is +nil+.
450
+ # @option options [Symbol] :on (:save) When this validation is run.
451
+ # Valid values include:
452
+ # * +:save:+
453
+ # * +:create:+
454
+ # * +:update:+
455
+ # @option options [Symbol,String,Proc] :if Specifies a method or proc
456
+ # to call. The validation will only be run if the return value is
457
+ # of the method/proc is true (e.g. +:if => :name_changed?+ or
458
+ # +:if => lambda{|book| book.in_stock? }+).
459
+ # @option options [Symbol,String,Proc] :unless Specifies a method or
460
+ # proc to call. The validation will *not* be run if the return value
461
+ # is of the method/proc is false.
462
+ def validates_format_of *args
463
+ validators << FormatValidator.new(self, *args)
464
+ end
465
+
466
+ # Validates that the attribute value is included in the given enumerable
467
+ # object.
468
+ #
469
+ # class MultipleChoiceAnswer < AWS::Record::Base
470
+ # validates_inclusion_of :letter, :in => %w(a b c d e)
471
+ # end
472
+ #
473
+ # === Multi-Valued Attributes
474
+ #
475
+ # You may use this with multi-valued attributes the same way you use it
476
+ # with single-valued attributes.
477
+ #
478
+ # @overload validates_inclusion_of(*attributes, options = {}, &block)
479
+ # @param attributes A list of attribute names to validate.
480
+ # @param [Hash] options
481
+ # @option options [required, Enumerable] :in An enumerable object to
482
+ # check for the value in.
483
+ # @option options [String] :message A custom error message. The defualt
484
+ # +:message+ is "is not included in the list".
485
+ # @option options [Boolean] :allow_nil (false) Skip validation if the
486
+ # attribute value is +nil+.
487
+ # @option options [Symbol] :on (:save) When this validation is run.
488
+ # Valid values include:
489
+ # * +:save:+
490
+ # * +:create:+
491
+ # * +:update:+
492
+ # @option options [Symbol,String,Proc] :if Specifies a method or proc
493
+ # to call. The validation will only be run if the return value is
494
+ # of the method/proc is true (e.g. +:if => :name_changed?+ or
495
+ # +:if => lambda{|book| book.in_stock? }+).
496
+ # @option options [Symbol,String,Proc] :unless Specifies a method or
497
+ # proc to call. The validation will *not* be run if the return value
498
+ # is of the method/proc is false.
499
+ def validates_inclusion_of *attributes
500
+ validators << InclusionValidator.new(self, *attributes)
501
+ end
502
+
503
+ # Validates the attribute values are of a specified length.
504
+ #
505
+ # validates_lenth_of :username, :within => 3..25
506
+ #
507
+ # === Length vs Count
508
+ #
509
+ # +validates_length_of+ validates the length of individual attribute
510
+ # values, whereas +validates_count_of: validates the number of
511
+ # attribute values.
512
+ #
513
+ # If you need to ensure there are certain number of values see
514
+ # {#validates_count_of} instead.
515
+ #
516
+ # @overload validates_length_of(*attributes, options = {}, &block)
517
+ # @param attributes A list of attribute names to validate.
518
+ # @param [Hash] options
519
+ # @option options [Enumerable] :within An enumerable object to
520
+ # ensure the length of the value falls within.
521
+ # @option options [Integer] :exactly The exact length a value must be.
522
+ # If this validation fails the error message specified by
523
+ # +:wrong_length+ will be added.
524
+ # @option options [Range] :within An enumerable object which must
525
+ # include the length of the attribute, or an error will be added.
526
+ # If the attribute has a length outside the range then the
527
+ # +:too_long+ or +:too_short+ error message will be added.
528
+ # @option options [Integer] :minimum The minimum length an attribute
529
+ # value should be. If it is shorter, the +:too_short+ error
530
+ # message will be added.
531
+ # @option options [Integer] :maximum The maximum length an attribute
532
+ # value should be. If it is longer, the +:too_long+ error
533
+ # message will be added.
534
+ # @option options [String] :too_long An error message added
535
+ # when the attribute value is too long. Defaults to
536
+ # <code>"is too long (maximum is %{maximum}
537
+ # characters)"</code>
538
+ # @option options [String] :too_short An error message added
539
+ # when the attribute value is too short. Defaults to
540
+ # <code>"is too short (minimum is %{minimum}
541
+ # characters)"</code>
542
+ # @option options [String] :wrong_length An error message
543
+ # added when the attribute has the incorrect length (as
544
+ # specified by +:exactly+). Defaults to <code>"is the wrong
545
+ # length (should be %{exactly} characters"</code>
546
+ # @option options [Boolean] :allow_nil (false) Skip validation if the
547
+ # attribute value is +nil+.
548
+ # @option options [Symbol] :on (:save) When this validation is run.
549
+ # Valid values include:
550
+ # * +:save:+
551
+ # * +:create:+
552
+ # * +:update:+
553
+ # @option options [Symbol,String,Proc] :if Specifies a method or proc
554
+ # to call. The validation will only be run if the return value is
555
+ # of the method/proc is true (e.g. +:if => :name_changed?+ or
556
+ # +:if => lambda{|book| book.in_stock? }+).
557
+ # @option options [Symbol,String,Proc] :unless Specifies a method or
558
+ # proc to call. The validation will *not* be run if the return value
559
+ # is of the method/proc is false.
560
+ def validates_length_of *args
561
+ validators << LengthValidator.new(self, *args)
562
+ end
563
+
564
+ # Validates the attribute has a numeric value.
565
+ #
566
+ # validates_numericality_of :age, :only_integer => true
567
+ #
568
+ # === Multi-Valued Attributes
569
+ #
570
+ # You can validate multi-valued attributes using this the same way you
571
+ # validate single-valued attributes. Each value will be validated
572
+ # individually.
573
+ #
574
+ # @overload validates_numericality_of(*attributes, options = {}, &block)
575
+ # @param attributes A list of attribute names to validate.
576
+ # @param [Hash] options
577
+ # @option options [Boolean] :only_integer (false) Adds an error
578
+ # when valiating and the value is numeric, but it not a whole number.
579
+ # @option options [Integer] :equal_to When set the value must equal
580
+ # the given value exactly. May not be used with the greater/less
581
+ # options.
582
+ # @option options [Numeric] :greater_than Ensures the attribute
583
+ # is greater than the given number.
584
+ # @option options [Integer] :greater_than_or_equal_to Ensures the
585
+ # attribute is greater than or equal to the given number.
586
+ # @option options [Numeric] :less_than Ensures the attribute is less
587
+ # than the given value.
588
+ # @option options [Integer] :less_than_or_equal_to Ensures the value is
589
+ # less than or equal to the given number.
590
+ # @option options [Numeric] :even If true, the value may only be
591
+ # an even integer. This forces the +:only_integer+ to +true+.
592
+ # @option options [Numeric] :odd If true, the value may only be
593
+ # an odd integer. This forces the +:only_integer+ to +true+.
594
+ # @option options [String] :message A custom error message. The defualt
595
+ # +:message+ is "is not a number".
596
+ # @option options [Boolean] :allow_nil (false) Skip validation if the
597
+ # attribute value is +nil+.
598
+ # @option options [Symbol] :on (:save) When this validation is run.
599
+ # Valid values include:
600
+ # * +:save:+
601
+ # * +:create:+
602
+ # * +:update:+
603
+ # @option options [Symbol,String,Proc] :if Specifies a method or proc
604
+ # to call. The validation will only be run if the return value is
605
+ # of the method/proc is true (e.g. +:if => :name_changed?+ or
606
+ # +:if => lambda{|book| book.in_stock? }+).
607
+ # @option options [Symbol,String,Proc] :unless Specifies a method or
608
+ # proc to call. The validation will *not* be run if the return value
609
+ # is of the method/proc is false.
610
+ def validates_numericality_of *args
611
+ validators << NumericalityValidator.new(self, *args)
612
+ end
613
+
614
+ # Validates the named attributes are not blank. For validation
615
+ # purposes, blank values include:
616
+ #
617
+ # * +nil+
618
+ # * empty string
619
+ # * anything that responds to #empty? with true
620
+ # * anything that responds to #blank? with true
621
+ #
622
+ # @overload validates_presence_of(*attributes, options = {}, &block)
623
+ # @param attributes A list of attribute names to validate.
624
+ # @param [Hash] options
625
+ # @option options [String] :message A custom error message. The defualt
626
+ # +:message+ is "may not be blank".
627
+ # @option options [Symbol] :on (:save) When this validation is run.
628
+ # Valid values include:
629
+ # * +:save:+
630
+ # * +:create:+
631
+ # * +:update:+
632
+ # @option options [Boolean] :allow_nil (false) Skip validation if the
633
+ # attribute value is +nil+.
634
+ # @option options [Symbol,String,Proc] :if Specifies a method or proc
635
+ # to call. The validation will only be run if the return value is
636
+ # of the method/proc is true (e.g. +:if => :name_changed?+ or
637
+ # +:if => lambda{|book| book.in_stock? }+).
638
+ # @option options [Symbol,String,Proc] :unless Specifies a method or
639
+ # proc to call. The validation will *not* be run if the return value
640
+ # is of the method/proc is false.
641
+ def validates_presence_of *args
642
+ validators << PresenceValidator.new(self, *args)
643
+ end
644
+
645
+ # @private
646
+ private
647
+ def validators
648
+ @validators ||= []
649
+ end
650
+
651
+ end
652
+ end
653
+ end