cloud-templates 0.2.0 → 0.3.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 (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,29 @@
1
+ require 'aws/templates/utils'
2
+
3
+ module Aws
4
+ module Templates
5
+ module Utils
6
+ module Contextualized
7
+ class Filter
8
+ ##
9
+ # Proc proxy
10
+ #
11
+ # Just passes opts to the proc the filter was initialized with. It is used internaly.
12
+ class Proxy < self
13
+ using Contextualized::Refinements
14
+
15
+ attr_reader :proc
16
+
17
+ def initialize(prc, &blk)
18
+ @proc = prc || blk
19
+ end
20
+
21
+ def filter(opts, memo, instance)
22
+ instance.instance_exec(opts, memo, &proc)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,46 @@
1
+ require 'aws/templates/utils'
2
+
3
+ module Aws
4
+ module Templates
5
+ module Utils
6
+ module Contextualized
7
+ class Filter
8
+ ##
9
+ # Base class for recursive operations
10
+ #
11
+ # Internally used by Add and Remove filters.
12
+ class RecursiveSchemaFilter < self
13
+ using Contextualized::Refinements
14
+
15
+ attr_reader :scheme
16
+
17
+ def initialize(*args)
18
+ schm = if args.last.respond_to?(:to_hash)
19
+ args.each_with_object(args.pop) do |field, hsh|
20
+ hsh[field] = nil
21
+ hsh
22
+ end
23
+ else
24
+ args
25
+ end
26
+
27
+ @scheme = _check_scheme(schm)
28
+ end
29
+
30
+ private
31
+
32
+ def _check_scheme(schm)
33
+ if schm.respond_to?(:to_hash)
34
+ schm.to_hash.each_pair { |_, sub| _check_scheme(sub) unless sub.nil? }
35
+ elsif !schm.respond_to?(:to_a)
36
+ raise "#{schm.inspect} is not appropriate branch in the scheme"
37
+ end
38
+
39
+ schm
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,66 @@
1
+ require 'aws/templates/utils'
2
+
3
+ module Aws
4
+ module Templates
5
+ module Utils
6
+ module Contextualized
7
+ class Filter
8
+ ##
9
+ # Remove specified keys from hash
10
+ #
11
+ # The filter performs removal of values from options hash
12
+ # according to specified schema represented by combination of
13
+ # nested hashes and arrays. User can specify removal of values
14
+ # at arbitrary depth in options hash hierarchy with arbitrary
15
+ # granularity.
16
+ #
17
+ # === Example
18
+ #
19
+ # class Piece
20
+ # include Aws::Templates::Utils::Contextualized
21
+ #
22
+ # contextualize filter(:copy) & filter(:remove, :a, :b, c: [:d])
23
+ # end
24
+ #
25
+ # i = Piece.new()
26
+ # opts = Options.new(a: { q: 1 }, b: 2, c: { d: { r: 5 }, e: 1 })
27
+ # opts.filter(i.filter) # => { c: { e: 1 } }
28
+ class Remove < RecursiveSchemaFilter
29
+ using Contextualized::Refinements
30
+
31
+ def filter(_, memo, _)
32
+ _recurse_remove(memo, scheme)
33
+ memo
34
+ end
35
+
36
+ private
37
+
38
+ def _recurse_remove(opts, schm)
39
+ return unless Utils.recursive?(opts)
40
+
41
+ if Utils.hashable?(schm)
42
+ _scheme_remove(opts, schm.to_hash)
43
+ elsif Utils.list?(schm)
44
+ _list_remove(opts, schm.to_ary)
45
+ end
46
+ end
47
+
48
+ def _list_remove(opts, list)
49
+ list.each { |field| opts.delete(field) }
50
+ end
51
+
52
+ def _scheme_remove(opts, schm)
53
+ schm.each_pair do |field, sub_scheme|
54
+ if sub_scheme.nil?
55
+ opts.delete(field)
56
+ elsif opts.include?(field)
57
+ _recurse_remove(opts[field], sub_scheme)
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,42 @@
1
+ require 'aws/templates/utils'
2
+
3
+ module Aws
4
+ module Templates
5
+ module Utils
6
+ module Contextualized
7
+ class Filter
8
+ ##
9
+ # Statically scoped filter
10
+ #
11
+ # Scoped filter wraps whatever Proc obejct passed to it into specified scope for
12
+ # execution. So whatever the scope the filter is called in, it will always be evaluated
13
+ # in the same scope specified at creation.
14
+ #
15
+ # The filter is used by the internal mechanics of the framework.
16
+ class Scoped < self
17
+ using Contextualized::Refinements
18
+
19
+ attr_reader :scoped_filter
20
+ attr_reader :scope
21
+
22
+ def initialize(fltr, scp)
23
+ @scoped_filter = _check_filter(fltr)
24
+ @scope = scp
25
+ end
26
+
27
+ def filter(options, memo, _)
28
+ scope.instance_exec(options, memo, &scoped_filter)
29
+ end
30
+
31
+ private
32
+
33
+ def _check_filter(fltr)
34
+ raise "#{fltr} is not a filter" unless fltr.respond_to?(:to_proc)
35
+ fltr
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,51 @@
1
+ require 'aws/templates/utils'
2
+
3
+ module Aws
4
+ module Templates
5
+ module Utils
6
+ module Contextualized
7
+ ##
8
+ # Filter transformation refinements
9
+ #
10
+ # Contains standard object refinements to transform their instances into scope filters.
11
+ module Refinements
12
+ ##
13
+ # Hash class patch
14
+ #
15
+ # Adds to_filter method converting a hash into an Override filter
16
+ refine ::Hash do
17
+ ##
18
+ # Convert to Override filter
19
+ def to_filter
20
+ Aws::Templates::Utils::Contextualized::Filter::Override.new(self)
21
+ end
22
+ end
23
+
24
+ ##
25
+ # NilClass class patch
26
+ #
27
+ # Adds to_filter method converting nil into the Identity filter
28
+ refine ::NilClass do
29
+ ##
30
+ # Convert nil to Identity filter
31
+ def to_filter
32
+ Aws::Templates::Utils::Contextualized::Filter::Identity.new
33
+ end
34
+ end
35
+
36
+ ##
37
+ # Proc class patch
38
+ #
39
+ # Adds to_filter method proxying a Proc through Filter interface object
40
+ refine ::Proc do
41
+ ##
42
+ # Proxy the Proc through Proxy filter object
43
+ def to_filter
44
+ Aws::Templates::Utils::Contextualized::Filter::Proxy.new(self)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -1,6 +1,4 @@
1
1
  require 'aws/templates/utils'
2
- require 'aws/templates/utils/options'
3
- require 'aws/templates/utils/inheritable'
4
2
 
5
3
  module Aws
6
4
  module Templates
@@ -16,24 +14,239 @@ module Aws
16
14
  # object which have common traits organized as an arbitrary graph
17
15
  # with many-to-many relationship.
18
16
  module Default
19
- include Inheritable
17
+ include Aws::Templates::Utils::Inheritable
18
+
19
+ ##
20
+ # Functionality-specific refinements
21
+ module Refinements
22
+ refine ::Object do
23
+ ##
24
+ # If the object can be considered a defaults override
25
+ #
26
+ # It is the one which can't be merged with existing defaults layers
27
+ def override?
28
+ respond_to?(:to_sym) || !(respond_to?(:to_proc) || Utils.recursive?(self))
29
+ end
30
+
31
+ ##
32
+ # Transform object to defaults definition.
33
+ def to_definition
34
+ Definition.from(self)
35
+ end
36
+ end
37
+ end
38
+
39
+ using Refinements
40
+
41
+ ##
42
+ # Abstract defaults definition
43
+ #
44
+ # Defaults definition is an object wrapper which enables definition merging. It also
45
+ # contains factory method to transform arbitrary objects into Definition object and defines
46
+ # basic functionality.
47
+ class Definition
48
+ ##
49
+ # Empty definition
50
+ #
51
+ # Doesn't mutate definition in any way. Tries to elliminate itself from the chain
52
+ # of merges.
53
+ class Empty < Definition
54
+ include ::Singleton
55
+
56
+ def for(_)
57
+ {}
58
+ end
59
+
60
+ def merge(b)
61
+ b.to_definition
62
+ end
63
+ end
64
+
65
+ ##
66
+ # Scalar definition
67
+ #
68
+ # Scalar definition overrides everything before it and can be overriden by anything
69
+ # after it.
70
+ class Scalar < Definition
71
+ attr_reader :value
72
+
73
+ def initialize(value)
74
+ @value = value
75
+ end
76
+
77
+ def for(_)
78
+ value
79
+ end
80
+
81
+ def merge(b)
82
+ b.to_definition
83
+ end
84
+
85
+ def override?
86
+ true
87
+ end
88
+ end
89
+
90
+ ##
91
+ # Definition with scheme
92
+ #
93
+ # Scheme definition can be merged without stacking layers with any other scheme
94
+ # definition. Internal schemes will be merged together producing aggregated scheme.
95
+ # Otherwise, the definition is wither overriden with Scalar or stacked together with
96
+ # Calculable.
97
+ class Scheme < Definition
98
+ attr_reader :scheme
99
+
100
+ def initialize(scheme)
101
+ @scheme = scheme
102
+ end
103
+
104
+ def merge(b)
105
+ if b.is_a? self.class
106
+ merge(b.scheme)
107
+ elsif Utils.recursive?(b)
108
+ self.class.new(Utils.merge(scheme, b) { |left, right| _merge(left, right) })
109
+ else
110
+ super(b)
111
+ end
112
+ end
113
+
114
+ def for(_)
115
+ scheme
116
+ end
117
+
118
+ private
119
+
120
+ def _merge(a, b)
121
+ a.override? || b.override? ? b : a.to_definition.merge(b)
122
+ end
123
+ end
124
+
125
+ ##
126
+ # Lazy-calculated definition
127
+ #
128
+ # Contains functor object which will be evaluated only during actual value look-up
129
+ class Calculable < Definition
130
+ include Utils::Guarded
131
+
132
+ attr_reader :block
133
+
134
+ def initialize(block)
135
+ @block = block
136
+ end
137
+
138
+ def for(instance)
139
+ guarded_for(instance, block) { _process_value(block, instance) }
140
+ end
141
+
142
+ private
143
+
144
+ def _process_value(value, instance)
145
+ return value if value.override? || Utils.recursive?(value)
146
+ _process_value(instance.instance_eval(&value), instance)
147
+ end
148
+ end
149
+
150
+ ##
151
+ # Definition composition
152
+ #
153
+ # Pair of definitions which act like one.
154
+ class Pair < Definition
155
+ class << self
156
+ def [](a, b)
157
+ return b if b.override? || a.override? || a == Definition.empty
158
+ return a if b.nil? || b == Definition.empty
159
+ _unite(a, b)
160
+ end
161
+
162
+ private
163
+
164
+ def _unite(a, b)
165
+ if a.is_a?(self)
166
+ new(a.a, a.b.merge(b))
167
+ elsif b.is_a?(self)
168
+ new(a.merge(b.a), b.b)
169
+ else
170
+ new(a, b)
171
+ end
172
+ end
173
+ end
174
+
175
+ attr_reader :a
176
+ attr_reader :b
177
+
178
+ def initialize(a, b)
179
+ @a = a.to_definition
180
+ @b = b.to_definition
181
+ end
182
+
183
+ def for(instance)
184
+ eval_b = b.for(instance)
185
+ return eval_b if eval_b.override? && !eval_b.nil?
186
+ eval_a = a.for(instance)
187
+ return eval_b if eval_a.override?
188
+
189
+ eval_a.to_definition.merge(eval_b).for(instance)
190
+ end
191
+ end
192
+
193
+ class << self
194
+ def empty
195
+ Empty.instance
196
+ end
197
+
198
+ def from(obj)
199
+ return obj if obj.is_a? Definition
200
+ return Scalar.new(obj) if obj.override?
201
+ return Scheme.new(obj) if Utils.recursive?(obj)
202
+ return Calculable.new(obj) if obj.respond_to?(:to_proc)
203
+
204
+ raise "Invalid object #{obj}"
205
+ end
206
+ end
207
+
208
+ def merge(b)
209
+ return b if b.override?
210
+ return self if b == Definition.empty
211
+ Pair[self, b]
212
+ end
213
+
214
+ def for(_)
215
+ raise 'Must be overriden'
216
+ end
217
+
218
+ def to_definition
219
+ self
220
+ end
221
+
222
+ def override?
223
+ false
224
+ end
225
+ end
20
226
 
21
227
  ##
22
228
  # Hash wrapper
23
229
  #
24
230
  # The hash wrapper does intermediate calculations of nested lambdas in the specified
25
231
  # context as they are encountered
26
- class Definition
232
+ class Instantiation
233
+ def value
234
+ return @value if @value
235
+ @value = @entry.to_definition.for(@context)
236
+ raise "#{@value.inspect} is not recursive" if @value.override?
237
+ @value
238
+ end
239
+
27
240
  ##
28
241
  # Defined hash keys
29
242
  def keys
30
- @hash.keys
243
+ value.keys
31
244
  end
32
245
 
33
246
  ##
34
247
  # Transform to hash
35
248
  def to_hash
36
- _recurse_into(@hash)
249
+ _recurse_into(value)
37
250
  end
38
251
 
39
252
  def dependency?
@@ -51,14 +264,14 @@ module Aws
51
264
  # returns it wrapping into Definition instance with the same context if needed
52
265
  # (if value is a map)
53
266
  def [](k)
54
- result = _process_value(@hash[k])
55
- result.respond_to?(:to_hash) ? self.class.new(result, @context) : result
267
+ result = _process_value(value[k])
268
+ Utils.recursive?(result) ? _new(result) : result
56
269
  end
57
270
 
58
271
  ##
59
272
  # Check if the key is present in the hash
60
273
  def include?(k)
61
- @hash.include?(k)
274
+ value.include?(k)
62
275
  end
63
276
 
64
277
  # The class already supports recursive concept so return self
@@ -70,27 +283,26 @@ module Aws
70
283
  # Create wrapper object
71
284
  #
72
285
  # Creates wrapper object with attached hash and context to evaluate lambdas in
73
- def initialize(hsh, ctx)
74
- @hash = hsh
286
+ def initialize(ent, ctx)
287
+ raise "#{ent.inspect} is not recursive" if ent.override?
288
+ @entry = ent
75
289
  @context = ctx
76
290
  end
77
291
 
78
292
  private
79
293
 
80
294
  def _process_value(value)
81
- if value.respond_to?(:to_hash)
82
- value
83
- elsif value.respond_to?(:to_proc)
84
- @context.instance_eval(&value)
85
- else
86
- value
87
- end
295
+ value.override? || Utils.recursive?(value) ? value : value.to_definition.for(@context)
296
+ end
297
+
298
+ def _new(ent)
299
+ self.class.new(ent, @context)
88
300
  end
89
301
 
90
302
  def _recurse_into(value)
91
- value.each_with_object({}) do |(k, v), memo|
92
- processed = _process_value(v)
93
- processed = _recurse_into(processed.to_hash) if Utils.hashable?(processed)
303
+ value.keys.each_with_object({}) do |k, memo|
304
+ processed = _process_value(value[k])
305
+ processed = _recurse_into(processed) if Utils.recursive?(processed)
94
306
  memo[k] = processed
95
307
  end
96
308
  end
@@ -109,22 +321,7 @@ module Aws
109
321
  # is working correctly with both parent classes and all Default
110
322
  # mixins used in between.
111
323
  def defaults
112
- # iterating through all ancestors with defaults
113
- ancestors_with_defaults.inject(Options.new) do |opts, mod|
114
- mod.defaults.inject(opts) do |acc, defaults_definition|
115
- acc.merge!(Definition.new(defaults_definition, self))
116
- end
117
- end
118
- end
119
-
120
- private
121
-
122
- def ancestors_with_defaults
123
- self
124
- .class
125
- .ancestors
126
- .select { |mod| (mod != Default) && mod.ancestors.include?(Default) }
127
- .reverse!
324
+ Instantiation.new(self.class.defaults_definition, self)
128
325
  end
129
326
  end
130
327
 
@@ -139,17 +336,28 @@ module Aws
139
336
  Aws::Templates::Utils::DELETED_MARKER
140
337
  end
141
338
 
339
+ ##
340
+ # Module's specific defaults
341
+ #
342
+ # The defaults defined in this module and not its' ancestors.
343
+ def module_defaults_definition
344
+ @module_defaults_definition || Definition.empty
345
+ end
346
+
142
347
  ##
143
348
  # Defaults for the input hash
144
349
  #
145
- # Class-level accessor of a hash which will be merged into input
146
- # parameters hash. The hash can't be changed directly or set to
147
- # another value. Only incremental changes are allowed with
148
- # default method which is a part of the framework DSL. The method
149
- # returns only defaults for the current class without
150
- # consideration of the class hierarchy.
151
- def defaults
152
- @defaults ||= []
350
+ # Class-level accessor of a definition of defaults which will be merged into input
351
+ # parameters hash. The definition can't be changed directly or set to another value.
352
+ # Only incremental changes are allowed with default method which is a part of the
353
+ # framework DSL. The method returns accumulated defaults of all ancestors of the module
354
+ # in one single definition object
355
+ def defaults_definition
356
+ return @defaults if @defaults
357
+ @defaults = ancestors_with(Default)
358
+ .inject(Definition.empty) do |acc, elem|
359
+ acc.merge(elem.module_defaults_definition)
360
+ end
153
361
  end
154
362
 
155
363
  ##
@@ -169,11 +377,11 @@ module Aws
169
377
  #
170
378
  # If the parameter you passed is neither a hash nor callable or
171
379
  # no parameters are passed at all, ArgumentError will be thrown.
172
- def default(defaults_hash = nil)
173
- raise_defaults_is_nil unless defaults_hash
174
- raise_default_type_mismatch(defaults_hash) unless defaults_hash.respond_to?(:to_hash)
380
+ def default(param)
381
+ raise_defaults_is_nil unless param
382
+ raise_default_type_mismatch(param) if param.override?
175
383
 
176
- defaults << defaults_hash.to_hash
384
+ @module_defaults_definition = module_defaults_definition.to_definition.merge(param)
177
385
  end
178
386
 
179
387
  def raise_defaults_is_nil
@@ -181,7 +389,7 @@ module Aws
181
389
  end
182
390
 
183
391
  def raise_default_type_mismatch(defaults_hash)
184
- raise ArgumentError.new("#{defaults_hash.inspect} is not a hash")
392
+ raise ArgumentError.new("#{defaults_hash.inspect} is not recursive or proc")
185
393
  end
186
394
  end
187
395
  end