cfndsl 0.0.13 → 0.0.14
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cfndsl/Types.rb +60 -22
- data/lib/cfndsl/names.rb +12 -6
- metadata +1 -1
data/lib/cfndsl/Types.rb
CHANGED
@@ -67,34 +67,72 @@ module CfnDsl
|
|
67
67
|
|
68
68
|
method = CfnDsl::Plurals::singularize(attr_name)
|
69
69
|
methods = attr_name
|
70
|
-
|
70
|
+
all_methods = CfnDsl::methodNames(method) +
|
71
|
+
CfnDsl::methodNames(methods)
|
71
72
|
type.class_eval do
|
72
|
-
|
73
|
+
all_methods.each do |method_name|
|
73
74
|
define_method(method_name) do | value=nil, *rest, &block|
|
74
|
-
|
75
|
-
|
76
|
-
if
|
77
|
-
|
75
|
+
existing = instance_variable_get( variable )
|
76
|
+
# For no-op invocations, get out now
|
77
|
+
return existing if value.nil? and rest.length == 0 and ! block
|
78
|
+
|
79
|
+
# We are going to modify the value in some
|
80
|
+
# way, make sure that we have an array to mess
|
81
|
+
# with if we start with nothing
|
82
|
+
if( !existing ) then
|
83
|
+
existing = instance_variable_set( variable, [] )
|
78
84
|
end
|
79
|
-
|
80
|
-
|
81
|
-
value
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
x = instance_variable_get( variable )
|
87
|
-
if( !x ) then
|
88
|
-
x = instance_variable_set( variable, [] )
|
85
|
+
|
86
|
+
# special case for just a block, no args
|
87
|
+
if( value.nil? and rest.length == 0 and block ) then
|
88
|
+
val = klass.new
|
89
|
+
existing.push val
|
90
|
+
value.instance_eval &block(val)
|
91
|
+
return existin
|
89
92
|
end
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
+
|
94
|
+
# Glue all of our parameters together into
|
95
|
+
# a giant array - flattening one level deep, if needed
|
96
|
+
array_params = []
|
97
|
+
if( value.kind_of? Array) then
|
98
|
+
value.each {|x| array_params.push x}
|
99
|
+
else
|
100
|
+
array_params.push value
|
93
101
|
end
|
94
|
-
|
95
|
-
|
96
|
-
|
102
|
+
if( rest.length > 0) then
|
103
|
+
rest.each do |v|
|
104
|
+
if( v.kind_of? Array ) then
|
105
|
+
array_params += rest
|
106
|
+
else
|
107
|
+
array_params.push v
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
# Here, if we were given multiple arguments either
|
113
|
+
# as method [a,b,c], method(a,b,c), or even
|
114
|
+
# method( a, [b], c) we end up with
|
115
|
+
# array_params = [a,b,c]
|
116
|
+
#
|
117
|
+
# array_params will have at least one item
|
118
|
+
# unless the user did something like pass in
|
119
|
+
# a bunch of empty arrays.
|
120
|
+
if block then
|
121
|
+
array_params.each do |val|
|
122
|
+
value = klass.new
|
123
|
+
existing.push value
|
124
|
+
value.instance_eval &block(val) if block
|
125
|
+
end
|
126
|
+
else
|
127
|
+
# List of parameters with no block -
|
128
|
+
# hope that the user knows what he is
|
129
|
+
# doing and stuff them into our existing
|
130
|
+
# array
|
131
|
+
array_params.each do |val|
|
132
|
+
existing.push value
|
133
|
+
end
|
97
134
|
end
|
135
|
+
return existing
|
98
136
|
end
|
99
137
|
end
|
100
138
|
end
|
data/lib/cfndsl/names.rb
CHANGED
@@ -2,11 +2,17 @@ module CfnDsl
|
|
2
2
|
##
|
3
3
|
# iterates through the the valid case-insensitive names
|
4
4
|
# for "name".
|
5
|
-
def self.methodNames(name)
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
5
|
+
def self.methodNames(name, &block)
|
6
|
+
if block then
|
7
|
+
name_str = name.to_s
|
8
|
+
yield name_str.to_sym
|
9
|
+
n = name_str.dup
|
10
|
+
n[0] = n[0].swapcase
|
11
|
+
yield n.to_sym
|
12
|
+
else
|
13
|
+
result = [name.dup,name.dup]
|
14
|
+
result[1][0] = result[1][0].swapcase
|
15
|
+
return result
|
16
|
+
end
|
11
17
|
end
|
12
18
|
end
|