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
@@ -0,0 +1,39 @@
1
+ require 'aws/templates/utils'
2
+
3
+ module Aws
4
+ module Templates
5
+ module Utils
6
+ module Parametrized
7
+ class Transformation
8
+ ##
9
+ # Convert input into integer
10
+ #
11
+ # Input value can be anything implementing :to_i method.
12
+ #
13
+ # === Example
14
+ #
15
+ # class Piece
16
+ # include Aws::Templates::Utils::Parametrized
17
+ #
18
+ # parameter :param, :transform => as_integer
19
+ # end
20
+ #
21
+ # i = Piece.new
22
+ # i.param # => nil
23
+ # i = Piece.new(:param => '23')
24
+ # i.param # => 23
25
+ class AsInteger < self
26
+ include ::Singleton
27
+
28
+ protected
29
+
30
+ def transform(_, value, _)
31
+ return if value.nil?
32
+ Integer(value)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,97 @@
1
+ require 'aws/templates/utils'
2
+
3
+ module Aws
4
+ module Templates
5
+ module Utils
6
+ module Parametrized
7
+ class Transformation
8
+ ##
9
+ # Transform input value into a list
10
+ #
11
+ # Input value can be either an array or something which implements
12
+ # to_a method standard semantics. Each list entry is evaluated
13
+ # with specified constraints and transformations.
14
+ #
15
+ # With as_list transformation you can have as many nested levels
16
+ # as it's needed in terms of nested lists or nested objects.
17
+ #
18
+ # === Example
19
+ #
20
+ # class Piece
21
+ # include Aws::Templates::Utils::Parametrized
22
+ #
23
+ # parameter :param1, :transform => as_list(
24
+ # # alias for all elements. Plays a role during introspection
25
+ # :name => :element,
26
+ # # description of what the element represents
27
+ # :description => 'List element',
28
+ # # constraint for list element
29
+ # :constraint => not_nil
30
+ # )
31
+ # parameter :param2, :transform => as_list(
32
+ # :name => :element,
33
+ # :description => 'List element',
34
+ # :transform => as_list( # nested list
35
+ # :name => :sub_element,
36
+ # :description => 'Sub-list element',
37
+ # :constraint => not_nil
38
+ # )
39
+ # )
40
+ # parameter :param3, :transform => as_list(
41
+ # :name => :particle,
42
+ # :description => 'Small particle',
43
+ # :transform => as_object( # nested object
44
+ # Aws::Templates::Utils::AsNamed
45
+ # )
46
+ # )
47
+ # end
48
+ #
49
+ # i = Piece.new(:param1 => [1,2,3])
50
+ # i.param1 # => [1,2,3]
51
+ # i = Piece.new(:param1 => [1,nil,3])
52
+ # i.param1 # throws exception
53
+ # i = Piece.new(:param2 => [[1],[2],[3]])
54
+ # i.param2 # => [[1],[2],[3]]
55
+ # i = Piece.new(:param2 => [1,[2],[3]])
56
+ # i.param2 # throws exception
57
+ # i = Piece.new(:param2 => [[1],[nil],[3]])
58
+ # i.param2 # throws exception
59
+ # i = Piece.new(:param3 => [{:name => 'Zed'}])
60
+ # i.param3.first.name # => 'Zed'
61
+ class AsList < self
62
+ attr_reader :sub_parameter
63
+
64
+ def initialize(klass = nil, options = nil)
65
+ return if options.nil?
66
+
67
+ @sub_parameter = Parametrized::Parameter.new(
68
+ options[:name],
69
+ klass,
70
+ description: options[:description],
71
+ transform: options[:transform],
72
+ constraint: options[:constraint]
73
+ )
74
+ end
75
+
76
+ protected
77
+
78
+ def transform(parameter, value, instance)
79
+ return if value.nil?
80
+
81
+ unless value.respond_to?(:to_a)
82
+ raise "#{parameter.name} is assigned to " \
83
+ "#{value.inspect} which is not a list"
84
+ end
85
+
86
+ if sub_parameter
87
+ value.to_a.map { |el| sub_parameter.process_value(instance, el) }
88
+ else
89
+ value.to_a
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,42 @@
1
+ require 'aws/templates/utils'
2
+
3
+ module Aws
4
+ module Templates
5
+ module Utils
6
+ module Parametrized
7
+ class Transformation
8
+ ##
9
+ # Convert to a Ruby class
10
+ #
11
+ # The transformation allows to use elements of metaprogramming in the framework. It
12
+ # tries to transform passed value to a Ruby class.
13
+ #
14
+ # === Example
15
+ #
16
+ # class Piece
17
+ # include Aws::Templates::Utils::Parametrized
18
+ #
19
+ # parameter :param, :transform => as_module
20
+ # end
21
+ #
22
+ # i = Piece.new
23
+ # i.param # => nil
24
+ # i = Piece.new(:param => 'Object')
25
+ # i.param # => Object
26
+ class AsModule < self
27
+ include ::Singleton
28
+
29
+ protected
30
+
31
+ def transform(_, value, _)
32
+ return if value.nil?
33
+ return value if value.is_a?(Module)
34
+ return Utils.lookup_module(value.to_s) if value.respond_to?(:to_s)
35
+ raise "#{value} can't be transformed to a class"
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,76 @@
1
+ require 'aws/templates/utils'
2
+
3
+ module Aws
4
+ module Templates
5
+ module Utils
6
+ module Parametrized
7
+ class Transformation
8
+ ##
9
+ # Transform input value into the object
10
+ #
11
+ # Input value can be either hash or object. The transformation performs
12
+ # nested object evaluation recusrsively as the input were Parametrized
13
+ # instance. As a parameter for the transformation you can
14
+ # specify either Module which mixes in Parametrized or to use
15
+ # a block which is to be evaluated as a part of Parametrized definition
16
+ # or both.
17
+ #
18
+ # With as_object transformation you can have as many nested levels
19
+ # as it's needed.
20
+ #
21
+ # === Example
22
+ #
23
+ # class Piece
24
+ # include Aws::Templates::Utils::Parametrized
25
+ #
26
+ # parameter :param1,
27
+ # :transform => as_object(Aws::Templates::Utils::AsNamed)
28
+ # parameter :param2, :transform => as_object {
29
+ # parameter :id, :description => 'Just ID',
30
+ # :constraint => not_nil
31
+ # }
32
+ # parameter :param3,
33
+ # :transform => as_object(Aws::Templates::Utils::AsNamed) {
34
+ # parameter :path, :description => 'Just path',
35
+ # :constraint => not_nil
36
+ # }
37
+ # end
38
+ #
39
+ # i = Piece.new
40
+ # i.param1 # => nil
41
+ # i = Piece.new(:param1 => {:name => 'Zed'})
42
+ # i.param1.name # => 'Zed'
43
+ # i = Piece.new(:param2 => {:id => 123})
44
+ # i.param2.id # => 123
45
+ # i = Piece.new(:param3 => {:path => 'a/b', :name => 'Rex'})
46
+ # i.param3.path # => 'a/b'
47
+ # i.param3.name # => 123
48
+ class AsObject < self
49
+ attr_reader :klass
50
+
51
+ def initialize(scope, klass = nil, &definition)
52
+ @klass = if klass.nil?
53
+ Parametrized::Nested.create_class(scope)
54
+ elsif klass.is_a?(Class)
55
+ klass
56
+ elsif klass.is_a?(Module)
57
+ Parametrized::Nested.create_class(scope).with(klass)
58
+ else
59
+ raise "#{klass} is neither a class nor a module"
60
+ end
61
+
62
+ @klass.class_eval(&definition) unless definition.nil?
63
+ end
64
+
65
+ protected
66
+
67
+ def transform(_, value, instance)
68
+ return if value.nil?
69
+ klass.new(instance, value)
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,100 @@
1
+ require 'aws/templates/utils'
2
+
3
+ module Aws
4
+ module Templates
5
+ module Utils
6
+ module Parametrized
7
+ class Transformation
8
+ ##
9
+ # Transform value with the specified render
10
+ #
11
+ # Input value can be anything which could be rendered by the
12
+ # specified render type. Returned value is rendered input.
13
+ #
14
+ # The transformation is useful when you have a document of some
15
+ # format embedded into a document of another format. An example
16
+ # could be Bash scripts embedded into AWS CFN template.
17
+ #
18
+ # === Example
19
+ #
20
+ # class Brush
21
+ # attr_reader :color
22
+ # attr_reader :thickness
23
+ # attr_reader :type
24
+ #
25
+ # def initialize(c, thick, t)
26
+ # @c = c
27
+ # @thick = thick
28
+ # @t = t
29
+ # end
30
+ # end
31
+ #
32
+ # class Circle
33
+ # attr_reader :radius
34
+ # attr_reader :brush
35
+ #
36
+ # def initialize(r, b)
37
+ # @radius = r
38
+ # @brush = b
39
+ # end
40
+ # end
41
+ #
42
+ # class Piece
43
+ # include Aws::Templates::Utils::Parametrized
44
+ #
45
+ # parameter :picture, :transform => as_rendered(
46
+ # # Render Type
47
+ # Graphics::Renders::JPEG,
48
+ # # parameter section for the render
49
+ # format: :base64
50
+ # )
51
+ # end
52
+ #
53
+ # i = Piece.new(picture: Circle.new(10, Brush.new(:red, 2, :dots)))
54
+ # i.picture # => <rendered representation>
55
+ class AsRendered < self
56
+ attr_reader :type
57
+ attr_reader :parameters
58
+
59
+ def initialize(render_type, params)
60
+ @type = _check_render_type(render_type)
61
+ @parameters = params
62
+ end
63
+
64
+ protected
65
+
66
+ def transform(_, value, instance)
67
+ return if value.nil?
68
+ type.view_for(value, _compute_render_parameters(instance)).to_rendered
69
+ end
70
+
71
+ private
72
+
73
+ def _check_render_type(render_type)
74
+ unless render_type.respond_to?(:view_for)
75
+ raise(
76
+ "Wrong render type object #{params}. " \
77
+ 'The instance should have #view_for method.'
78
+ )
79
+ end
80
+
81
+ render_type
82
+ end
83
+
84
+ def _compute_render_parameters(instance)
85
+ return if parameters.nil?
86
+
87
+ if parameters.respond_to?(:to_hash)
88
+ parameters
89
+ elsif parameters.respond_to?(:to_proc)
90
+ instance.instance_exec(&parameters)
91
+ else
92
+ parameters
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,39 @@
1
+ require 'aws/templates/utils'
2
+
3
+ module Aws
4
+ module Templates
5
+ module Utils
6
+ module Parametrized
7
+ class Transformation
8
+ ##
9
+ # Convert input into string
10
+ #
11
+ # Input value can be anything implementing :to_s method.
12
+ #
13
+ # === Example
14
+ #
15
+ # class Piece
16
+ # include Aws::Templates::Utils::Parametrized
17
+ #
18
+ # parameter :param, :transform => as_string
19
+ # end
20
+ #
21
+ # i = Piece.new
22
+ # i.param # => nil
23
+ # i = Piece.new(:param => 23)
24
+ # i.param # => '23'
25
+ class AsString < self
26
+ include ::Singleton
27
+
28
+ protected
29
+
30
+ def transform(_, value, _)
31
+ return if value.nil?
32
+ String(value)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,94 @@
1
+ require 'aws/templates/utils'
2
+
3
+ module Aws
4
+ module Templates
5
+ module Utils
6
+ module Parametrized
7
+ class Transformation
8
+ ##
9
+ # Syntax sugar for transformations definition
10
+ #
11
+ # It injects the methods as class-scope methods into mixing classes.
12
+ # The methods are factories to create particular type of transformation
13
+ module Dsl
14
+ include Utils::Inheritable
15
+
16
+ class_scope do
17
+ ##
18
+ # Chain a few transformations into a single one
19
+ #
20
+ # alias for AsChain class
21
+ def as_chain(*components)
22
+ Parametrized::Transformation::AsChain.new(components)
23
+ end
24
+
25
+ ##
26
+ # Transform the value into an object
27
+ #
28
+ # alias for AsObject class
29
+ def as_object(klass = nil, &definition)
30
+ Parametrized::Transformation::AsObject.new(self, klass, &definition)
31
+ end
32
+
33
+ ##
34
+ # Transform the value into a list
35
+ #
36
+ # alias for AsList class
37
+ def as_list(parameters = nil)
38
+ Parametrized::Transformation::AsList.new(self, parameters)
39
+ end
40
+
41
+ ##
42
+ # Transform value with the specified render
43
+ #
44
+ # alias for AsRendered class
45
+ def as_rendered(render_type, params = nil, &params_block)
46
+ Parametrized::Transformation::AsRendered.new(render_type, params || params_block)
47
+ end
48
+
49
+ ##
50
+ # Convert input into integer
51
+ #
52
+ # alias for AsInteger class
53
+ def as_integer
54
+ Parametrized::Transformation::AsInteger.instance
55
+ end
56
+
57
+ ##
58
+ # Convert input into string
59
+ #
60
+ # alias for AsString class
61
+ def as_string
62
+ Parametrized::Transformation::AsString.instance
63
+ end
64
+
65
+ ##
66
+ # Convert input into boolean
67
+ #
68
+ # alias for AsBoolean class
69
+ def as_boolean
70
+ Parametrized::Transformation::AsBoolean.instance
71
+ end
72
+
73
+ ##
74
+ # Convert input into hash
75
+ #
76
+ # alias for AsHash class
77
+ def as_hash(&blk)
78
+ Parametrized::Transformation::AsHash.new(self, &blk)
79
+ end
80
+
81
+ ##
82
+ # Convert input into a class
83
+ #
84
+ # alias for AsModule class
85
+ def as_module
86
+ Parametrized::Transformation::AsModule.instance
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end