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,237 @@
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
+ # Base class for all validators
18
+ # @private
19
+ class Validator
20
+
21
+ # these should be defined in subclasses
22
+ ACCEPTED_OPTIONS = []
23
+
24
+ def initialize record_class, *attribute_names, &block
25
+
26
+ @options = attribute_names.last.is_a?(Hash) ? attribute_names.pop : {}
27
+
28
+ @attribute_names = attribute_names
29
+
30
+ reject_unknown_options
31
+
32
+ ensure_type([Symbol, Proc], :if, :unless)
33
+ ensure_is([:save, :create, :update], :on)
34
+
35
+ setup(record_class)
36
+
37
+ end
38
+
39
+ # @return [String] Returns the name of the attribute this validator
40
+ # will check on the record during validation.
41
+ attr_reader :attribute_names
42
+
43
+ # @return [Hash] Returns the hash of options for this validator.
44
+ attr_reader :options
45
+
46
+ def validate record
47
+ if
48
+ passes_on_condition?(record) and
49
+ passes_if_condition?(record) and
50
+ passes_unless_condition?(record)
51
+ then
52
+ validate_attributes(record)
53
+ end
54
+ end
55
+
56
+ # @private
57
+ protected
58
+ def setup klass
59
+ end
60
+
61
+ # @private
62
+ protected
63
+ def each_value value, &block
64
+ case value
65
+ when Array, Set then value.each{|v| yield(v) }
66
+ else yield(value)
67
+ end
68
+ end
69
+
70
+ # @private
71
+ protected
72
+ def add_accessors klass, *accessors
73
+
74
+ methods = klass.instance_methods.collect{|m| m.to_s }
75
+
76
+ accessors.each do |attr|
77
+
78
+ setter = "#{attr}="
79
+ getter = attr.to_s
80
+
81
+ unless methods.include?(getter)
82
+ klass.send(:attr_reader, attr)
83
+ klass.send(:public, getter)
84
+ end
85
+
86
+ unless methods.include?(setter)
87
+ klass.send(:attr_writer, attr)
88
+ klass.send(:public, setter)
89
+ end
90
+
91
+ end
92
+
93
+ end
94
+
95
+ # @private
96
+ protected
97
+ def validate_attributes record
98
+ attribute_names.each do |attribute_name|
99
+ value = read_attribute_for_validation(record, attribute_name)
100
+ next if value.nil? and options[:allow_nil]
101
+ validate_attribute(record, attribute_name, value)
102
+ end
103
+ end
104
+
105
+ # @private
106
+ protected
107
+ def read_attribute_for_validation(record, attribute_name)
108
+ record.send(attribute_name)
109
+ end
110
+
111
+ # @private
112
+ def reject_unknown_options
113
+ invalid_keys = options.keys - self.class::ACCEPTED_OPTIONS
114
+ if invalid_keys.length == 1
115
+ raise ArgumentError, "unknown option :#{invalid_keys.first}"
116
+ elsif invalid_keys.length > 1
117
+ bad = invalid_keys.collect{|k| ":#{k}" }.join(', ')
118
+ raise ArgumentError, "unknown options #{bad}"
119
+ end
120
+ end
121
+
122
+ # @private
123
+ private
124
+ def passes_if_condition? record
125
+ case options[:if]
126
+ when nil then true
127
+ when Proc then options[:if].call(record)
128
+ when String, Symbol then record.send(options[:if])
129
+ else
130
+ raise 'invalid :if option, must be nil, a method name or a Proc'
131
+ end
132
+ end
133
+
134
+ # @private
135
+ private
136
+ def passes_unless_condition? record
137
+ case options[:unless]
138
+ when nil then true
139
+ when Proc then !options[:unless].call(record)
140
+ when String, Symbol then !record.send(options[:unless])
141
+ else
142
+ raise 'invalid :unless option, must be nil, a method name or a Proc'
143
+ end
144
+ end
145
+
146
+ # @private
147
+ private
148
+ def passes_on_condition? record
149
+ case options[:on]
150
+ when nil then true
151
+ when :save then true
152
+ when :create then record.new_record?
153
+ when :update then record.persisted?
154
+ else
155
+ raise 'invalid :on option, must be nil, :save, :create or :update'
156
+ end
157
+ end
158
+
159
+ # @private
160
+ protected
161
+ def ensure_type type_or_types, *keys
162
+
163
+ types = Array(type_or_types)
164
+
165
+ keys.each do |key|
166
+
167
+ next unless options.has_key?(key)
168
+ next unless types.none?{|type| options[key].is_a?(type) }
169
+
170
+ expected = types.map{|type| type.to_s }
171
+ if expected.count == 1
172
+ raise ArgumentError, "expected option :#{key} to be a #{expected}"
173
+ else
174
+ msg = "expected :#{key} to be one of #{expected.join(', ')}"
175
+ raise ArgumentError, msg
176
+ end
177
+
178
+ end
179
+ end
180
+
181
+ def ensure_is value_or_values, *keys
182
+ values = Array(value_or_values)
183
+ keys.each do |key|
184
+ next unless options.has_key?(key)
185
+ unless values.include?(options[key])
186
+ valid = values.map{|v| v.is_a?(Symbol) ? ":#{v}" : v.to_s }.join(', ')
187
+ raise ArgumentError, "expected :#{key} to be one of #{valid}"
188
+ end
189
+ end
190
+ end
191
+
192
+ # @private
193
+ protected
194
+ def ensure_exclusive *key_groups
195
+ key_groups.each do |key_group|
196
+ others = key_groups - [key_group]
197
+ Array(key_group).each do |key|
198
+ next unless options.has_key?(key)
199
+ conflicts = others.flatten.select{|other| options.has_key?(other) }
200
+ unless conflicts.empty?
201
+ msg = ":#{key} may not be used with :#{conflicts.first}"
202
+ raise ArgumentError, msg
203
+ end
204
+ end
205
+ end
206
+ end
207
+
208
+ # @private
209
+ protected
210
+ def ensure_present *keys
211
+ keys.each do |k|
212
+ unless options.has_key?(k)
213
+ raise ArgumentError, "missing required option :#{k}"
214
+ end
215
+ end
216
+ end
217
+
218
+ # @private
219
+ protected
220
+ def ensure_at_least_one *keys
221
+ found = keys.select{|k| options.has_key?(k) }
222
+ unless found.count >= 1
223
+ opts = keys.collect{|k| ":#{k}" }.join(', ')
224
+ msg = "must provide at least one of the following options: #{opts}"
225
+ raise ArgumentError, msg
226
+ end
227
+ end
228
+
229
+ protected
230
+ def set_default key, value
231
+ options[key] = value unless options.has_key?(key)
232
+ end
233
+
234
+ end
235
+
236
+ end
237
+ end
@@ -0,0 +1,51 @@
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/validator'
15
+
16
+ module AWS
17
+ module Record
18
+
19
+ # @private
20
+ class AcceptanceValidator < Validator
21
+
22
+ ACCEPTED_OPTIONS = [:accept, :message, :allow_nil, :on, :if, :unless]
23
+
24
+ def setup record_class
25
+ set_default(:allow_nil, true)
26
+ add_accessors(record_class, *attribute_names)
27
+ end
28
+
29
+ def validate_attribute record, attribute_name, value
30
+
31
+ accepted = case value
32
+ when '1' then true
33
+ when true then true
34
+ else
35
+ options.has_key?(:accept) ?
36
+ value == options[:accept] :
37
+ false
38
+ end
39
+
40
+ record.errors.add(attribute_name, message) unless accepted
41
+
42
+ end
43
+
44
+ def message
45
+ options[:message] || 'must be accepted'
46
+ end
47
+
48
+ end
49
+
50
+ end
51
+ end
@@ -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
+ require 'aws/record/validator'
15
+
16
+ module AWS
17
+ module Record
18
+
19
+ # @private
20
+ class BlockValidator < Validator
21
+
22
+ ACCEPTED_OPTIONS = [:allow_nil, :on, :if, :unless]
23
+
24
+ def initialize *args, &block
25
+ @block = block
26
+ super(*args)
27
+ end
28
+
29
+ attr_reader :block
30
+
31
+ def validate_attribute record, attribute_name, value
32
+ block.call(record, attribute_name, value)
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,43 @@
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/validator'
15
+
16
+ module AWS
17
+ module Record
18
+
19
+ # @private
20
+ class ConfirmationValidator < Validator
21
+
22
+ ACCEPTED_OPTIONS = [:message, :on, :if, :unless]
23
+
24
+ def setup record_class
25
+ accessors = attribute_names.collect{|m| "#{m}_confirmation" }
26
+ add_accessors(record_class, *accessors)
27
+ end
28
+
29
+ def validate_attribute record, attribute_name, value
30
+ confirmation_value = record.send("#{attribute_name}_confirmation")
31
+ unless value == confirmation_value
32
+ record.errors.add(attribute_name, message)
33
+ end
34
+ end
35
+
36
+ def message
37
+ options[:message] || "doesn't match confirmation"
38
+ end
39
+
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,108 @@
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/validator'
15
+
16
+ module AWS
17
+ module Record
18
+
19
+ # @private
20
+ class CountValidator < Validator
21
+
22
+ ACCEPTED_OPTIONS = [
23
+ :exactly, :within, :minimum, :maximum,
24
+ :too_many, :too_few, :wrong_number,
25
+ :on, :if, :unless,
26
+ ]
27
+
28
+ def setup record_class
29
+ ensure_at_least_one(:within, :exactly, :minimum, :maximum)
30
+ ensure_exclusive(:within, :exactly, [:minimum, :maximum])
31
+ ensure_type(Range, :within)
32
+ ensure_type(Integer, :exactly, :minimum, :maximum)
33
+ ensure_type(String, :too_many, :too_few, :wrong_number)
34
+ end
35
+
36
+ def validate_attribute record, attribute_name, value
37
+
38
+ count = case value
39
+ when nil then 0
40
+ when String then 1
41
+ when Enumerable then value.count
42
+ else 1
43
+ end
44
+
45
+ if exact = options[:exactly]
46
+ unless count == exact
47
+ record.errors.add(attribute_name, wrong_number(exact, count))
48
+ end
49
+ end
50
+
51
+ if within = options[:within]
52
+ if count < within.first
53
+ record.errors.add(attribute_name, too_few(within.first, count))
54
+ end
55
+ if count > within.last
56
+ record.errors.add(attribute_name, too_many(within.last, count))
57
+ end
58
+ end
59
+
60
+ if min = options[:minimum]
61
+ if count < min
62
+ record.errors.add(attribute_name, too_few(min, count))
63
+ end
64
+ end
65
+
66
+ if max = options[:maximum]
67
+ if count > max
68
+ record.errors.add(attribute_name, too_many(max, count))
69
+ end
70
+ end
71
+
72
+ end
73
+
74
+ # @private
75
+ protected
76
+ def wrong_number exactly, got
77
+ msg = options[:wrong_number] ||
78
+ "has the wrong number of values (should have %{exactly})"
79
+ interpolate(msg, :exactly => exactly, :count => got)
80
+ end
81
+
82
+ # @private
83
+ protected
84
+ def too_few min, got
85
+ msg = options[:too_few] || "has too few values (minimum is %{minimum})"
86
+ interpolate(msg, :minimum => min, :count => got)
87
+ end
88
+
89
+ # @private
90
+ protected
91
+ def too_many max, got
92
+ msg = options[:too_many] || "has too many values (maximum is %{maximum})"
93
+ interpolate(msg, :maximum => max, :count => got)
94
+ end
95
+
96
+ protected
97
+ def interpolate message_with_placeholders, values
98
+ msg = message_with_placeholders.dup
99
+ values.each_pair do |key,value|
100
+ msg.gsub!(/%\{#{key}\}/, value.to_s)
101
+ end
102
+ msg
103
+ end
104
+
105
+ end
106
+
107
+ end
108
+ end