Module: Statement

Defined in:
lib/puppet/parser/functions/statement.rb

Defined Under Namespace

Classes: Argument, Option, Parameter, SimpleParameter, Stment, TypedParameter, TypedParameterSimpleValue, TypedParameterTypedValue, TypedParameterValue

Constant Summary collapse

@@indent =
""
@@indent_step =
"    "
@@current_statement =
nil
@@current_option =
nil
@@current_parameter =
nil
@@current_parameter_value =
nil

Class Method Summary collapse

Class Method Details

.create_and_add_option(item) ⇒ Object



274
275
276
277
278
# File 'lib/puppet/parser/functions/statement.rb', line 274

def self.create_and_add_option(item)
    @@current_option = Option.new()
    parse_option(item)
    @@current_statement.add_option(@@current_option)
end

.create_and_add_parameters(params) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/puppet/parser/functions/statement.rb', line 246

def self.create_and_add_parameters(params)
    params.each do |item|
        if is_simple_type?(item)
            @@current_parameter = SimpleParameter.new(item)
        elsif item.is_a? Hash
            @@current_parameter = TypedParameter.new
            parse_typed_parameter(item)
        end

        @@current_option.add_parameter(@@current_parameter)
    end
end

.create_configuration_tree(id, type, options) ⇒ Object



293
294
295
296
297
# File 'lib/puppet/parser/functions/statement.rb', line 293

def self.create_configuration_tree(id, type, options)
    @@indent = ""
    @@current_statement = Stment.new(type, id)
    parse_tree(options)
end

.decrease_indentObject



176
177
178
# File 'lib/puppet/parser/functions/statement.rb', line 176

def self.decrease_indent()
    @@indent = @@indent[4..-1]
end

.expand_one_key_hash(hash) ⇒ Object



192
193
194
195
196
# File 'lib/puppet/parser/functions/statement.rb', line 192

def self.expand_one_key_hash(hash)
    type = hash.keys[0]
    value = hash[type]
    return type, value
end

.generate_statement(id, type, options) ⇒ Object



305
306
307
308
# File 'lib/puppet/parser/functions/statement.rb', line 305

def self.generate_statement(id, type, options)
    create_configuration_tree(id, type, options)
    render_configuration()
end

.get_indentObject



180
181
182
# File 'lib/puppet/parser/functions/statement.rb', line 180

def self.get_indent()
    return @@indent
end

.getln(line) ⇒ Object



184
185
186
# File 'lib/puppet/parser/functions/statement.rb', line 184

def self.getln(line)
   return @@indent + "#{line}\n"
end

.increase_indentObject



172
173
174
# File 'lib/puppet/parser/functions/statement.rb', line 172

def self.increase_indent()
    @@indent += @@indent_step
end

.is_simple_type?(value) ⇒ Boolean

Returns:

  • (Boolean)


216
217
218
# File 'lib/puppet/parser/functions/statement.rb', line 216

def self.is_simple_type?(value)
    return [String, Numeric].any? {|item| value.is_a? item}
end

.line(line) ⇒ Object



188
189
190
# File 'lib/puppet/parser/functions/statement.rb', line 188

def self.line(line)
    return @@indent + "#{line}"
end

.parse_option(option) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/puppet/parser/functions/statement.rb', line 259

def self.parse_option(option)
    if option.has_key?('type')
        type = option['type']
        params = option['options']
        @@current_option.set_type(type)

        create_and_add_parameters(params)
    else
        type, params = expand_one_key_hash(option)
        @@current_option.set_type(type)

        create_and_add_parameters(params)
    end
end

.parse_tree(options) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/puppet/parser/functions/statement.rb', line 280

def self.parse_tree(options)
    if options.is_a? Array
        options.each do |item|
            create_and_add_option(item)
        end
    elsif options.is_a? Hash
        item = options
        create_and_add_option(item)
    else
        raise Puppet::ParseError, "You must use a Hash or an Array as the parameter"
    end
end

.parse_typed_parameter(item) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/puppet/parser/functions/statement.rb', line 220

def self.parse_typed_parameter(item)
    type, value = expand_one_key_hash(item)

    @@current_parameter.type = type

    ## flags => 'no-parse'
    if is_simple_type?(value) and value != ''
        @@current_parameter_value = TypedParameterSimpleValue.new(value)
        @@current_parameter.add_value(@@current_parameter_value)
    # flags => ['something', 'no-parse']
    elsif value.is_a? Array
        value.each do |item|
            # 'no-parse'
            if is_simple_type?(item)
                @@current_parameter_value = TypedParameterSimpleValue.new(item)
                @@current_parameter.add_value(@@current_parameter_value)
            # { ... }
            elsif item.is_a? Hash
                @@current_parameter_value = TypedParameterTypedValue.new
                parse_typed_parameter_typed_value(item)
                @@current_parameter.add_value(@@current_parameter_value)
            end
        end
    end
end

.parse_typed_parameter_typed_value(values) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/puppet/parser/functions/statement.rb', line 198

def self.parse_typed_parameter_typed_value(values)
    type, value = expand_one_key_hash(values)

    # key_file => ...
    @@current_parameter_value.type = type

    if is_simple_type?(value)
        a = Argument.new(value)
        @@current_parameter_value.add_argument(a)
    elsif value.is_a? Array
        value.each do |item|
            # these should be strings or numbers or whatever simple types
            a = Argument.new(item)
            @@current_parameter_value.add_argument(a)
        end
    end
end

.render_configurationObject



299
300
301
302
303
# File 'lib/puppet/parser/functions/statement.rb', line 299

def self.render_configuration()
    text_repr = @@current_statement.build
    @@indent = ""
    return text_repr
end