Class: Conditional

Inherits:
Object
  • Object
show all
Defined in:
lib/retrospec/conditional.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(branch, parameters) ⇒ Conditional

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



11
12
13
# File 'lib/retrospec/conditional.rb', line 11

def initialize(branch, parameters)
   @statements = branch.statements
end

Instance Attribute Details

#statementsObject (readonly)

Returns the value of attribute statements.



4
5
6
# File 'lib/retrospec/conditional.rb', line 4

def statements
  @statements
end

#testObject (readonly)

Returns the value of attribute test.



4
5
6
# File 'lib/retrospec/conditional.rb', line 4

def test
  @test
end

#valueObject (readonly)

Returns the value of attribute value.



4
5
6
# File 'lib/retrospec/conditional.rb', line 4

def value
  @value
end

Class Method Details

.all(type) ⇒ Object

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



17
18
19
20
21
22
23
# File 'lib/retrospec/conditional.rb', line 17

def self.all(type)
  r_attrs = []
  generate_conditionals(type).each do |c|
    r_attrs << Resource.all(c.statements)
  end
  r_attrs.flatten
end

.find_conditionals(type) ⇒ Object

returns a array of branch subtypes



35
36
37
38
39
40
41
# File 'lib/retrospec/conditional.rb', line 35

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

.generate_conditionals(type) ⇒ Object

find and create an array of conditionals we need the type so we can look through the code to find conditional statements



45
46
47
48
49
50
51
# File 'lib/retrospec/conditional.rb', line 45

def self.generate_conditionals(type)
  conditionals = []
  find_conditionals(type).each do |cond|
    conditionals << Conditional.new(cond, type.arguments)
  end
  conditionals
end

.typesObject

a array of types the are known to contain conditional code and statements



26
27
28
29
30
31
32
# File 'lib/retrospec/conditional.rb', line 26

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