cfndsl 0.0.13 → 0.0.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/cfndsl/Types.rb +60 -22
  2. data/lib/cfndsl/names.rb +12 -6
  3. 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
- CfnDsl::methodNames(method) do |method_name|
73
+ all_methods.each do |method_name|
73
74
  define_method(method_name) do | value=nil, *rest, &block|
74
- value ||= klass.new
75
- x = instance_variable_get( variable )
76
- if( !x ) then
77
- x = instance_variable_set( variable, [] )
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
- x.push value
80
- value.instance_eval &block if block
81
- value
82
- end
83
- end
84
- CfnDsl::methodNames(methods) do |methods_name|
85
- define_method(methods_name) do | value, &block |
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
- if( ! value.type_of? Array) then
92
- value = [value]
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
- value.each do |v|
95
- x.push v
96
- v.instance_eval &block if block
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
- name_str = name.to_s
7
- yield name_str.to_sym
8
- n = name_str.dup
9
- n[0] = n[0].swapcase
10
- yield n.to_sym
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cfndsl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13
4
+ version: 0.0.14
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: