Class: PuppetX::Cisco::AutoGen

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_x/cisco/autogen.rb

Overview

PuppetX::Cisco::AutoGen - automatically generate getter/setter methods rubocop:disable Metrics/ClassLength

Class Method Summary collapse

Class Method Details

.mk_puppet_getters_array_flat(klass, nu_name, props) ⇒ Object

Auto-generator for puppet single-level array-based GETTER methods These properties expect a flat array but optionally support a string of space-separated values in the manifest.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/puppet_x/cisco/autogen.rb', line 46

def self.mk_puppet_getters_array_flat(klass, nu_name, props)
  props.each do |prop|
    klass.instance_eval do
      # def foo_array_flat
      #   return @property_hash[:foo_array_flat] if @resource[:foo_array_flat].nil?
      #   if @resource[:foo_array_flat][0] == :default &&
      #      @property_hash[:foo_array_flat] == @nu.default_foo_array_flat
      #     return [:default]
      #   else
      #     @property_hash[:foo_array_flat]
      #   end
      # end
      define_method(prop) do
        return @property_hash[prop] if @resource[prop].nil?
        if @resource[prop][0] == :default &&
           @property_hash[prop] == instance_variable_get(nu_name).send("default_#{prop}")
          return [:default]
        else
          @property_hash[prop]
        end
      end
    end
  end
end

.mk_puppet_getters_bool(klass, nu_name, props) ⇒ Object

Auto-generator for puppet boolean-based GETTER methods



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/puppet_x/cisco/autogen.rb', line 167

def self.mk_puppet_getters_bool(klass, nu_name, props)
  props.each do |prop|
    klass.instance_eval do
      # Generate GETTER method; e.g.
      # def foo
      #   val = @nu.foo
      #   return :default if
      #     @resource[foo] == :default and
      #     val == @nu_name.default_foo
      #   if val.nil?
      #     @property_hash[prop] = nil
      #   else
      #     @property_hash[prop] = val ? :true : :false
      #   end
      # end
      define_method(prop) do
        val = instance_variable_get(nu_name).send(prop)
        return :default if
          @resource[prop] == :default &&
          val == instance_variable_get(nu_name).send("default_#{prop}")
        if val.nil?
          @property_hash[prop] = nil
        else
          @property_hash[prop] = val ? :true : :false
        end
      end
    end
  end
end

.mk_puppet_getters_non_bool(klass, nu_name, props) ⇒ Object

Auto-generator for puppet non-boolean-based GETTER methods



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/puppet_x/cisco/autogen.rb', line 123

def self.mk_puppet_getters_non_bool(klass, nu_name, props)
  props.each do |prop|
    klass.instance_eval do
      # Generate GETTER method; e.g.
      # def foo
      #   return :default if
      #     @resource[:foo] == :default &&
      #     @property_hash[:foo] == @nu.default_foo
      #   @property_hash[:foo]
      # end
      define_method(prop) do
        return :default if
          @resource[prop] == :default &&
          @property_hash[prop] == instance_variable_get(nu_name).send("default_#{prop}")
        @property_hash[prop]
      end
    end
  end
end

.mk_puppet_methods(mtype, klass, nu_name, props) ⇒ Object

“nu_name” refers to node_utils



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/puppet_x/cisco/autogen.rb', line 25

def self.mk_puppet_methods(mtype, klass, nu_name, props)
  case mtype
  when :array_flat
    mk_puppet_getters_array_flat(klass, nu_name, props)
    mk_puppet_setters_array_flat(klass, nu_name, props)
  when :array_nested
    # The *nested* array getters are identical to flat array getters
    mk_puppet_getters_array_flat(klass, nu_name, props)
    mk_puppet_setters_array_nested(klass, nu_name, props)
  when :non_bool
    mk_puppet_getters_non_bool(klass, nu_name, props)
    mk_puppet_setters_non_bool(klass, nu_name, props)
  when :bool
    mk_puppet_getters_bool(klass, nu_name, props)
    mk_puppet_setters_bool(klass, nu_name, props)
  end
end

.mk_puppet_setters_array_flat(klass, nu_name, props) ⇒ Object

Auto-generator for puppet single-level array-based SETTER methods. These properties expect a flat array but optionally support a string of space-separated values in the manifest; however, munge will transform the string into a nested array, hence the flatten.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/puppet_x/cisco/autogen.rb', line 75

def self.mk_puppet_setters_array_flat(klass, nu_name, props)
  props.each do |prop|
    klass.instance_eval do
      # def foo_array_flat=(val)
      #   fail '@property_flush not defined' if
      #     @property_flush.nil?
      #   val = @nu.default_foo_array_flat if
      #     val == :default
      #   @property_flush[:foo_array_flat] = val.flatten
      # end
      define_method("#{prop}=") do |val|
        fail '@property_flush not defined' if
          instance_variable_get(:@property_flush).nil?
        val = instance_variable_get(nu_name).send("default_#{prop}") if
          val == [:default]
        @property_flush[prop] = val.flatten
      end
    end
  end
end

.mk_puppet_setters_array_nested(klass, nu_name, props) ⇒ Object

Auto-generator for puppet nested array-based SETTER methods These properties expect a nested array but optionally support a string of space-separated values in the manifest.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/puppet_x/cisco/autogen.rb', line 101

def self.mk_puppet_setters_array_nested(klass, nu_name, props)
  props.each do |prop|
    klass.instance_eval do
      # def foo_array_nested=(val)
      #   fail '@property_flush not defined' if
      #     @property_flush.nil?
      #   val = @nu.default_foo_array_nested if
      #     val[0] == :default                      (index 0)
      #   @property_flush[:foo_array_nested] = val  (no flatten)
      # end
      define_method("#{prop}=") do |val|
        fail '@property_flush not defined' if
          instance_variable_get(:@property_flush).nil?
        val = instance_variable_get(nu_name).send("default_#{prop}") if
          val[0] == :default
        @property_flush[prop] = val
      end
    end
  end
end

.mk_puppet_setters_bool(klass, nu_name, props) ⇒ Object

Auto-generator for puppet boolean-based SETTER methods



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/puppet_x/cisco/autogen.rb', line 198

def self.mk_puppet_setters_bool(klass, nu_name, props)
  props.each do |prop|
    klass.instance_eval do
      # Generate SETTER method; e.g.
      # def foo=
      #   if val == :default
      #     @property_flush[foo] = @nu_name.default_foo
      #   else
      #     @property_flush[prop] = (val == :true)
      #   end
      # end
      define_method("#{prop}=") do |val|
        fail '@property_flush not defined' if
          instance_variable_get(:@property_flush).nil?
        if val == :default
          @property_flush[prop] = instance_variable_get(nu_name).send("default_#{prop}")
        else
          @property_flush[prop] = (val == :true)
        end
      end
    end
  end
end

.mk_puppet_setters_non_bool(klass, nu_name, props) ⇒ Object

Auto-generator for puppet non-boolean-based SETTER methods



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/puppet_x/cisco/autogen.rb', line 144

def self.mk_puppet_setters_non_bool(klass, nu_name, props)
  props.each do |prop|
    klass.instance_eval do
      # Generate SETTER method; e.g.
      # def foo=(val)
      #   if val == :default
      #     val = @nu.default_foo
      #   end
      #   @property_flush[:foo] = val
      # end
      define_method("#{prop}=")do |val|
        fail '@property_flush not defined' if
          instance_variable_get(:@property_flush).nil?
        if val == :default
          val = instance_variable_get(nu_name).send("default_#{prop}")
        end
        @property_flush[prop] = val
      end
    end
  end
end