cloud-templates 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (225) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +4 -0
  3. data/.rubocop.yml +1 -0
  4. data/Rakefile +1 -1
  5. data/bin/cloud-templates-runner.rb +5 -0
  6. data/cloud-templates.gemspec +9 -4
  7. data/examples/lib/user_directory.rb +0 -10
  8. data/examples/lib/user_directory/artifacts.rb +8 -0
  9. data/examples/lib/user_directory/artifacts/catalogized.rb +8 -7
  10. data/examples/lib/user_directory/artifacts/group.rb +27 -30
  11. data/examples/lib/user_directory/artifacts/ided.rb +8 -7
  12. data/examples/lib/user_directory/artifacts/organization.rb +10 -11
  13. data/examples/lib/user_directory/artifacts/pathed.rb +17 -17
  14. data/examples/lib/user_directory/artifacts/person.rb +20 -15
  15. data/examples/lib/user_directory/artifacts/team.rb +26 -21
  16. data/examples/lib/user_directory/artifacts/unit.rb +15 -17
  17. data/examples/lib/user_directory/artifacts/user.rb +26 -23
  18. data/examples/lib/user_directory/render.rb +8 -0
  19. data/examples/lib/user_directory/render/etc.rb +2 -0
  20. data/examples/lib/user_directory/render/etc/artifact_view.rb +2 -2
  21. data/examples/lib/user_directory/render/etc/composite_view.rb +1 -2
  22. data/examples/lib/user_directory/render/etc/group_view.rb +3 -3
  23. data/examples/lib/user_directory/render/etc/person_view.rb +2 -2
  24. data/examples/lib/user_directory/render/etc/registry.rb +4 -6
  25. data/examples/lib/user_directory/render/etc/user_view.rb +2 -3
  26. data/examples/lib/user_directory/render/ldap.rb +2 -0
  27. data/examples/lib/user_directory/render/ldap/artifact_view.rb +3 -4
  28. data/examples/lib/user_directory/render/ldap/composite_view.rb +3 -4
  29. data/examples/lib/user_directory/render/ldap/group_view.rb +3 -4
  30. data/examples/lib/user_directory/render/ldap/organization_view.rb +3 -4
  31. data/examples/lib/user_directory/render/ldap/person_view.rb +3 -4
  32. data/examples/lib/user_directory/render/ldap/registry.rb +3 -4
  33. data/examples/lib/user_directory/render/ldap/unit_view.rb +3 -4
  34. data/examples/lib/user_directory/render/ldap/user_view.rb +3 -4
  35. data/examples/lib/user_directory/utils.rb +1 -1
  36. data/examples/spec/user_directory_spec.rb +24 -25
  37. data/lib/aws/templates.rb +0 -8
  38. data/lib/aws/templates/artifact.rb +20 -12
  39. data/lib/aws/templates/cli.rb +14 -0
  40. data/lib/aws/templates/cli/formatter.rb +21 -0
  41. data/lib/aws/templates/cli/formatter/json.rb +20 -0
  42. data/lib/aws/templates/cli/interface.rb +92 -0
  43. data/lib/aws/templates/composite.rb +6 -9
  44. data/lib/aws/templates/exception.rb +8 -0
  45. data/lib/aws/templates/exception/nested_parameter_exception.rb +19 -0
  46. data/lib/aws/templates/exception/option_error.rb +14 -0
  47. data/lib/aws/templates/exception/option_scalar_on_the_way.rb +26 -0
  48. data/lib/aws/templates/exception/option_should_be_recursive.rb +20 -0
  49. data/lib/aws/templates/exception/option_value_deleted.rb +22 -0
  50. data/lib/aws/templates/exception/parameter_already_exist.rb +26 -0
  51. data/lib/aws/templates/exception/parameter_exception.rb +26 -0
  52. data/lib/aws/templates/exception/parameter_getter_is_not_defined.rb +23 -0
  53. data/lib/aws/templates/exception/parameter_method_name_conflict.rb +26 -0
  54. data/lib/aws/templates/exception/parameter_specification_is_invalid.rb +30 -0
  55. data/lib/aws/templates/exception/parameter_value_invalid.rb +37 -0
  56. data/lib/aws/templates/exception/parametrized_dsl_error.rb +14 -0
  57. data/lib/aws/templates/exception/view_not_found.rb +26 -0
  58. data/lib/aws/templates/help.rb +43 -0
  59. data/lib/aws/templates/help/aggregate.rb +74 -0
  60. data/lib/aws/templates/help/dsl.rb +30 -0
  61. data/lib/aws/templates/help/provider.rb +15 -0
  62. data/lib/aws/templates/help/rdoc.rb +105 -0
  63. data/lib/aws/templates/help/rdoc/artifact.rb +50 -0
  64. data/lib/aws/templates/help/rdoc/composite.rb +20 -0
  65. data/lib/aws/templates/help/rdoc/contextualized.rb +12 -0
  66. data/lib/aws/templates/help/rdoc/contextualized/filter.rb +19 -0
  67. data/lib/aws/templates/help/rdoc/contextualized/filters.rb +14 -0
  68. data/lib/aws/templates/help/rdoc/contextualized/filters/add.rb +21 -0
  69. data/lib/aws/templates/help/rdoc/contextualized/filters/chain.rb +33 -0
  70. data/lib/aws/templates/help/rdoc/contextualized/filters/copy.rb +23 -0
  71. data/lib/aws/templates/help/rdoc/contextualized/filters/identity.rb +23 -0
  72. data/lib/aws/templates/help/rdoc/contextualized/filters/override.rb +29 -0
  73. data/lib/aws/templates/help/rdoc/contextualized/filters/proxy.rb +28 -0
  74. data/lib/aws/templates/help/rdoc/contextualized/filters/recursive_schema_filter.rb +28 -0
  75. data/lib/aws/templates/help/rdoc/contextualized/filters/remove.rb +21 -0
  76. data/lib/aws/templates/help/rdoc/contextualized/filters/scoped.rb +29 -0
  77. data/lib/aws/templates/help/rdoc/contextualized/provider.rb +28 -0
  78. data/lib/aws/templates/help/rdoc/default.rb +12 -0
  79. data/lib/aws/templates/help/rdoc/default/definition.rb +89 -0
  80. data/lib/aws/templates/help/rdoc/default/provider.rb +25 -0
  81. data/lib/aws/templates/help/rdoc/dsl.rb +20 -0
  82. data/lib/aws/templates/help/rdoc/inheritable.rb +48 -0
  83. data/lib/aws/templates/help/rdoc/parametrized.rb +12 -0
  84. data/lib/aws/templates/help/rdoc/parametrized/constraint.rb +31 -0
  85. data/lib/aws/templates/help/rdoc/parametrized/constraints.rb +14 -0
  86. data/lib/aws/templates/help/rdoc/parametrized/constraints/all_of.rb +39 -0
  87. data/lib/aws/templates/help/rdoc/parametrized/constraints/condition.rb +44 -0
  88. data/lib/aws/templates/help/rdoc/parametrized/constraints/depends_on_value.rb +41 -0
  89. data/lib/aws/templates/help/rdoc/parametrized/constraints/enum.rb +33 -0
  90. data/lib/aws/templates/help/rdoc/parametrized/constraints/matches.rb +27 -0
  91. data/lib/aws/templates/help/rdoc/parametrized/constraints/not_nil.rb +27 -0
  92. data/lib/aws/templates/help/rdoc/parametrized/constraints/requires.rb +33 -0
  93. data/lib/aws/templates/help/rdoc/parametrized/constraints/satisfies_condition.rb +28 -0
  94. data/lib/aws/templates/help/rdoc/parametrized/getter.rb +17 -0
  95. data/lib/aws/templates/help/rdoc/parametrized/getters.rb +14 -0
  96. data/lib/aws/templates/help/rdoc/parametrized/nested.rb +32 -0
  97. data/lib/aws/templates/help/rdoc/parametrized/parameter.rb +55 -0
  98. data/lib/aws/templates/help/rdoc/parametrized/provider.rb +32 -0
  99. data/lib/aws/templates/help/rdoc/parametrized/transformation.rb +17 -0
  100. data/lib/aws/templates/help/rdoc/parametrized/transformations.rb +14 -0
  101. data/lib/aws/templates/help/rdoc/parametrized/transformations/as_boolean.rb +25 -0
  102. data/lib/aws/templates/help/rdoc/parametrized/transformations/as_chain.rb +34 -0
  103. data/lib/aws/templates/help/rdoc/parametrized/transformations/as_hash.rb +40 -0
  104. data/lib/aws/templates/help/rdoc/parametrized/transformations/as_integer.rb +26 -0
  105. data/lib/aws/templates/help/rdoc/parametrized/transformations/as_list.rb +32 -0
  106. data/lib/aws/templates/help/rdoc/parametrized/transformations/as_module.rb +25 -0
  107. data/lib/aws/templates/help/rdoc/parametrized/transformations/as_object.rb +32 -0
  108. data/lib/aws/templates/help/rdoc/parametrized/transformations/as_rendered.rb +28 -0
  109. data/lib/aws/templates/help/rdoc/parametrized/transformations/as_string.rb +25 -0
  110. data/lib/aws/templates/help/rdoc/provider.rb +20 -0
  111. data/lib/aws/templates/help/rdoc/routing.rb +104 -0
  112. data/lib/aws/templates/help/rdoc/texting.rb +42 -0
  113. data/lib/aws/templates/processor.rb +53 -0
  114. data/lib/aws/templates/processor/handler.rb +104 -0
  115. data/lib/aws/templates/processor/registry.rb +74 -0
  116. data/lib/aws/templates/processor/routing.rb +27 -0
  117. data/lib/aws/templates/render.rb +27 -25
  118. data/lib/aws/templates/render/basic_view.rb +47 -0
  119. data/lib/aws/templates/render/utils.rb +14 -0
  120. data/lib/aws/templates/render/utils/base_type_views.rb +12 -19
  121. data/lib/aws/templates/render/utils/inspect.rb +2 -4
  122. data/lib/aws/templates/render/utils/stringify.rb +2 -5
  123. data/lib/aws/templates/render/view.rb +3 -100
  124. data/lib/aws/templates/utils.rb +39 -15
  125. data/lib/aws/templates/utils/artifact_storage.rb +2 -3
  126. data/lib/aws/templates/utils/{named.rb → as_named.rb} +2 -3
  127. data/lib/aws/templates/utils/autoload.rb +224 -0
  128. data/lib/aws/templates/utils/contextualized.rb +8 -17
  129. data/lib/aws/templates/utils/contextualized/filter.rb +62 -0
  130. data/lib/aws/templates/utils/contextualized/filter/add.rb +67 -0
  131. data/lib/aws/templates/utils/contextualized/filter/chain.rb +56 -0
  132. data/lib/aws/templates/utils/contextualized/filter/copy.rb +38 -0
  133. data/lib/aws/templates/utils/contextualized/filter/dsl.rb +50 -0
  134. data/lib/aws/templates/utils/contextualized/filter/identity.rb +45 -0
  135. data/lib/aws/templates/utils/contextualized/filter/override.rb +62 -0
  136. data/lib/aws/templates/utils/contextualized/filter/proxy.rb +29 -0
  137. data/lib/aws/templates/utils/contextualized/filter/recursive_schema_filter.rb +46 -0
  138. data/lib/aws/templates/utils/contextualized/filter/remove.rb +66 -0
  139. data/lib/aws/templates/utils/contextualized/filter/scoped.rb +42 -0
  140. data/lib/aws/templates/utils/contextualized/refinements.rb +51 -0
  141. data/lib/aws/templates/utils/default.rb +258 -50
  142. data/lib/aws/templates/utils/dependency.rb +7 -6
  143. data/lib/aws/templates/utils/dependency/refinements.rb +80 -0
  144. data/lib/aws/templates/utils/dependent.rb +6 -3
  145. data/lib/aws/templates/utils/guarded.rb +30 -0
  146. data/lib/aws/templates/utils/inheritable.rb +21 -16
  147. data/lib/aws/templates/utils/inspectable.rb +4 -4
  148. data/lib/aws/templates/utils/late_bound.rb +2 -2
  149. data/lib/aws/templates/utils/memoized.rb +2 -0
  150. data/lib/aws/templates/utils/options.rb +49 -35
  151. data/lib/aws/templates/utils/parametrized.rb +32 -25
  152. data/lib/aws/templates/utils/parametrized/constraint.rb +95 -0
  153. data/lib/aws/templates/utils/parametrized/constraint/all_of.rb +51 -0
  154. data/lib/aws/templates/utils/parametrized/constraint/condition.rb +93 -0
  155. data/lib/aws/templates/utils/parametrized/constraint/depends_on_value.rb +60 -0
  156. data/lib/aws/templates/utils/parametrized/constraint/dsl.rb +86 -0
  157. data/lib/aws/templates/utils/parametrized/constraint/enum.rb +45 -0
  158. data/lib/aws/templates/utils/parametrized/constraint/matches.rb +44 -0
  159. data/lib/aws/templates/utils/parametrized/constraint/not_nil.rb +38 -0
  160. data/lib/aws/templates/utils/parametrized/constraint/requires.rb +54 -0
  161. data/lib/aws/templates/utils/parametrized/constraint/satisfies_condition.rb +55 -0
  162. data/lib/aws/templates/utils/parametrized/getter.rb +67 -0
  163. data/lib/aws/templates/utils/parametrized/getter/as_instance_variable.rb +41 -0
  164. data/lib/aws/templates/utils/parametrized/getter/as_is.rb +37 -0
  165. data/lib/aws/templates/utils/parametrized/getter/dsl.rb +62 -0
  166. data/lib/aws/templates/utils/parametrized/getter/one_of.rb +48 -0
  167. data/lib/aws/templates/utils/parametrized/getter/path.rb +53 -0
  168. data/lib/aws/templates/utils/parametrized/getter/value.rb +50 -0
  169. data/lib/aws/templates/utils/parametrized/nested.rb +9 -16
  170. data/lib/aws/templates/utils/parametrized/transformation.rb +74 -0
  171. data/lib/aws/templates/utils/parametrized/transformation/as_boolean.rb +42 -0
  172. data/lib/aws/templates/utils/parametrized/transformation/as_chain.rb +57 -0
  173. data/lib/aws/templates/utils/parametrized/transformation/as_hash.rb +89 -0
  174. data/lib/aws/templates/utils/parametrized/transformation/as_integer.rb +39 -0
  175. data/lib/aws/templates/utils/parametrized/transformation/as_list.rb +97 -0
  176. data/lib/aws/templates/utils/parametrized/transformation/as_module.rb +42 -0
  177. data/lib/aws/templates/utils/parametrized/transformation/as_object.rb +76 -0
  178. data/lib/aws/templates/utils/parametrized/transformation/as_rendered.rb +100 -0
  179. data/lib/aws/templates/utils/parametrized/transformation/as_string.rb +39 -0
  180. data/lib/aws/templates/utils/parametrized/transformation/dsl.rb +94 -0
  181. data/lib/aws/templates/utils/recursive.rb +22 -0
  182. data/spec/aws/templates/artifact_spec.rb +10 -10
  183. data/spec/aws/templates/cli_spec.rb +83 -0
  184. data/spec/aws/templates/composite_spec.rb +6 -4
  185. data/spec/aws/templates/help/rdoc/artifact_spec.rb +94 -0
  186. data/spec/aws/templates/help/rdoc/composite_spec.rb +53 -0
  187. data/spec/aws/templates/help/rdoc/parametrized/constraints/all_of_spec.rb +17 -0
  188. data/spec/aws/templates/help/rdoc/parametrized/constraints/depends_on_value_spec.rb +21 -0
  189. data/spec/aws/templates/help/rdoc/parametrized/constraints/enum_spec.rb +17 -0
  190. data/spec/aws/templates/help/rdoc/parametrized/constraints/matches_spec.rb +17 -0
  191. data/spec/aws/templates/help/rdoc/parametrized/constraints/satisfies_condition_spec.rb +17 -0
  192. data/spec/aws/templates/help/rdoc/parametrized/transformations/as_boolean_spec.rb +17 -0
  193. data/spec/aws/templates/help/rdoc/parametrized/transformations/as_chain_spec.rb +17 -0
  194. data/spec/aws/templates/help/rdoc/parametrized/transformations/as_hash_spec.rb +72 -0
  195. data/spec/aws/templates/help/rdoc/parametrized/transformations/as_integer_spec.rb +17 -0
  196. data/spec/aws/templates/help/rdoc/parametrized/transformations/as_list_spec.rb +39 -0
  197. data/spec/aws/templates/help/rdoc/parametrized/transformations/as_module_spec.rb +17 -0
  198. data/spec/aws/templates/help/rdoc/parametrized/transformations/as_object_spec.rb +38 -0
  199. data/spec/aws/templates/help/rdoc/parametrized/transformations/as_rendered_spec.rb +17 -0
  200. data/spec/aws/templates/render/utils/base_type_views_spec.rb +9 -11
  201. data/spec/aws/templates/render_spec.rb +1 -1
  202. data/spec/aws/templates/utils/as_named_spec.rb +0 -2
  203. data/spec/aws/templates/utils/autoload_spec.rb +14 -0
  204. data/spec/aws/templates/utils/contextualized_spec.rb +5 -5
  205. data/spec/aws/templates/utils/late_bound_spec.rb +2 -2
  206. data/spec/aws/templates/utils/parametrized/constraint_spec.rb +8 -10
  207. data/spec/aws/templates/utils/parametrized/getters_spec.rb +6 -7
  208. data/spec/aws/templates/utils/parametrized/transformation_spec.rb +3 -6
  209. data/spec/aws/templates/utils/parametrized_spec.rb +9 -10
  210. data/spec/aws/templates/utils_spec.rb +18 -0
  211. data/spec/test_empty/stuff.rb +4 -0
  212. data/spec/test_empty/stuff/here/test.rb +9 -0
  213. metadata +235 -19
  214. data/lib/aws/templates/exceptions.rb +0 -221
  215. data/lib/aws/templates/render/registry.rb +0 -60
  216. data/lib/aws/templates/utils/contextualized/filters.rb +0 -437
  217. data/lib/aws/templates/utils/contextualized/hash.rb +0 -13
  218. data/lib/aws/templates/utils/contextualized/nil.rb +0 -13
  219. data/lib/aws/templates/utils/contextualized/proc.rb +0 -13
  220. data/lib/aws/templates/utils/dependency/enumerable.rb +0 -13
  221. data/lib/aws/templates/utils/dependency/object.rb +0 -46
  222. data/lib/aws/templates/utils/parametrized/constraints.rb +0 -423
  223. data/lib/aws/templates/utils/parametrized/getters.rb +0 -293
  224. data/lib/aws/templates/utils/parametrized/guarded.rb +0 -32
  225. data/lib/aws/templates/utils/parametrized/transformations.rb +0 -652
