Sha256: 53728db0cac149b2ae89aab9acdb36213ede01f5fb0f9e47e9db86b37ee8ccff

Contents?: true

Size: 1.69 KB

Versions: 4

Compression:

Stored size: 1.69 KB

Contents

require_relative 'resource'

class Conditional
  attr_reader :test, :value, :statements

  # things I need:  a key/value store for variables
  # types of variables
  # those that can be changed
  # those that can be influenced (facts, other variables that contain variables)
  # takes a subtype of Puppet::AST::Branch that contains conditional logic
  def initialize(branch, parameters)
     @statements = branch.statements
  end

  # get the attributes for the given resources found in the type code passed in
  # this will return a array of hashes, one for each resource found
  def self.all(type)
    r_attrs = []
    generate_conditionals(type).each do |c|
      r_attrs << Resource.all(c.statements)
    end
    r_attrs.flatten
  end

  # a array of types the are known to contain conditional code and statements
  def self.types
    #test, statement, value
    # if I don't have a statement that I am part of a bigger code block
    # [Puppet::Parser::AST::IfStatement, Puppet::Parser::AST::CaseStatement, Puppet::Parser::AST::Else,
    #  Puppet::Parser::AST::CaseOpt, Puppet::Parser::AST::Selector]
    [Puppet::Parser::AST::IfStatement, Puppet::Parser::AST::Else]
  end

  # returns a array of branch subtypes
  def self.find_conditionals(type)
    conds = []
    if type.code.respond_to?(:find_all)
      conds = type.code.find_all {|c| types.include?(c.class)  }
    end
    conds
  end

  # find and create an array of conditionals
  # we need the type so we can look through the code to find conditional statements
  def self.generate_conditionals(type)
    conditionals = []
    find_conditionals(type).each do |cond|
      conditionals << Conditional.new(cond, type.arguments)
    end
    conditionals
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
puppet-retrospec-0.11.0 lib/retrospec/plugins/v1/plugin/conditional.rb
puppet-retrospec-0.10.0 lib/retrospec/plugins/v1/plugin/conditional.rb
puppet-retrospec-0.9.1 lib/retrospec/plugins/v1/plugin/conditional.rb
puppet-retrospec-0.9.0 lib/retrospec/plugins/v1/plugin/conditional.rb