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,42 @@
1
+ require 'aws/templates/utils'
2
+
3
+ module Aws
4
+ module Templates
5
+ module Help
6
+ module Rdoc
7
+ ##
8
+ # Rdoc classes wrapper
9
+ #
10
+ # Creates simple DSL over Rdoc native classes so text blocks can be composed in idiomatic
11
+ # Ruby way.
12
+ module Texting
13
+ def sub(*parts)
14
+ item = ::RDoc::Markup::ListItem.new(nil, *parts)
15
+ yield item if block_given?
16
+ item
17
+ end
18
+
19
+ def text(str)
20
+ ::RDoc::Markup::Paragraph.new(str)
21
+ end
22
+
23
+ def list(type = :BULLET, *parts)
24
+ list = ::RDoc::Markup::List.new(type, *parts)
25
+ yield list if block_given?
26
+ list
27
+ end
28
+
29
+ def parsed_for(str)
30
+ sub { |s| RDoc::Markup.parse(str).each { |part| s << part } }
31
+ end
32
+
33
+ def document(*parts)
34
+ doc = ::RDoc::Markup::Document.new(*parts)
35
+ yield doc if block_given?
36
+ doc
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,53 @@
1
+ require 'aws/templates/utils'
2
+
3
+ module Aws
4
+ module Templates
5
+ ##
6
+ # Abstract processor
7
+ #
8
+ # Processors are used in artifacts rendering and help generators. Processor embodies
9
+ # functionality required to register an entity handler and look-up the registry when the
10
+ # entity is processed.
11
+ module Processor
12
+ include Routing
13
+
14
+ ##
15
+ # Proxy for Registry handler_for method
16
+ def handler_for(*args)
17
+ registry.handler_for(*args)
18
+ end
19
+
20
+ ##
21
+ # Proxy for Registry handler? method
22
+ def handler?(*args)
23
+ registry.handler?(*args)
24
+ end
25
+
26
+ ##
27
+ # Handlder look-up logic.
28
+ #
29
+ # Should provide logic for processing entities.
30
+ def process(_entity, _params = nil)
31
+ raise Templates::Exception::NotImplementedError.new('The method should be overriden')
32
+ end
33
+
34
+ ##
35
+ # Define handler for entity
36
+ #
37
+ # Another way to define handlers for entities. Creates anonymous class and attaches as
38
+ # the handler to the specified entity.
39
+ def define_handler(entity, handler, &blk)
40
+ Class.new(handler, &blk).register_in(self).for_entity(entity)
41
+ end
42
+
43
+ ##
44
+ # Add handlers
45
+ #
46
+ # Add routing between handlers and correspondent entities from another entity which supports
47
+ # routing concept.
48
+ def routing(routes)
49
+ registry.merge(routes.registry)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,104 @@
1
+ require 'aws/templates/utils'
2
+
3
+ module Aws
4
+ module Templates
5
+ module Processor
6
+ ##
7
+ # Basic entity handler
8
+ #
9
+ # Handler are classes encapsulating functionality of transforming entity into desired output.
10
+ # For instance, the same LDAP record can be transformed into JSON description or LDIF
11
+ # definition. The same help article can be written to output console or HTML.
12
+ #
13
+ # Each handler is attached to a registry object which stores correspondence
14
+ # between entities and their handlers. A handler is registered in a registry only when it is
15
+ # attached to an entity. Handler depend on entities but entities are not aware of handlers.
16
+ #
17
+ # Handlers are regular Ruby classes and all assumptions made about polymorphism, inheritance
18
+ # and incapsulation are true for them.
19
+ #
20
+ # Handler class itself is an abstract class which can't be instantiated directly.
21
+ class Handler
22
+ class << self
23
+ ##
24
+ # Render accessor
25
+ #
26
+ # Returns either processor of this handler class or processor of any ancestor.
27
+ def processor
28
+ @processor || (superclass.processor if superclass < Handler)
29
+ end
30
+
31
+ ##
32
+ # Register the hander class in a processor
33
+ #
34
+ # Registers the handler class in the processor
35
+ # * +r+ - processor registrar
36
+ def register_in(r)
37
+ @processor = r
38
+ self
39
+ end
40
+
41
+ ##
42
+ # Link the handler class to the entity
43
+ #
44
+ # Registers the link in the processor object of the handler class.
45
+ def for_entity(entity)
46
+ @entity = entity
47
+ processor.register(entity, self)
48
+ self
49
+ end
50
+
51
+ ##
52
+ # Entity the handler is registered for
53
+ attr_reader :entity
54
+ end
55
+
56
+ ##
57
+ # Context handler object is attached to
58
+ attr_reader :context
59
+
60
+ ##
61
+ # Assigned handler parameters
62
+ attr_reader :parameters
63
+
64
+ ##
65
+ # Execute in the context
66
+ #
67
+ # Executes passed block in the context. It helps against putting too much context-dependend
68
+ # method accesses in long blocks. Returns the value returned by the block.
69
+ def in_context(*args, &blk)
70
+ context.instance_exec(*args, &blk)
71
+ end
72
+
73
+ ##
74
+ # Create handler instance and link it to the context
75
+ def initialize(ctx, params = nil)
76
+ @context = ctx
77
+ @parameters = params
78
+ end
79
+
80
+ ##
81
+ # Alias for class method processor
82
+ def processor
83
+ self.class.processor
84
+ end
85
+
86
+ ##
87
+ # Process the object
88
+ #
89
+ # Processes passed object with the handler default processor
90
+ def processed_for(obj, parameters_override = nil)
91
+ processor.process(obj, parameters_override.nil? ? parameters : parameters_override)
92
+ end
93
+
94
+ ##
95
+ # Get handler for the entity
96
+ #
97
+ # Returns registered handler for the entity
98
+ def handler_for(entity)
99
+ processor.handler_for(entity)
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,74 @@
1
+ require 'aws/templates/utils'
2
+
3
+ module Aws
4
+ module Templates
5
+ module Processor
6
+ ##
7
+ # Handler registry
8
+ #
9
+ # Handler registries encapsulate differerent ways of transforming entities into
10
+ # domain-specific output. In nutshell, they are registries of Handler classes which are able
11
+ # to lookup proper Handler for a given entity.
12
+ class Registry
13
+ # Handler registry accessor
14
+ def registry
15
+ @registry ||= ::Concurrent::Map.new
16
+ end
17
+
18
+ ##
19
+ # Register pair entity-handler
20
+ #
21
+ # Invoked from inside of a Handler class at definition of the link between the handler class
22
+ # and an entity.
23
+ # * +entity+ - entity the handler claims to be able to process
24
+ # * +handler+ - handler class
25
+ def register(entity, handler)
26
+ registry.put_if_absent(_process_entity(entity), handler)
27
+ end
28
+
29
+ ##
30
+ # Try to look-up the handler
31
+ def handler_for(entity)
32
+ handler = self[entity]
33
+ raise "Handler is not found for #{entity}" unless handler
34
+ handler.reduce
35
+ end
36
+
37
+ ##
38
+ # Merge map with another recursive
39
+ def merge(recursive)
40
+ raise "#{recursive} is not recursive" unless Utils.recursive?(recursive)
41
+ recursive.keys.each { |k| register(k, recursive[k]) }
42
+ self
43
+ end
44
+
45
+ # All possible entity types
46
+ def keys
47
+ registry.keys
48
+ end
49
+
50
+ ##
51
+ # Look-up the handler
52
+ def [](entity)
53
+ return registry[entity] unless entity.is_a?(Module)
54
+ registry[entity.name] || registry[entity]
55
+ end
56
+
57
+ ##
58
+ # Check if handler exists
59
+ def include?(k)
60
+ !self[k].nil?
61
+ end
62
+
63
+ alias handler? include?
64
+
65
+ private
66
+
67
+ def _process_entity(entity)
68
+ return entity unless entity.is_a?(Module)
69
+ entity.name || entity.reduce
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,27 @@
1
+ require 'aws/templates/utils'
2
+
3
+ module Aws
4
+ module Templates
5
+ module Processor
6
+ ##
7
+ # Routing DSL
8
+ #
9
+ # Defines trivial methods to wire handlers with respective entities.
10
+ module Routing
11
+ ##
12
+ # Registry accessor
13
+ #
14
+ # All handlers and corresponding entities in a processor are stored in a registry.
15
+ def registry
16
+ @registry ||= Processor::Registry.new
17
+ end
18
+
19
+ ##
20
+ # Proxy for Registry register method
21
+ def register(*args)
22
+ registry.register(*args)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,6 +1,4 @@
1
- require 'aws/templates/exceptions'
2
- require 'aws/templates/render/registry'
3
- require 'aws/templates/render/view'
1
+ require 'aws/templates/utils'
4
2
 