@@ -1,9 +1,4 @@
1
- require 'aws/templates/exceptions'
2
- require 'aws/templates/utils/parametrized/guarded'
3
- require 'aws/templates/utils/inheritable'
4
- require 'aws/templates/utils/dependent'
5
- require 'aws/templates/utils/inspectable'
6
- require 'set'
1
+ require 'aws/templates/utils'
7
2
 
8
3
  module Aws
9
4
  module Templates
@@ -16,10 +11,13 @@ module Aws
16
11
  # value extraction, constraints checking and transformation. Essentially,
17
12
  # it's domain-specific extended implementation of attr_reader.
18
13
  module Parametrized
19
- include Guarded
20
- include Dependent
21
- include Inheritable
22
- include Inspectable
14
+ include Utils::Guarded
15
+ include Utils::Dependent
16
+ include Utils::Inheritable
17
+ include Utils::Inspectable
18
+ include Getter::Dsl
19
+ include Constraint::Dsl
20
+ include Transformation::Dsl
23
21
 
24
22
  ##
25
23
  # Parameter object
@@ -97,7 +95,12 @@ module Aws
97
95
  private
98
96
 
99
97
  def extract_value(instance)
100
- raise ParameterGetterIsNotDefined.new(self) unless getter(instance)
98
+ unless getter(instance)
99
+ raise(
100
+ Templates::Exception::ParameterGetterIsNotDefined.new(self)
101
+ )
102
+ end
103
+
101
104
  execute_getter(instance, getter(instance))
