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.
- checksums.yaml +4 -4
- data/.rspec +4 -0
- data/.rubocop.yml +1 -0
- data/Rakefile +1 -1
- data/bin/cloud-templates-runner.rb +5 -0
- data/cloud-templates.gemspec +9 -4
- data/examples/lib/user_directory.rb +0 -10
- data/examples/lib/user_directory/artifacts.rb +8 -0
- data/examples/lib/user_directory/artifacts/catalogized.rb +8 -7
- data/examples/lib/user_directory/artifacts/group.rb +27 -30
- data/examples/lib/user_directory/artifacts/ided.rb +8 -7
- data/examples/lib/user_directory/artifacts/organization.rb +10 -11
- data/examples/lib/user_directory/artifacts/pathed.rb +17 -17
- data/examples/lib/user_directory/artifacts/person.rb +20 -15
- data/examples/lib/user_directory/artifacts/team.rb +26 -21
- data/examples/lib/user_directory/artifacts/unit.rb +15 -17
- data/examples/lib/user_directory/artifacts/user.rb +26 -23
- data/examples/lib/user_directory/render.rb +8 -0
- data/examples/lib/user_directory/render/etc.rb +2 -0
- data/examples/lib/user_directory/render/etc/artifact_view.rb +2 -2
- data/examples/lib/user_directory/render/etc/composite_view.rb +1 -2
- data/examples/lib/user_directory/render/etc/group_view.rb +3 -3
- data/examples/lib/user_directory/render/etc/person_view.rb +2 -2
- data/examples/lib/user_directory/render/etc/registry.rb +4 -6
- data/examples/lib/user_directory/render/etc/user_view.rb +2 -3
- data/examples/lib/user_directory/render/ldap.rb +2 -0
- data/examples/lib/user_directory/render/ldap/artifact_view.rb +3 -4
- data/examples/lib/user_directory/render/ldap/composite_view.rb +3 -4
- data/examples/lib/user_directory/render/ldap/group_view.rb +3 -4
- data/examples/lib/user_directory/render/ldap/organization_view.rb +3 -4
- data/examples/lib/user_directory/render/ldap/person_view.rb +3 -4
- data/examples/lib/user_directory/render/ldap/registry.rb +3 -4
- data/examples/lib/user_directory/render/ldap/unit_view.rb +3 -4
- data/examples/lib/user_directory/render/ldap/user_view.rb +3 -4
- data/examples/lib/user_directory/utils.rb +1 -1
- data/examples/spec/user_directory_spec.rb +24 -25
- data/lib/aws/templates.rb +0 -8
- data/lib/aws/templates/artifact.rb +20 -12
- data/lib/aws/templates/cli.rb +14 -0
- data/lib/aws/templates/cli/formatter.rb +21 -0
- data/lib/aws/templates/cli/formatter/json.rb +20 -0
- data/lib/aws/templates/cli/interface.rb +92 -0
- data/lib/aws/templates/composite.rb +6 -9
- data/lib/aws/templates/exception.rb +8 -0
- data/lib/aws/templates/exception/nested_parameter_exception.rb +19 -0
- data/lib/aws/templates/exception/option_error.rb +14 -0
- data/lib/aws/templates/exception/option_scalar_on_the_way.rb +26 -0
- data/lib/aws/templates/exception/option_should_be_recursive.rb +20 -0
- data/lib/aws/templates/exception/option_value_deleted.rb +22 -0
- data/lib/aws/templates/exception/parameter_already_exist.rb +26 -0
- data/lib/aws/templates/exception/parameter_exception.rb +26 -0
- data/lib/aws/templates/exception/parameter_getter_is_not_defined.rb +23 -0
- data/lib/aws/templates/exception/parameter_method_name_conflict.rb +26 -0
- data/lib/aws/templates/exception/parameter_specification_is_invalid.rb +30 -0
- data/lib/aws/templates/exception/parameter_value_invalid.rb +37 -0
- data/lib/aws/templates/exception/parametrized_dsl_error.rb +14 -0
- data/lib/aws/templates/exception/view_not_found.rb +26 -0
- data/lib/aws/templates/help.rb +43 -0
- data/lib/aws/templates/help/aggregate.rb +74 -0
- data/lib/aws/templates/help/dsl.rb +30 -0
- data/lib/aws/templates/help/provider.rb +15 -0
- data/lib/aws/templates/help/rdoc.rb +105 -0
- data/lib/aws/templates/help/rdoc/artifact.rb +50 -0
- data/lib/aws/templates/help/rdoc/composite.rb +20 -0
- data/lib/aws/templates/help/rdoc/contextualized.rb +12 -0
- data/lib/aws/templates/help/rdoc/contextualized/filter.rb +19 -0
- data/lib/aws/templates/help/rdoc/contextualized/filters.rb +14 -0
- data/lib/aws/templates/help/rdoc/contextualized/filters/add.rb +21 -0
- data/lib/aws/templates/help/rdoc/contextualized/filters/chain.rb +33 -0
- data/lib/aws/templates/help/rdoc/contextualized/filters/copy.rb +23 -0
- data/lib/aws/templates/help/rdoc/contextualized/filters/identity.rb +23 -0
- data/lib/aws/templates/help/rdoc/contextualized/filters/override.rb +29 -0
- data/lib/aws/templates/help/rdoc/contextualized/filters/proxy.rb +28 -0
- data/lib/aws/templates/help/rdoc/contextualized/filters/recursive_schema_filter.rb +28 -0
- data/lib/aws/templates/help/rdoc/contextualized/filters/remove.rb +21 -0
- data/lib/aws/templates/help/rdoc/contextualized/filters/scoped.rb +29 -0
- data/lib/aws/templates/help/rdoc/contextualized/provider.rb +28 -0
- data/lib/aws/templates/help/rdoc/default.rb +12 -0
- data/lib/aws/templates/help/rdoc/default/definition.rb +89 -0
- data/lib/aws/templates/help/rdoc/default/provider.rb +25 -0
- data/lib/aws/templates/help/rdoc/dsl.rb +20 -0
- data/lib/aws/templates/help/rdoc/inheritable.rb +48 -0
- data/lib/aws/templates/help/rdoc/parametrized.rb +12 -0
- data/lib/aws/templates/help/rdoc/parametrized/constraint.rb +31 -0
- data/lib/aws/templates/help/rdoc/parametrized/constraints.rb +14 -0
- data/lib/aws/templates/help/rdoc/parametrized/constraints/all_of.rb +39 -0
- data/lib/aws/templates/help/rdoc/parametrized/constraints/condition.rb +44 -0
- data/lib/aws/templates/help/rdoc/parametrized/constraints/depends_on_value.rb +41 -0
- data/lib/aws/templates/help/rdoc/parametrized/constraints/enum.rb +33 -0
- data/lib/aws/templates/help/rdoc/parametrized/constraints/matches.rb +27 -0
- data/lib/aws/templates/help/rdoc/parametrized/constraints/not_nil.rb +27 -0
- data/lib/aws/templates/help/rdoc/parametrized/constraints/requires.rb +33 -0
- data/lib/aws/templates/help/rdoc/parametrized/constraints/satisfies_condition.rb +28 -0
- data/lib/aws/templates/help/rdoc/parametrized/getter.rb +17 -0
- data/lib/aws/templates/help/rdoc/parametrized/getters.rb +14 -0
- data/lib/aws/templates/help/rdoc/parametrized/nested.rb +32 -0
- data/lib/aws/templates/help/rdoc/parametrized/parameter.rb +55 -0
- data/lib/aws/templates/help/rdoc/parametrized/provider.rb +32 -0
- data/lib/aws/templates/help/rdoc/parametrized/transformation.rb +17 -0
- data/lib/aws/templates/help/rdoc/parametrized/transformations.rb +14 -0
- data/lib/aws/templates/help/rdoc/parametrized/transformations/as_boolean.rb +25 -0
- data/lib/aws/templates/help/rdoc/parametrized/transformations/as_chain.rb +34 -0
- data/lib/aws/templates/help/rdoc/parametrized/transformations/as_hash.rb +40 -0
- data/lib/aws/templates/help/rdoc/parametrized/transformations/as_integer.rb +26 -0
- data/lib/aws/templates/help/rdoc/parametrized/transformations/as_list.rb +32 -0
- data/lib/aws/templates/help/rdoc/parametrized/transformations/as_module.rb +25 -0
- data/lib/aws/templates/help/rdoc/parametrized/transformations/as_object.rb +32 -0
- data/lib/aws/templates/help/rdoc/parametrized/transformations/as_rendered.rb +28 -0
- data/lib/aws/templates/help/rdoc/parametrized/transformations/as_string.rb +25 -0
- data/lib/aws/templates/help/rdoc/provider.rb +20 -0
- data/lib/aws/templates/help/rdoc/routing.rb +104 -0
- data/lib/aws/templates/help/rdoc/texting.rb +42 -0
- data/lib/aws/templates/processor.rb +53 -0
- data/lib/aws/templates/processor/handler.rb +104 -0
- data/lib/aws/templates/processor/registry.rb +74 -0
- data/lib/aws/templates/processor/routing.rb +27 -0
- data/lib/aws/templates/render.rb +27 -25
- data/lib/aws/templates/render/basic_view.rb +47 -0
- data/lib/aws/templates/render/utils.rb +14 -0
- data/lib/aws/templates/render/utils/base_type_views.rb +12 -19
- data/lib/aws/templates/render/utils/inspect.rb +2 -4
- data/lib/aws/templates/render/utils/stringify.rb +2 -5
- data/lib/aws/templates/render/view.rb +3 -100
- data/lib/aws/templates/utils.rb +39 -15
- data/lib/aws/templates/utils/artifact_storage.rb +2 -3
- data/lib/aws/templates/utils/{named.rb → as_named.rb} +2 -3
- data/lib/aws/templates/utils/autoload.rb +224 -0
- data/lib/aws/templates/utils/contextualized.rb +8 -17
- data/lib/aws/templates/utils/contextualized/filter.rb +62 -0
- data/lib/aws/templates/utils/contextualized/filter/add.rb +67 -0
- data/lib/aws/templates/utils/contextualized/filter/chain.rb +56 -0
- data/lib/aws/templates/utils/contextualized/filter/copy.rb +38 -0
- data/lib/aws/templates/utils/contextualized/filter/dsl.rb +50 -0
- data/lib/aws/templates/utils/contextualized/filter/identity.rb +45 -0
- data/lib/aws/templates/utils/contextualized/filter/override.rb +62 -0
- data/lib/aws/templates/utils/contextualized/filter/proxy.rb +29 -0
- data/lib/aws/templates/utils/contextualized/filter/recursive_schema_filter.rb +46 -0
- data/lib/aws/templates/utils/contextualized/filter/remove.rb +66 -0
- data/lib/aws/templates/utils/contextualized/filter/scoped.rb +42 -0
- data/lib/aws/templates/utils/contextualized/refinements.rb +51 -0
- data/lib/aws/templates/utils/default.rb +258 -50
- data/lib/aws/templates/utils/dependency.rb +7 -6
- data/lib/aws/templates/utils/dependency/refinements.rb +80 -0
- data/lib/aws/templates/utils/dependent.rb +6 -3
- data/lib/aws/templates/utils/guarded.rb +30 -0
- data/lib/aws/templates/utils/inheritable.rb +21 -16
- data/lib/aws/templates/utils/inspectable.rb +4 -4
- data/lib/aws/templates/utils/late_bound.rb +2 -2
- data/lib/aws/templates/utils/memoized.rb +2 -0
- data/lib/aws/templates/utils/options.rb +49 -35
- data/lib/aws/templates/utils/parametrized.rb +32 -25
- data/lib/aws/templates/utils/parametrized/constraint.rb +95 -0
- data/lib/aws/templates/utils/parametrized/constraint/all_of.rb +51 -0
- data/lib/aws/templates/utils/parametrized/constraint/condition.rb +93 -0
- data/lib/aws/templates/utils/parametrized/constraint/depends_on_value.rb +60 -0
- data/lib/aws/templates/utils/parametrized/constraint/dsl.rb +86 -0
- data/lib/aws/templates/utils/parametrized/constraint/enum.rb +45 -0
- data/lib/aws/templates/utils/parametrized/constraint/matches.rb +44 -0
- data/lib/aws/templates/utils/parametrized/constraint/not_nil.rb +38 -0
- data/lib/aws/templates/utils/parametrized/constraint/requires.rb +54 -0
- data/lib/aws/templates/utils/parametrized/constraint/satisfies_condition.rb +55 -0
- data/lib/aws/templates/utils/parametrized/getter.rb +67 -0
- data/lib/aws/templates/utils/parametrized/getter/as_instance_variable.rb +41 -0
- data/lib/aws/templates/utils/parametrized/getter/as_is.rb +37 -0
- data/lib/aws/templates/utils/parametrized/getter/dsl.rb +62 -0
- data/lib/aws/templates/utils/parametrized/getter/one_of.rb +48 -0
- data/lib/aws/templates/utils/parametrized/getter/path.rb +53 -0
- data/lib/aws/templates/utils/parametrized/getter/value.rb +50 -0
- data/lib/aws/templates/utils/parametrized/nested.rb +9 -16
- data/lib/aws/templates/utils/parametrized/transformation.rb +74 -0
- data/lib/aws/templates/utils/parametrized/transformation/as_boolean.rb +42 -0
- data/lib/aws/templates/utils/parametrized/transformation/as_chain.rb +57 -0
- data/lib/aws/templates/utils/parametrized/transformation/as_hash.rb +89 -0
- data/lib/aws/templates/utils/parametrized/transformation/as_integer.rb +39 -0
- data/lib/aws/templates/utils/parametrized/transformation/as_list.rb +97 -0
- data/lib/aws/templates/utils/parametrized/transformation/as_module.rb +42 -0
- data/lib/aws/templates/utils/parametrized/transformation/as_object.rb +76 -0
- data/lib/aws/templates/utils/parametrized/transformation/as_rendered.rb +100 -0
- data/lib/aws/templates/utils/parametrized/transformation/as_string.rb +39 -0
- data/lib/aws/templates/utils/parametrized/transformation/dsl.rb +94 -0
- data/lib/aws/templates/utils/recursive.rb +22 -0
- data/spec/aws/templates/artifact_spec.rb +10 -10
- data/spec/aws/templates/cli_spec.rb +83 -0
- data/spec/aws/templates/composite_spec.rb +6 -4
- data/spec/aws/templates/help/rdoc/artifact_spec.rb +94 -0
- data/spec/aws/templates/help/rdoc/composite_spec.rb +53 -0
- data/spec/aws/templates/help/rdoc/parametrized/constraints/all_of_spec.rb +17 -0
- data/spec/aws/templates/help/rdoc/parametrized/constraints/depends_on_value_spec.rb +21 -0
- data/spec/aws/templates/help/rdoc/parametrized/constraints/enum_spec.rb +17 -0
- data/spec/aws/templates/help/rdoc/parametrized/constraints/matches_spec.rb +17 -0
- data/spec/aws/templates/help/rdoc/parametrized/constraints/satisfies_condition_spec.rb +17 -0
- data/spec/aws/templates/help/rdoc/parametrized/transformations/as_boolean_spec.rb +17 -0
- data/spec/aws/templates/help/rdoc/parametrized/transformations/as_chain_spec.rb +17 -0
- data/spec/aws/templates/help/rdoc/parametrized/transformations/as_hash_spec.rb +72 -0
- data/spec/aws/templates/help/rdoc/parametrized/transformations/as_integer_spec.rb +17 -0
- data/spec/aws/templates/help/rdoc/parametrized/transformations/as_list_spec.rb +39 -0
- data/spec/aws/templates/help/rdoc/parametrized/transformations/as_module_spec.rb +17 -0
- data/spec/aws/templates/help/rdoc/parametrized/transformations/as_object_spec.rb +38 -0
- data/spec/aws/templates/help/rdoc/parametrized/transformations/as_rendered_spec.rb +17 -0
- data/spec/aws/templates/render/utils/base_type_views_spec.rb +9 -11
- data/spec/aws/templates/render_spec.rb +1 -1
- data/spec/aws/templates/utils/as_named_spec.rb +0 -2
- data/spec/aws/templates/utils/autoload_spec.rb +14 -0
- data/spec/aws/templates/utils/contextualized_spec.rb +5 -5
- data/spec/aws/templates/utils/late_bound_spec.rb +2 -2
- data/spec/aws/templates/utils/parametrized/constraint_spec.rb +8 -10
- data/spec/aws/templates/utils/parametrized/getters_spec.rb +6 -7
- data/spec/aws/templates/utils/parametrized/transformation_spec.rb +3 -6
- data/spec/aws/templates/utils/parametrized_spec.rb +9 -10
- data/spec/aws/templates/utils_spec.rb +18 -0
- data/spec/test_empty/stuff.rb +4 -0
- data/spec/test_empty/stuff/here/test.rb +9 -0
- metadata +235 -19
- data/lib/aws/templates/exceptions.rb +0 -221
- data/lib/aws/templates/render/registry.rb +0 -60
- data/lib/aws/templates/utils/contextualized/filters.rb +0 -437
- data/lib/aws/templates/utils/contextualized/hash.rb +0 -13
- data/lib/aws/templates/utils/contextualized/nil.rb +0 -13
- data/lib/aws/templates/utils/contextualized/proc.rb +0 -13
- data/lib/aws/templates/utils/dependency/enumerable.rb +0 -13
- data/lib/aws/templates/utils/dependency/object.rb +0 -46
- data/lib/aws/templates/utils/parametrized/constraints.rb +0 -423
- data/lib/aws/templates/utils/parametrized/getters.rb +0 -293
- data/lib/aws/templates/utils/parametrized/guarded.rb +0 -32
- data/lib/aws/templates/utils/parametrized/transformations.rb +0 -652
@@ -1,293 +0,0 @@
|
|
1
|
-
require 'aws/templates/exceptions'
|
2
|
-
require 'aws/templates/utils/parametrized'
|
3
|
-
require 'singleton'
|
4
|
-
|
5
|
-
module Aws
|
6
|
-
module Templates
|
7
|
-
module Utils
|
8
|
-
module Parametrized #:nodoc:
|
9
|
-
##
|
10
|
-
# Getter functor class
|
11
|
-
#
|
12
|
-
# A getter is a Proc without parameters and it is expected to return
|
13
|
-
# a value. Since the proc is to be executed in instance context
|
14
|
-
# the value can be calculated based on other methods or extracted from
|
15
|
-
# options attrribute
|
16
|
-
#
|
17
|
-
# The class implements functor pattern through to_proc method and
|
18
|
-
# closure. Essentially, all getters can be used everywhere where
|
19
|
-
# a block is expected.
|
20
|
-
#
|
21
|
-
# It provides protected method get which should be overriden in
|
22
|
-
# all concrete getter classes.
|
23
|
-
class Getter
|
24
|
-
##
|
25
|
-
# Get parameter from instance variables as is
|
26
|
-
#
|
27
|
-
# Gets value from instance variable by parameter's name without
|
28
|
-
# any other operations performed.
|
29
|
-
#
|
30
|
-
# === Example
|
31
|
-
#
|
32
|
-
# class Piece
|
33
|
-
# include Aws::Templates::Utils::Parametrized
|
34
|
-
#
|
35
|
-
# parameter :param1, getter: as_instance_variable
|
36
|
-
#
|
37
|
-
# def initialize(x)
|
38
|
-
# @param1 = x
|
39
|
-
# end
|
40
|
-
# end
|
41
|
-
#
|
42
|
-
# i = Piece.new(3)
|
43
|
-
# i.param1 # => 3
|
44
|
-
class AsInstanceVariable < Getter
|
45
|
-
include Singleton
|
46
|
-
|
47
|
-
protected
|
48
|
-
|
49
|
-
def get(parameter, instance)
|
50
|
-
instance.instance_variable_get("@#{parameter.name}")
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
##
|
55
|
-
# Get options value "as is"
|
56
|
-
#
|
57
|
-
# Gets value from options attribute by parameter's name without
|
58
|
-
# any other operations performed.
|
59
|
-
#
|
60
|
-
# === Example
|
61
|
-
#
|
62
|
-
# class Piece
|
63
|
-
# include Aws::Templates::Utils::Parametrized
|
64
|
-
#
|
65
|
-
# parameter :param1, :getter => as_is
|
66
|
-
# end
|
67
|
-
#
|
68
|
-
# i = Piece.new(:param1 => 3)
|
69
|
-
# i.param1 # => 3
|
70
|
-
class AsIs < Getter
|
71
|
-
include Singleton
|
72
|
-
|
73
|
-
protected
|
74
|
-
|
75
|
-
def get(parameter, instance)
|
76
|
-
instance.options[parameter.name]
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
##
|
81
|
-
# Lookup value in options by path
|
82
|
-
#
|
83
|
-
# Looks up value from options attribute by specified path. The path
|
84
|
-
# can be either statically specified or a block can be provided.
|
85
|
-
# The block shouldn't have parameters and should return an array
|
86
|
-
# containing path. The block will be executed in the instance context.
|
87
|
-
#
|
88
|
-
# === Example
|
89
|
-
#
|
90
|
-
# class Piece
|
91
|
-
# include Aws::Templates::Utils::Parametrized
|
92
|
-
#
|
93
|
-
# parameter :param1, :getter => path(:a, :b)
|
94
|
-
# end
|
95
|
-
#
|
96
|
-
# i = Piece.new(:a => { :b => 3 })
|
97
|
-
# i.param1 # => 3
|
98
|
-
class Path < Getter
|
99
|
-
attr_reader :path
|
100
|
-
|
101
|
-
def initialize(path)
|
102
|
-
unless path.respond_to?(:to_proc) || path.respond_to?(:to_a)
|
103
|
-
raise ArgumentError.new(
|
104
|
-
"Path can be either array or Proc: #{path.inspect}"
|
105
|
-
)
|
106
|
-
end
|
107
|
-
|
108
|
-
@path = path
|
109
|
-
end
|
110
|
-
|
111
|
-
protected
|
112
|
-
|
113
|
-
def get(_, instance)
|
114
|
-
if path.respond_to?(:to_proc)
|
115
|
-
instance.options[*instance.instance_eval(&path)]
|
116
|
-
elsif path.respond_to?(:to_a)
|
117
|
-
instance.options[*path]
|
118
|
-
end
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
##
|
123
|
-
# Calculate value
|
124
|
-
#
|
125
|
-
# If a block is specified, it will be executed in the instance
|
126
|
-
# context and return will be used as parameter value. If a value
|
127
|
-
# specified then it will be used as parameter value instead.
|
128
|
-
#
|
129
|
-
# === Example
|
130
|
-
#
|
131
|
-
# class Piece
|
132
|
-
# include Aws::Templates::Utils::Parametrized
|
133
|
-
#
|
134
|
-
# parameter :param1, :getter => value(1)
|
135
|
-
# parameter :param2, :getter => value { options[:z] + 1 }
|
136
|
-
# end
|
137
|
-
#
|
138
|
-
# i = Piece.new(:z => 3)
|
139
|
-
# i.param2 # => 4
|
140
|
-
# i.param1 # => 1
|
141
|
-
class Value < Getter
|
142
|
-
attr_reader :calculation
|
143
|
-
|
144
|
-
def initialize(calculation)
|
145
|
-
@calculation = calculation
|
146
|
-
end
|
147
|
-
|
148
|
-
protected
|
149
|
-
|
150
|
-
def get(_, instance)
|
151
|
-
if calculation.respond_to?(:to_hash)
|
152
|
-
calculation
|
153
|
-
elsif calculation.respond_to?(:to_proc)
|
154
|
-
instance.instance_eval(&calculation)
|
155
|
-
else
|
156
|
-
calculation
|
157
|
-
end
|
158
|
-
end
|
159
|
-
end
|
160
|
-
|
161
|
-
##
|
162
|
-
# Pick one of non-nil values returned by nested getters
|
163
|
-
#
|
164
|
-
# In general it plays the same role as || operator in Ruby. It
|
165
|
-
# just picks first non-nil value returned by a list of getters
|
166
|
-
#
|
167
|
-
# === Example
|
168
|
-
#
|
169
|
-
# class Piece
|
170
|
-
# include Aws::Templates::Utils::Parametrized
|
171
|
-
#
|
172
|
-
# parameter :param1, :getter => one_of(
|
173
|
-
# path(:a, :b),
|
174
|
-
# path(:b, :c)
|
175
|
-
# )
|
176
|
-
# end
|
177
|
-
#
|
178
|
-
# i = Piece.new( :a => { :b => 3 } )
|
179
|
-
# i.param1 # => 3
|
180
|
-
# i = Piece.new( :b => { :c => 4 } )
|
181
|
-
# i.param1 # => 4
|
182
|
-
class OneOf < Getter
|
183
|
-
attr_reader :getters
|
184
|
-
|
185
|
-
def initialize(getters)
|
186
|
-
@getters = getters
|
187
|
-
end
|
188
|
-
|
189
|
-
protected
|
190
|
-
|
191
|
-
def get(parameter, instance)
|
192
|
-
getters.inject(nil) do |value, g|
|
193
|
-
value = instance.instance_exec(parameter, &g)
|
194
|
-
break value unless value.nil?
|
195
|
-
end
|
196
|
-
end
|
197
|
-
end
|
198
|
-
|
199
|
-
##
|
200
|
-
# Creates closure with getter invocation
|
201
|
-
#
|
202
|
-
# It's an interface method required for Getter to expose
|
203
|
-
# functor properties. It encloses invocation of Getter get_wrapper
|
204
|
-
# method into a closure. The closure itself is executed in the context
|
205
|
-
# of Parametrized instance which provides proper set "self" variable.
|
206
|
-
#
|
207
|
-
# The closure itself accepts 1 parameters
|
208
|
-
# * +parameter+ - the Parameter object which the getter is executed for
|
209
|
-
# ...where instance is assumed from self
|
210
|
-
def to_proc
|
211
|
-
getter = self
|
212
|
-
|
213
|
-
lambda do |parameter|
|
214
|
-
getter.get_wrapper(parameter, self)
|
215
|
-
end
|
216
|
-
end
|
217
|
-
|
218
|
-
##
|
219
|
-
# Wraps getter-dependent method
|
220
|
-
#
|
221
|
-
# It wraps constraint-dependent "get" method into a rescue block
|
222
|
-
# to standardize exception type and information provided by failed
|
223
|
-
# value calculation
|
224
|
-
# * +parameter+ - the Parameter object which the getter is executed for
|
225
|
-
# * +instance+ - the instance value is taken from
|
226
|
-
def get_wrapper(parameter, instance)
|
227
|
-
get(parameter, instance)
|
228
|
-
rescue StandardError
|
229
|
-
raise NestedParameterException.new(parameter)
|
230
|
-
end
|
231
|
-
|
232
|
-
protected
|
233
|
-
|
234
|
-
##
|
235
|
-
# Getter method
|
236
|
-
#
|
237
|
-
# * +parameter+ - the Parameter object which the getter is executed for
|
238
|
-
# * +instance+ - the instance value is taken from
|
239
|
-
def get(parameter, instance); end
|
240
|
-
end
|
241
|
-
|
242
|
-
##
|
243
|
-
# Syntax sugar for getters definition
|
244
|
-
#
|
245
|
-
# It injects the methods as class-scope methods into mixing classes.
|
246
|
-
# The methods are factories to create particular type of getter
|
247
|
-
class_scope do
|
248
|
-
##
|
249
|
-
# Get parameter from instance variables as is
|
250
|
-
#
|
251
|
-
# alias for AsInstanceVariable class
|
252
|
-
def as_instance_variable
|
253
|
-
Getter::AsInstanceVariable.instance
|
254
|
-
end
|
255
|
-
|
256
|
-
##
|
257
|
-
# Get parameter from Options as is
|
258
|
-
#
|
259
|
-
# alias for AsIs class
|
260
|
-
def as_is
|
261
|
-
Getter::AsIs.instance
|
262
|
-
end
|
263
|
-
|
264
|
-
##
|
265
|
-
# Calculate value of parameter
|
266
|
-
#
|
267
|
-
# alias for Value class
|
268
|
-
def value(v = nil, &blk)
|
269
|
-
Getter::Value.new(v.nil? ? blk : v)
|
270
|
-
end
|
271
|
-
|
272
|
-
##
|
273
|
-
# Look up value of the parameter with path
|
274
|
-
#
|
275
|
-
# alias for Path class
|
276
|
-
def path(*v, &blk)
|
277
|
-
Getter::Path.new(
|
278
|
-
v.empty? ? blk : v
|
279
|
-
)
|
280
|
-
end
|
281
|
-
|
282
|
-
##
|
283
|
-
# Choose one non-nil value from nested getters
|
284
|
-
#
|
285
|
-
# alias for OneOf class
|
286
|
-
def one_of(*getters)
|
287
|
-
Getter::OneOf.new(getters)
|
288
|
-
end
|
289
|
-
end
|
290
|
-
end
|
291
|
-
end
|
292
|
-
end
|
293
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
require 'set'
|
2
|
-
|
3
|
-
module Aws
|
4
|
-
module Templates
|
5
|
-
module Utils
|
6
|
-
module Parametrized
|
7
|
-
##
|
8
|
-
# Remember Alan Turing's halting problem and don't believe in miracles. The method will
|
9
|
-
# only work with parameters because they are supposed to be pure unmodifying functions.
|
10
|
-
# Hence we can terminate if a parameter method was invoked twice in the stack with the same
|
11
|
-
# context.
|
12
|
-
module Guarded
|
13
|
-
Call = Struct.new(:instance, :parameter)
|
14
|
-
|
15
|
-
def guarded_get(instance, parameter_object)
|
16
|
-
current_call = Call.new(instance, parameter_object)
|
17
|
-
return unless trace.add?(current_call)
|
18
|
-
ret = parameter_object.get(self)
|
19
|
-
trace.delete(current_call)
|
20
|
-
ret
|
21
|
-
end
|
22
|
-
|
23
|
-
private
|
24
|
-
|
25
|
-
def trace
|
26
|
-
Thread.current[Guarded.name] ||= Set.new
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
@@ -1,652 +0,0 @@
|
|
1
|
-
require 'aws/templates/exceptions'
|
2
|
-
require 'aws/templates/utils/parametrized'
|
3
|
-
require 'aws/templates/utils/parametrized/nested'
|
4
|
-
require 'singleton'
|
5
|
-
|
6
|
-
module Aws
|
7
|
-
module Templates
|
8
|
-
module Utils
|
9
|
-
module Parametrized #:nodoc:
|
10
|
-
##
|
11
|
-
# Transformation functor class
|
12
|
-
#
|
13
|
-
# A transformation is a Proc accepting input value and providing output
|
14
|
-
# value which is expected to be a transformation of the input.
|
15
|
-
# The proc is executed in instance context so instance methods can
|
16
|
-
# be used for calculation.
|
17
|
-
#
|
18
|
-
# The class implements functor pattern through to_proc method and
|
19
|
-
# closure. Essentially, all transformations can be used everywhere where
|
20
|
-
# a block is expected.
|
21
|
-
#
|
22
|
-
# It provides protected method transform which should be overriden in
|
23
|
-
# all concrete transformation classes.
|
24
|
-
class Transformation
|
25
|
-
##
|
26
|
-
# Apply several transformation sequentially
|
27
|
-
#
|
28
|
-
# Useful when a few transformations need to be applied to a single value to get
|
29
|
-
# the final result
|
30
|
-
#
|
31
|
-
# === Example
|
32
|
-
#
|
33
|
-
# class Piece
|
34
|
-
# include Aws::Templates::Utils::Parametrized
|
35
|
-
#
|
36
|
-
# parameter :param,
|
37
|
-
# transform: as_chain(
|
38
|
-
# as_hash,
|
39
|
-
# as_object(Aws::Templates::Utils::AsNamed)
|
40
|
-
# )
|
41
|
-
# end
|
42
|
-
#
|
43
|
-
# i = Piece.new(param: [:name, 'Rex'])
|
44
|
-
# i.param.name # => 'Rex'
|
45
|
-
class AsChain < Transformation
|
46
|
-
attr_reader :components
|
47
|
-
|
48
|
-
def initialize(components)
|
49
|
-
@components = _check_components(components)
|
50
|
-
end
|
51
|
-
|
52
|
-
protected
|
53
|
-
|
54
|
-
def transform(parameter, value, instance)
|
55
|
-
return if value.nil?
|
56
|
-
components.inject(value) { |acc, elem| instance.instance_exec(parameter, acc, &elem) }
|
57
|
-
end
|
58
|
-
|
59
|
-
private
|
60
|
-
|
61
|
-
def _check_components(components)
|
62
|
-
result = components.to_a
|
63
|
-
|
64
|
-
invalid_components = result.reject { |component| component.respond_to?(:to_proc) }
|
65
|
-
raise "Invalid components: #{invalid_components}" unless invalid_components.empty?
|
66
|
-
|
67
|
-
result
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
##
|
72
|
-
# Transform input value into the object
|
73
|
-
#
|
74
|
-
# Input value can be either hash or object. The transformation performs
|
75
|
-
# nested object evaluation recusrsively as the input were Parametrized
|
76
|
-
# instance. As a parameter for the transformation you can
|
77
|
-
# specify either Module which mixes in Parametrized or to use
|
78
|
-
# a block which is to be evaluated as a part of Parametrized definition
|
79
|
-
# or both.
|
80
|
-
#
|
81
|
-
# With as_object transformation you can have as many nested levels
|
82
|
-
# as it's needed.
|
83
|
-
#
|
84
|
-
# === Example
|
85
|
-
#
|
86
|
-
# class Piece
|
87
|
-
# include Aws::Templates::Utils::Parametrized
|
88
|
-
#
|
89
|
-
# parameter :param1,
|
90
|
-
# :transform => as_object(Aws::Templates::Utils::AsNamed)
|
91
|
-
# parameter :param2, :transform => as_object {
|
92
|
-
# parameter :id, :description => 'Just ID',
|
93
|
-
# :constraint => not_nil
|
94
|
-
# }
|
95
|
-
# parameter :param3,
|
96
|
-
# :transform => as_object(Aws::Templates::Utils::AsNamed) {
|
97
|
-
# parameter :path, :description => 'Just path',
|
98
|
-
# :constraint => not_nil
|
99
|
-
# }
|
100
|
-
# end
|
101
|
-
#
|
102
|
-
# i = Piece.new
|
103
|
-
# i.param1 # => nil
|
104
|
-
# i = Piece.new(:param1 => {:name => 'Zed'})
|
105
|
-
# i.param1.name # => 'Zed'
|
106
|
-
# i = Piece.new(:param2 => {:id => 123})
|
107
|
-
# i.param2.id # => 123
|
108
|
-
# i = Piece.new(:param3 => {:path => 'a/b', :name => 'Rex'})
|
109
|
-
# i.param3.path # => 'a/b'
|
110
|
-
# i.param3.name # => 123
|
111
|
-
class AsObject < Transformation
|
112
|
-
attr_reader :klass
|
113
|
-
|
114
|
-
def initialize(scope, klass = nil, &definition)
|
115
|
-
@klass = if klass.nil?
|
116
|
-
Nested.create_class(scope)
|
117
|
-
elsif klass.is_a?(Class)
|
118
|
-
klass
|
119
|
-
elsif klass.is_a?(Module)
|
120
|
-
Nested.create_class(scope).with(klass)
|
121
|
-
else
|
122
|
-
raise "#{klass} is neither a class nor a module"
|
123
|
-
end
|
124
|
-
|
125
|
-
@klass.class_eval(&definition) unless definition.nil?
|
126
|
-
end
|
127
|
-
|
128
|
-
protected
|
129
|
-
|
130
|
-
def transform(_, value, instance)
|
131
|
-
return if value.nil?
|
132
|
-
klass.new(instance, value)
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
##
|
137
|
-
# Transform input value into a list
|
138
|
-
#
|
139
|
-
# Input value can be either an array or something which implements
|
140
|
-
# to_a method standard semantics. Each list entry is evaluated
|
141
|
-
# with specified constraints and transformations.
|
142
|
-
#
|
143
|
-
# With as_list transformation you can have as many nested levels
|
144
|
-
# as it's needed in terms of nested lists or nested objects.
|
145
|
-
#
|
146
|
-
# === Example
|
147
|
-
#
|
148
|
-
# class Piece
|
149
|
-
# include Aws::Templates::Utils::Parametrized
|
150
|
-
#
|
151
|
-
# parameter :param1, :transform => as_list(
|
152
|
-
# # alias for all elements. Plays a role during introspection
|
153
|
-
# :name => :element,
|
154
|
-
# # description of what the element represents
|
155
|
-
# :description => 'List element',
|
156
|
-
# # constraint for list element
|
157
|
-
# :constraint => not_nil
|
158
|
-
# )
|
159
|
-
# parameter :param2, :transform => as_list(
|
160
|
-
# :name => :element,
|
161
|
-
# :description => 'List element',
|
162
|
-
# :transform => as_list( # nested list
|
163
|
-
# :name => :sub_element,
|
164
|
-
# :description => 'Sub-list element',
|
165
|
-
# :constraint => not_nil
|
166
|
-
# )
|
167
|
-
# )
|
168
|
-
# parameter :param3, :transform => as_list(
|
169
|
-
# :name => :particle,
|
170
|
-
# :description => 'Small particle',
|
171
|
-
# :transform => as_object( # nested object
|
172
|
-
# Aws::Templates::Utils::AsNamed
|
173
|
-
# )
|
174
|
-
# )
|
175
|
-
# end
|
176
|
-
#
|
177
|
-
# i = Piece.new(:param1 => [1,2,3])
|
178
|
-
# i.param1 # => [1,2,3]
|
179
|
-
# i = Piece.new(:param1 => [1,nil,3])
|
180
|
-
# i.param1 # throws exception
|
181
|
-
# i = Piece.new(:param2 => [[1],[2],[3]])
|
182
|
-
# i.param2 # => [[1],[2],[3]]
|
183
|
-
# i = Piece.new(:param2 => [1,[2],[3]])
|
184
|
-
# i.param2 # throws exception
|
185
|
-
# i = Piece.new(:param2 => [[1],[nil],[3]])
|
186
|
-
# i.param2 # throws exception
|
187
|
-
# i = Piece.new(:param3 => [{:name => 'Zed'}])
|
188
|
-
# i.param3.first.name # => 'Zed'
|
189
|
-
class AsList < Transformation
|
190
|
-
attr_reader :sub_parameter
|
191
|
-
|
192
|
-
def initialize(klass = nil, options = nil)
|
193
|
-
return if options.nil?
|
194
|
-
|
195
|
-
@sub_parameter = Parameter.new(
|
196
|
-
options[:name],
|
197
|
-
klass,
|
198
|
-
description: options[:description],
|
199
|
-
transform: options[:transform],
|
200
|
-
constraint: options[:constraint]
|
201
|
-
)
|
202
|
-
end
|
203
|
-
|
204
|
-
protected
|
205
|
-
|
206
|
-
def transform(parameter, value, instance)
|
207
|
-
return if value.nil?
|
208
|
-
|
209
|
-
unless value.respond_to?(:to_a)
|
210
|
-
raise "#{parameter.name} is assigned to " \
|
211
|
-
"#{value.inspect} which is not a list"
|
212
|
-
end
|
213
|
-
|
214
|
-
if sub_parameter
|
215
|
-
value.to_a.map { |el| sub_parameter.process_value(instance, el) }
|
216
|
-
else
|
217
|
-
value.to_a
|
218
|
-
end
|
219
|
-
end
|
220
|
-
end
|
221
|
-
|
222
|
-
##
|
223
|
-
# Transform value with the specified render
|
224
|
-
#
|
225
|
-
# Input value can be anything which could be rendered by the
|
226
|
-
# specified render type. Returned value is rendered input.
|
227
|
-
#
|
228
|
-
# The transformation is useful when you have a document of some
|
229
|
-
# format embedded into a document of another format. An example
|
230
|
-
# could be Bash scripts embedded into AWS CFN template.
|
231
|
-
#
|
232
|
-
# === Example
|
233
|
-
#
|
234
|
-
# class Brush
|
235
|
-
# attr_reader :color
|
236
|
-
# attr_reader :thickness
|
237
|
-
# attr_reader :type
|
238
|
-
#
|
239
|
-
# def initialize(c, thick, t)
|
240
|
-
# @c = c
|
241
|
-
# @thick = thick
|
242
|
-
# @t = t
|
243
|
-
# end
|
244
|
-
# end
|
245
|
-
#
|
246
|
-
# class Circle
|
247
|
-
# attr_reader :radius
|
248
|
-
# attr_reader :brush
|
249
|
-
#
|
250
|
-
# def initialize(r, b)
|
251
|
-
# @radius = r
|
252
|
-
# @brush = b
|
253
|
-
# end
|
254
|
-
# end
|
255
|
-
#
|
256
|
-
# class Piece
|
257
|
-
# include Aws::Templates::Utils::Parametrized
|
258
|
-
#
|
259
|
-
# parameter :picture, :transform => as_rendered(
|
260
|
-
# # Render Type
|
261
|
-
# Graphics::Renders::JPEG,
|
262
|
-
# # parameter section for the render
|
263
|
-
# format: :base64
|
264
|
-
# )
|
265
|
-
# end
|
266
|
-
#
|
267
|
-
# i = Piece.new(picture: Circle.new(10, Brush.new(:red, 2, :dots)))
|
268
|
-
# i.picture # => <rendered representation>
|
269
|
-
class AsRendered < Transformation
|
270
|
-
attr_reader :type
|
271
|
-
attr_reader :parameters
|
272
|
-
|
273
|
-
def initialize(render_type, params)
|
274
|
-
@type = _check_render_type(render_type)
|
275
|
-
@parameters = params
|
276
|
-
end
|
277
|
-
|
278
|
-
protected
|
279
|
-
|
280
|
-
def transform(_, value, instance)
|
281
|
-
return if value.nil?
|
282
|
-
type.view_for(value, _compute_render_parameters(instance)).to_rendered
|
283
|
-
end
|
284
|
-
|
285
|
-
private
|
286
|
-
|
287
|
-
def _check_render_type(render_type)
|
288
|
-
unless render_type.respond_to?(:view_for)
|
289
|
-
raise(
|
290
|
-
"Wrong render type object #{params}. " \
|
291
|
-
'The instance should have #view_for method.'
|
292
|
-
)
|
293
|
-
end
|
294
|
-
|
295
|
-
render_type
|
296
|
-
end
|
297
|
-
|
298
|
-
def _compute_render_parameters(instance)
|
299
|
-
return if parameters.nil?
|
300
|
-
|
301
|
-
if parameters.respond_to?(:to_hash)
|
302
|
-
parameters
|
303
|
-
elsif parameters.respond_to?(:to_proc)
|
304
|
-
instance.instance_exec(¶meters)
|
305
|
-
else
|
306
|
-
parameters
|
307
|
-
end
|
308
|
-
end
|
309
|
-
end
|
310
|
-
|
311
|
-
##
|
312
|
-
# Convert input into integer
|
313
|
-
#
|
314
|
-
# Input value can be anything implementing :to_i method.
|
315
|
-
#
|
316
|
-
# === Example
|
317
|
-
#
|
318
|
-
# class Piece
|
319
|
-
# include Aws::Templates::Utils::Parametrized
|
320
|
-
#
|
321
|
-
# parameter :param, :transform => as_integer
|
322
|
-
# end
|
323
|
-
#
|
324
|
-
# i = Piece.new
|
325
|
-
# i.param # => nil
|
326
|
-
# i = Piece.new(:param => '23')
|
327
|
-
# i.param # => 23
|
328
|
-
class AsInteger < Transformation
|
329
|
-
include Singleton
|
330
|
-
|
331
|
-
protected
|
332
|
-
|
333
|
-
def transform(_, value, _)
|
334
|
-
return if value.nil?
|
335
|
-
Integer(value)
|
336
|
-
end
|
337
|
-
end
|
338
|
-
|
339
|
-
##
|
340
|
-
# Convert input into string
|
341
|
-
#
|
342
|
-
# Input value can be anything implementing :to_s method.
|
343
|
-
#
|
344
|
-
# === Example
|
345
|
-
#
|
346
|
-
# class Piece
|
347
|
-
# include Aws::Templates::Utils::Parametrized
|
348
|
-
#
|
349
|
-
# parameter :param, :transform => as_string
|
350
|
-
# end
|
351
|
-
#
|
352
|
-
# i = Piece.new
|
353
|
-
# i.param # => nil
|
354
|
-
# i = Piece.new(:param => 23)
|
355
|
-
# i.param # => '23'
|
356
|
-
class AsString < Transformation
|
357
|
-
include Singleton
|
358
|
-
|
359
|
-
protected
|
360
|
-
|
361
|
-
def transform(_, value, _)
|
362
|
-
return if value.nil?
|
363
|
-
String(value)
|
364
|
-
end
|
365
|
-
end
|
366
|
-
|
367
|
-
##
|
368
|
-
# Convert input into boolean
|
369
|
-
#
|
370
|
-
# Input value can be anything implementing :to_s method. Value considered false if it is:
|
371
|
-
# * +'false' as a string+
|
372
|
-
# * +FalseClass+
|
373
|
-
# Otherwise, value is true. If value is nil, it won't be replaced by "false"
|
374
|
-
#
|
375
|
-
# === Example
|
376
|
-
#
|
377
|
-
# class Piece
|
378
|
-
# include Aws::Templates::Utils::Parametrized
|
379
|
-
#
|
380
|
-
# parameter :param, :transform => as_boolean
|
381
|
-
# end
|
382
|
-
#
|
383
|
-
# i = Piece.new
|
384
|
-
# i.param # => false
|
385
|
-
# i = Piece.new(:param => 0)
|
386
|
-
# i.param # => true
|
387
|
-
class AsBoolean < Transformation
|
388
|
-
include Singleton
|
389
|
-
|
390
|
-
protected
|
391
|
-
|
392
|
-
def transform(_, value, _)
|
393
|
-
return if value.nil?
|
394
|
-
!value.to_s.casecmp('false').zero?
|
395
|
-
end
|
396
|
-
end
|
397
|
-
|
398
|
-
##
|
399
|
-
# Convert input into hash
|
400
|
-
#
|
401
|
-
# Input value can be anything implementing :to_hash method.
|
402
|
-
#
|
403
|
-
# === Example
|
404
|
-
#
|
405
|
-
# class Piece
|
406
|
-
# include Aws::Templates::Utils::Parametrized
|
407
|
-
#
|
408
|
-
# parameter :param, :transform => as_hash
|
409
|
-
# parameter :param2,
|
410
|
-
# transform: as_hash {
|
411
|
-
# value name: :number,
|
412
|
-
# description: 'Number',
|
413
|
-
# constraint: not_nil,
|
414
|
-
# transform: as_integer
|
415
|
-
# }
|
416
|
-
# end
|
417
|
-
#
|
418
|
-
# i = Piece.new
|
419
|
-
# i.param # => nil
|
420
|
-
# i = Piece.new(:param => [[1,2]])
|
421
|
-
# i.param # => {1=>2}
|
422
|
-
# i = Piece.new(:param2 => [[1,'3']])
|
423
|
-
# i.param # => {1=>3}
|
424
|
-
class AsHash < Transformation
|
425
|
-
include Parametrized.class_scope
|
426
|
-
|
427
|
-
def key(opts)
|
428
|
-
@key_parameter = _create_parameter(opts)
|
429
|
-
end
|
430
|
-
|
431
|
-
def value(opts)
|
432
|
-
@value_parameter = _create_parameter(opts)
|
433
|
-
end
|
434
|
-
|
435
|
-
def initialize(klass = nil, &blk)
|
436
|
-
@klass = klass
|
437
|
-
instance_eval(&blk) if blk
|
438
|
-
end
|
439
|
-
|
440
|
-
protected
|
441
|
-
|
442
|
-
def transform(_, value, instance)
|
443
|
-
return if value.nil?
|
444
|
-
|
445
|
-
Hash[
|
446
|
-
Hash[value].map do |k, v|
|
447
|
-
[
|
448
|
-
_process_value(@key_parameter, instance, k),
|
449
|
-
_process_value(@value_parameter, instance, v)
|
450
|
-
]
|
451
|
-
end
|
452
|
-
]
|
453
|
-
end
|
454
|
-
|
455
|
-
def _process_value(parameter, instance, value)
|
456
|
-
return value if parameter.nil?
|
457
|
-
parameter.process_value(instance, value)
|
458
|
-
end
|
459
|
-
|
460
|
-
private
|
461
|
-
|
462
|
-
def _create_parameter(opts)
|
463
|
-
Parameter.new(
|
464
|
-
opts[:name],
|
465
|
-
@klass,
|
466
|
-
description: opts[:description],
|
467
|
-
transform: opts[:transform],
|
468
|
-
constraint: opts[:constraint]
|
469
|
-
)
|
470
|
-
end
|
471
|
-
end
|
472
|
-
|
473
|
-
##
|
474
|
-
# Convert to a Ruby class
|
475
|
-
#
|
476
|
-
# The transformation allows to use elements of metaprogramming in the framework. It
|
477
|
-
# tries to transform passed value to a Ruby class.
|
478
|
-
#
|
479
|
-
# === Example
|
480
|
-
#
|
481
|
-
# class Piece
|
482
|
-
# include Aws::Templates::Utils::Parametrized
|
483
|
-
#
|
484
|
-
# parameter :param, :transform => as_module
|
485
|
-
# end
|
486
|
-
#
|
487
|
-
# i = Piece.new
|
488
|
-
# i.param # => nil
|
489
|
-
# i = Piece.new(:param => 'Object')
|
490
|
-
# i.param # => Object
|
491
|
-
class AsModule < Transformation
|
492
|
-
include Singleton
|
493
|
-
|
494
|
-
protected
|
495
|
-
|
496
|
-
def transform(_, value, _)
|
497
|
-
return if value.nil?
|
498
|
-
return value if value.is_a?(Module)
|
499
|
-
return _lookup(value.to_s) if value.respond_to?(:to_s)
|
500
|
-
raise "#{value} can't be transformed to a class"
|
501
|
-
end
|
502
|
-
|
503
|
-
private
|
504
|
-
|
505
|
-
PATH_REGEXP = Regexp.compile('::|[.]|/')
|
506
|
-
|
507
|
-
def _lookup(class_name)
|
508
|
-
target = class_name.split(PATH_REGEXP)
|
509
|
-
.inject(::Kernel) { |acc, elem| acc.const_get(elem) }
|
510
|
-
|
511
|
-
raise "#{class_name} == #{target} which is not a class" unless target.is_a?(Module)
|
512
|
-
|
513
|
-
target
|
514
|
-
end
|
515
|
-
end
|
516
|
-
|
517
|
-
##
|
518
|
-
# Creates closure with transformation invocation
|
519
|
-
#
|
520
|
-
# It's an interface method required for Transformation to expose
|
521
|
-
# functor properties. It encloses invocation of Transformation
|
522
|
-
# transform_wrapper method into a closure. The closure itself is
|
523
|
-
# executed in the context of Parametrized instance which provides
|
524
|
-
# proper set "self" variable.
|
525
|
-
#
|
526
|
-
# The closure itself accepts 2 parameters:
|
527
|
-
# * +parameter+ - the Parameter object which the transformation
|
528
|
-
# will be performed for
|
529
|
-
# * +value+ - parameter value to be transformed
|
530
|
-
# ...where instance is assumed from self
|
531
|
-
def to_proc
|
532
|
-
transform = self
|
533
|
-
|
534
|
-
lambda do |parameter, value|
|
535
|
-
transform.transform_wrapper(parameter, value, self)
|
536
|
-
end
|
537
|
-
end
|
538
|
-
|
539
|
-
##
|
540
|
-
# Wraps transformation-dependent method
|
541
|
-
#
|
542
|
-
# It wraps constraint-dependent "transform" method into a rescue block
|
543
|
-
# to standardize exception type and information provided by failed
|
544
|
-
# transformation calculation
|
545
|
-
# * +parameter+ - the Parameter object which the transformation will
|
546
|
-
# be performed for
|
547
|
-
# * +value+ - parameter value to be transformed
|
548
|
-
# * +instance+ - the instance value is transform
|
549
|
-
def transform_wrapper(parameter, value, instance)
|
550
|
-
transform(parameter, value, instance)
|
551
|
-
rescue StandardError
|
552
|
-
raise NestedParameterException.new(parameter)
|
553
|
-
end
|
554
|
-
|
555
|
-
protected
|
556
|
-
|
557
|
-
##
|
558
|
-
# Transform method
|
559
|
-
#
|
560
|
-
# * +parameter+ - the Parameter object which the transformation is
|
561
|
-
# performed for
|
562
|
-
# * +value+ - parameter value to be transformed
|
563
|
-
# * +instance+ - the instance value is transform
|
564
|
-
def transform(parameter, value, instance); end
|
565
|
-
end
|
566
|
-
|
567
|
-
##
|
568
|
-
# Syntax sugar for transformations definition
|
569
|
-
#
|
570
|
-
# It injects the methods as class-scope methods into mixing classes.
|
571
|
-
# The methods are factories to create particular type of transformation
|
572
|
-
class_scope do
|
573
|
-
##
|
574
|
-
# Chain a few transformations into a single one
|
575
|
-
#
|
576
|
-
# alias for AsChain class
|
577
|
-
def as_chain(*components)
|
578
|
-
Transformation::AsChain.new(components)
|
579
|
-
end
|
580
|
-
|
581
|
-
##
|
582
|
-
# Transform the value into an object
|
583
|
-
#
|
584
|
-
# alias for AsObject class
|
585
|
-
def as_object(klass = nil, &definition)
|
586
|
-
Transformation::AsObject.new(self, klass, &definition)
|
587
|
-
end
|
588
|
-
|
589
|
-
##
|
590
|
-
# Transform the value into a list
|
591
|
-
#
|
592
|
-
# alias for AsList class
|
593
|
-
def as_list(parameters = nil)
|
594
|
-
Transformation::AsList.new(self, parameters)
|
595
|
-
end
|
596
|
-
|
597
|
-
##
|
598
|
-
# Transform value with the specified render
|
599
|
-
#
|
600
|
-
# alias for AsRendered class
|
601
|
-
def as_rendered(render_type, params = nil, ¶ms_block)
|
602
|
-
Transformation::AsRendered.new(render_type, params || params_block)
|
603
|
-
end
|
604
|
-
|
605
|
-
##
|
606
|
-
# Convert input into integer
|
607
|
-
#
|
608
|
-
# alias for AsInteger class
|
609
|
-
def as_integer
|
610
|
-
Transformation::AsInteger.instance
|
611
|
-
end
|
612
|
-
|
613
|
-
##
|
614
|
-
# Convert input into string
|
615
|
-
#
|
616
|
-
# alias for AsString class
|
617
|
-
def as_string
|
618
|
-
Transformation::AsString.instance
|
619
|
-
end
|
620
|
-
|
621
|
-
##
|
622
|
-
# Convert input into boolean
|
623
|
-
#
|
624
|
-
# alias for AsBoolean class
|
625
|
-
def as_boolean
|
626
|
-
Transformation::AsBoolean.instance
|
627
|
-
end
|
628
|
-
|
629
|
-
##
|
630
|
-
# Convert input into hash
|
631
|
-
#
|
632
|
-
# alias for AsHash class
|
633
|
-
def as_hash(&blk)
|
634
|
-
Transformation::AsHash.new(self, &blk)
|
635
|
-
end
|
636
|
-
|
637
|
-
##
|
638
|
-
# Convert input into a class
|
639
|
-
#
|
640
|
-
# alias for AsModule class
|
641
|
-
def as_module
|
642
|
-
Transformation::AsModule.instance
|
643
|
-
end
|
644
|
-
end
|
645
|
-
|
646
|
-
class Nested #:nodoc:
|
647
|
-
include Parametrized
|
648
|
-
end
|
649
|
-
end
|
650
|
-
end
|
651
|
-
end
|
652
|
-
end
|