5
3
  module Aws
6
4
  module Templates
@@ -29,31 +27,36 @@ module Aws
29
27
  # extend Aws::Templates::Render
30
28
  # end
31
29
  module Render
30
+ include Templates::Processor
31
+
32
32
  ##
33
- # Registry accessor
33
+ # Can object be rendered
34
34
  #
35
- # All views and corresponding artifacts in a render are stored
36
- # in a registry.
37
- def registry
38
- @registry ||= Registry.new
35
+ # Returns true if the object passed can be rendered by one of the views in the registry
36
+ def can_render?(instance)
37
+ instance.class.ancestors.any? { |ancestor| handler?(ancestor) }
39
38
  end
40
39
 
41
40
  ##
42
- # Proxy for Registry register method
43
- def register(*args)
44
- registry.register(*args)
45
- end
41
+ # Lookup a view for the artifact
42
+ #
43
+ # Searches registry for artifact's class and all its ancestors
44
+ # in the registry and returns the closest matching view
45
+ # * +instance+ - artifact instance to render
46
+ # * +params+ - assigned parameters; it can be arbitrary value;
47
+ # it is propagated to selected render
48
+ def view_for(instance, params = nil)
49
+ return instance if instance.respond_to?(:to_rendered)
46
50
 
47
- ##
48
- # Proxy for Registry can_render? method
49
- def can_render?(*args)
50
- registry.can_render?(*args)
51
+ ancestor = instance.class.ancestors.find { |mod| handler?(mod) }
52
+
53
+ raise Templates::Exception::ViewNotFound.new(instance) unless ancestor
54
+
55
+ handler_for(ancestor).new(instance, params)
51
56
  end