102
105
  end
103
106
 
@@ -111,7 +114,7 @@ module Aws
111
114
  end
112
115
  end
113
116
 
114
- ALLOWED_SPECIFICATION_ENTRIES = Set.new %i[description getter transform constraint]
117
+ ALLOWED_SPECIFICATION_ENTRIES = ::Set.new %i[description getter transform constraint]
115
118
 
116
119
  def set_specification(enclosing_class, specification) # :nodoc:
117
120
  @klass = enclosing_class
@@ -126,7 +129,7 @@ module Aws
126
129
  end
127
130
 
128
131
  def raise_wrong_options(wrong_options)
129
- raise ParameterSpecificationIsInvalid.new(self, wrong_options)
132
+ raise Templates::Exception::ParameterSpecificationIsInvalid.new(self, wrong_options)
130
133
  end
131
134
 
132
135
  def process_specification(spec)
@@ -170,11 +173,15 @@ module Aws
170
173
  end
171
174
 
172
175
  instance_scope do
176
+ def guarded_get(instance, parameter_object)
177
+ guarded_for(instance, parameter_object) { parameter_object.get(self) }
178
+ end
179
+
173
180
  ##
174
181
  # Lazy initializer
175
182
  def dependencies
176
183
  if @dependencies.nil?
177
- @dependencies = Set.new
184
+ @dependencies = ::Set.new
178
185
  depends_on(parameter_names.map { |name| send(name) })