52
57
 
53
- ##
54
- # Proxy for Registry view_for method
55
- def view_for(*args)
56
- registry.view_for(*args)
58
+ def process(entity, params = nil)
59
+ view_for(entity, params).to_rendered
57
60
  end
58
61
 
59
62
  ##
@@ -61,12 +64,11 @@ module Aws
61
64
  #
62
65
  # Another way to define views for artifacts. Creates anonymous class and attaches as the view
63
66
  # to the specified artifact
64
- def define_view(artifact_class, view = nil, &blk)
65
- Class
66
- .new(view || Aws::Templates::Render::BasicView, &blk)
67
- .register_in(self)
68
- .artifact(artifact_class)
67
+ def define_handler(artifact_class, view = nil, &blk)
68
+ super(artifact_class, view || BasicView, &blk)
69
69
  end
70
+
71
+ alias define_view define_handler
70
72
  end
71
73
  end
72
74
  end
@@ -0,0 +1,47 @@
1
+ require 'aws/templates/utils'
2
+
3
+ module Aws
4
+ module Templates
5
+ module Render
6
+ ##
7
+ # Basic render view
8
+ #
9
+ # Views are classes encapsulating functionality of transforming
10
+ # artifacts into domain-specific output. For instance, the same
11
+ # LDAP record can be transformed into JSON description or LDIF definition.
12
+ # Views can be attached to ancestors of an artifact and it expected
13
+ # that the library will look-up the closest ancestor having view attached
14
+ # if the render is invoked on a child.
15
+ #
16
+ # Each view is attached to a registry object which stores correspondence
17
+ # between artifact classes and their views, and optionally to an artifact.
18
+ # A view is registered in a registry only when it is attached to an artifact.
19
+ # Views depend on artifacts but artifacts are not aware of views.
20
+ # As the extreme case, a sole view can be attached to Artifact
21
+ # if you have universal way to render your domain objects.
22
+ #
23
+ # Views are regular Ruby classes and all assumptions made about
24
+ # polymorphism, inheritance and incapsulation are true for them.
25
+ #
26
+ # View class itself is an abstract class which can't be instantiated
27
+ # directly.
28
+ class BasicView < Templates::Processor::Handler
29
+ class << self
30
+ alias artifact for_entity
31
+ end
32
+
33
+ alias instance context
34
+ alias in_instance in_context
35
+ alias rendered_for processed_for
36
+
37
+ ##
38
+ # Render the instance of the artifact
39
+ #
40
+ # The method should be overriden and return rendered form of the attached instance
41
+ def to_rendered
42
+ raise Templates::Exception::NotImplementedError.new('The method should be overriden')
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,14 @@
1
+ require 'aws/templates/utils'
2
+
3
+ module Aws
4
+ module Templates
5
+ module Render
6
+ ##
7
+ # Namespace for rendering utils
8
+ #
9
+ # Contains supplemental re-usable components which are not related to the core functionality.
10
+ module Utils
11
+ end
12
+ end
13
+ end
14
+ end