179
186
  end
180
187
 
@@ -225,11 +232,9 @@ module Aws
225
232
  # The list includes both the class parameters and all ancestor
226
233
  # parameters.
227
234
  def list_all_parameter_names
228
- ancestors
229
- .select { |mod| mod.include?(Parametrized) }
230
- .inject(Set.new) do |parameter_collection, mod|
231
- parameter_collection.merge(mod.parameters.keys)
232
- end
235
+ ancestors_with(Parametrized).inject(::Set.new) do |parameter_collection, mod|
236
+ parameter_collection.merge(mod.parameters.keys)
237
+ end
233
238
  end
234
239
 
235
240
  ##
@@ -248,10 +253,8 @@ module Aws
248
253
  # inheritance hierarchy.
249
254
  # * +parameter_alias+ - parameter name
250
255
  def get_parameter(parameter_alias)
251
- ancestor =
252
- ancestors
253
- .select { |mod| mod.include?(Parametrized) }
254
- .find { |mod| mod.parameters.key?(parameter_alias) }
256
+ ancestor = ancestors_with(Parametrized)
257
+ .find { |mod| mod.parameters.key?(parameter_alias) }
255
258
 
256
259
  ancestor.parameters[parameter_alias] if ancestor
257
260
  end
@@ -277,9 +280,13 @@ module Aws
277
280
  def raise_already_exists(name)
278
281
  parameter_object = get_parameter(name)
279
282
 
280
- raise(ParameterAlreadyExist.new(parameter_object)) if parameter_object
283
+ if parameter_object
284
+ raise(
285
+ Templates::Exception::ParameterAlreadyExist.new(parameter_object)
286
+ )
287
+ end
281
288
 
282
- raise ParameterMethodNameConflict.new(instance_method(name))
289
+ raise Aws::Templates::Exception::ParameterMethodNameConflict.new(instance_method(name))
283
290
  end
284
291
  end
285
292
  end
@@ -0,0 +1,95 @@
1
+ require 'aws/templates/utils'
2
+
3
+ module Aws
4
+ module Templates
5
+ module Utils
6
+ module Parametrized
7
+ ##
8
+ # Constraint functor class
9
+ #
10
+ # A constraint is a Proc which accepts one parameter which is a value
11
+ # which needs to be checked and ir is expected to throw an exception
12
+ # if the value is not in compliance with the constraint
13
+ #
14
+ # The class implements functor pattern through to_proc method and
15
+ # closure. Essentially, all constraints can be used everywhere where
16
+ # a block is expected.
17
+ #
18
+ # It provides protected method check which should be overriden in
19
+ # all concrete constraint classes.
20
+ class Constraint
21
+ ##
22
+ # Creates closure with checker invocation
23
+ #
24
+ # It's an interface method required for Constraint to expose
25
+ # functor properties. It encloses invocation of Constraint check_wrapper
26
+ # method into a closure. The closure itself is executed in the context
27
+ # of Parametrized instance which provides proper set "self" variable.
28
+ #
29
+ # The closure itself accepts 2 parameters:
30
+ # * +parameter+ - the Parameter object which the constraint is evaluated
31
+ # against
32
+ # * +value+ - parameter value to be checked
33
+ # ...where instance is assumed from self
34
+ def to_proc
35
+ constraint = self
36
+
37
+ lambda do |parameter, value|
38
+ constraint.check_wrapper(parameter, value, self)
39
+ end
40
+ end
41
+
42
+ ##
43
+ # Wraps constraint-dependent method
44
+ #
45
+ # It wraps constraint-dependent "check" method into a rescue block
46
+ # to standardize exception type and information provided by failed
47
+ # constraint validation
48
+ # * +parameter+ - the Parameter object which the constraint is evaluated
49
+ # against
50
+ # * +value+ - parameter value to be checked
51
+ # * +instance+ - the instance value is checked for
52
+ def check_wrapper(parameter, value, instance)
53
+ check(parameter, value, instance) if pre_condition.check(value, instance)
54
+ rescue StandardError
55
+ raise Templates::Exception::ParameterValueInvalid.new(parameter, instance, value)
56
+ end
57
+
58
+ ##
59
+ # Change precondition of the constraint
60
+ #
61
+ # Pre-condition is a modifier to the main constraint. The constraint won't be evaluated
62
+ # if pre-condition is not met. Default condition is that value should be not nil meaning
63
+ # that if the value is nil then the constraint will be ignored.
64
+ def if(*params, &blk)
65
+ @pre_condition = Condition.for(
66
+ if params.empty?
67
+ raise 'Block must be specified' unless block_given?
68
+ blk
69
+ else
70
+ params.first
71
+ end
72
+ )
73
+
74
+ self
75
+ end
76
+
77
+ def pre_condition
78
+ @pre_condition ||= Condition.not_nil
79
+ end
80
+
81
+ protected
82
+
83
+ ##
84
+ # Constraint-dependent check
85
+ #
86
+ # * +parameter+ - the Parameter object which the constraint is evaluated
87
+ # against
88
+ # * +value+ - parameter value to be checked
89
+ # * +instance+ - the instance value is checked for
90
+ def check(parameter, value, instance); end
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,51 @@
1
+ require 'aws/templates/utils'
2
+
3
+ module Aws
4
+ module Templates
5
+ module Utils
6
+ module Parametrized
7
+ class Constraint
8
+ ##
9
+ # Aggregate constraint
10
+ #
11
+ # It is used to perform checks against a list of constraints-functors
12
+ # or lambdas.
13
+ #
14
+ # === Example
15
+ #
16
+ # class Piece
17
+ # include Aws::Templates::Utils::Parametrized
18
+ # parameter :param1,
19
+ # :constraint => all_of(
20
+ # not_nil,
21
+ # satisfies("Should be moderate") { |v| v < 100 }
22
+ # )
23
+ # end
24
+ #
25
+ # i = Piece.new(:param1 => nil)
26
+ # i.param1 # raise ParameterValueInvalid
27
+ # i = Piece.new(:param1 => 200)
28
+ # i.param1 # raise ParameterValueInvalid with description
29
+ # i = Piece.new(:param1 => 50)
30
+ # i.param1 # => 50
31
+ class AllOf < self
32
+ attr_reader :constraints
33
+
34
+ def initialize(constraints)
35
+ @constraints = constraints
36
+ self.if(Parametrized.any)
37
+ end
38
+
39
+ protected
40
+
41
+ def check(parameter, value, instance)
42
+ constraints.each do |c|
43
+ instance.instance_exec(parameter, value, &c)
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,93 @@
1
+ require 'aws/templates/utils'
2
+
3
+ module Aws
4
+ module Templates
5
+ module Utils
6
+ module Parametrized
7
+ class Constraint
8
+ ##
9
+ # Condition
10
+ #
11
+ # Condition is a strategy which decides if some particular constraint should be enforced
12
+ # or not.
13
+ class Condition
14
+ ##
15
+ # Enforce constraint in all cases
16
+ class Unconditional < Condition
17
+ def check(_, _)
18
+ true
19
+ end
20
+ end
21
+
22
+ ##
23
+ # Do not evaluate constraint if value is nil
24
+ class NotNil < Condition
25
+ def check(value, _)
26
+ !value.nil?
27
+ end
28
+ end
29
+
30
+ ##
31
+ # If value is equal to the condition then evaluate constraint
32
+ class Equal < Condition
33
+ attr_reader :expected
34
+
35
+ def initialize(expected)
36
+ @expected = expected
37
+ end
38
+
39
+ def check(value, _)
40
+ value == expected
41
+ end
42
+ end
43
+
44
+ ##
45
+ # Check specified lambda if constraint should be evaluated
46
+ class Conditional < Condition
47
+ attr_reader :condition
48
+
49
+ def initialize(condition)
50
+ @condition = condition
51
+ end
52
+
53
+ def check(value, instance)
54
+ instance.instance_exec(value, &condition)
55
+ end
56
+ end
57
+
58
+ def check(_value, _instance)
59
+ raise 'Must be overriden'
60
+ end
61
+
62
+ ##
63
+ # Factory method for Unconditional singleton
64
+ def self.any
65
+ @unconditional ||= Unconditional.new
66
+ end
67
+
68
+ ##
69
+ # Factory method for NotNil singleton
70
+ def self.not_nil
71
+ @not_nil ||= NotNil.new
72
+ end
73
+
74
+ ##
75
+ # Factory method transforming arbitrary object into Condition object depending
76
+ # on object's concept.
77
+ def self.for(condition)
78
+ if condition.is_a?(self)
79
+ condition
80
+ elsif condition.respond_to?(:to_sym)
81
+ Equal.new(condition)
82
+ elsif condition.respond_to?(:to_proc)
83
+ Conditional.new(condition)
84
+ else
85
+ Equal.new(condition)
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,60 @@
1
+ require 'aws/templates/utils'
2
+
3
+ module Aws
4
+ module Templates
5
+ module Utils
6
+ module Parametrized
7
+ class Constraint
8
+ ##
9
+ # Switch-like variant check
10
+ #
11
+ # Recursive check implementing switch-based semantics for defining
12
+ # checks need to be performed depending on parameter's value.
13
+ #
14
+ # === Example
15
+ #
16
+ # class Piece
17
+ # include Aws::Templates::Utils::Parametrized
18
+ # parameter :param2
19
+ # parameter :param1,
20
+ # :constraint => depends_on_value(
21
+ # 1 => lambda { |v| raise 'Too big' if param2 > 3 },
22
+ # 2 => lambda { |v| raise 'Too small' if param2 < 2 }
23
+ # )
24
+ # end
25
+ #
26
+ # i = Piece.new(:param1 => 1, :param2 => 1)
27
+ # i.param1 # => 1
28
+ # i = Piece.new(:param1 => 1, :param2 => 5)
29
+ # i.param1 # raise ParameterValueInvalid
30
+ # i = Piece.new(:param1 => 2, :param2 => 1)
31
+ # i.param1 # raise ParameterValueInvalid
32
+ # i = Piece.new(:param1 => 2, :param2 => 5)
33
+ # i.param1 # => 2
34
+ class DependsOnValue < self
35
+ ##
36
+ # Selector hash
37
+ attr_reader :selector
38
+
39
+ def initialize(selector)
40
+ @selector = selector
41
+ self.if(Parametrized.any)
42
+ end
43
+
44
+ protected
45
+
46
+ def check(parameter, value, instance)
47
+ return unless selector.key?(value)
48
+
49
+ instance.instance_exec(
50
+ parameter,
51
+ value,
52
+ &selector[value]
53
+ )
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,86 @@
1
+ require 'aws/templates/utils'
2
+
3
+ module Aws
4
+ module Templates
5
+ module Utils
6
+ module Parametrized
7
+ class Constraint
8
+ ##
9
+ # Syntax sugar for constraints definition
10
+ #
11
+ # It injects the methods as class-scope methods into mixing classes.
12
+ # The methods are factories to create particular type of constraint
13
+ module Dsl
14
+ include Utils::Inheritable
15
+
16
+ class_scope do
17
+ ##
18
+ # Match-all precondition
19
+ #
20
+ # Any constraint with this precondition will process any value
21
+ def any
22
+ Constraint::Condition.any
23
+ end
24
+
25
+ ##
26
+ # Parameter shouldn't be nil
27
+ #
28
+ # alias for NotNil class
29
+ def not_nil
30
+ Parametrized::Constraint::NotNil.new
31
+ end
32
+
33
+ ##
34
+ # Parameter value should be in enumeration
35
+ #
36
+ # alias for Enum class
37
+ def enum(*items)
38
+ Parametrized::Constraint::Enum.new(items.flatten)
39
+ end
40
+
41
+ ##
42
+ # Parameter value should satisfy all specified constraints
43
+ #
44
+ # alias for AllOf class
45
+ def all_of(*constraints)
46
+ Parametrized::Constraint::AllOf.new(constraints)
47
+ end
48
+
49
+ ##
50
+ # Requires presence of the parameters if condition is satisfied
51
+ #
52
+ # alias for Requires class
53
+ def requires(*dependencies)
54
+ Parametrized::Constraint::Requires.new(dependencies)
55
+ end
56
+
57
+ ##
58
+ # Constraint depends on value
59
+ #
60
+ # alias for DependsOnValue class
61
+ def depends_on_value(selector)
62
+ Parametrized::Constraint::DependsOnValue.new(selector)
63
+ end
64
+
65
+ ##
66
+ # Constraint should satisfy the condition
67
+ #
68
+ # alias for SatisfiesCondition class
69
+ def satisfies(description, &cond_block)
70
+ Parametrized::Constraint::SatisfiesCondition.new(description, &cond_block)
71
+ end
72
+
73
+ ##
74
+ # Value should match the regular experession
75
+ #
76
+ # alias for Matches
77
+ def matches(rex)
78
+ Parametrized::Constraint::Matches.new(rex)
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end