Class: Toaster::DefaultProcessorRecursive

Inherits:
Hash
  • Object
show all
Defined in:
lib/toaster/chef/chef_node_inspector.rb

Overview

Enhanced, recursive mini-DSL processor which is able to parse multi-level nested Chef attributes, e.g., default[‘worker’] = 4

Author: Waldemar Hummer ([email protected])

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node_values = {}, &error_reporting) ⇒ DefaultProcessorRecursive

Returns a new instance of DefaultProcessorRecursive.



32
33
34
35
36
# File 'lib/toaster/chef/chef_node_inspector.rb', line 32

def initialize(node_values = {}, &error_reporting)
  @user_values = Hash.new.merge(node_values) if node_values
  @error_reporting = error_reporting
  @swallow_calls_hash = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *arguments, &block) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/toaster/chef/chef_node_inspector.rb', line 73

def method_missing(sym, *arguments, &block)
  predefined_syms = ["platform", "platform_version", "ipaddress", "fqdn", 
      "macaddress", "hostname", "domain", "recipes", "roles", 
      "ohai_time", "kernel", "cpu"]
  value = @user_values[sym.to_s]
  if !value
    if @error_reporting
      @error_reporting.call(Logger::WARN, "No value for Chef node attribute '#{sym.to_s}'. Please provide a value in the chef_node_inspector configuration.")
    end
    if !predefined_syms.include?(sym.to_s)
      store(sym.to_s, self.class.new)
      value = self[sym.to_s]
      #raise "Missing method #{sym} when trying to parse Chef node attributes"
    end
  end
  if swallow_calls_hash
    puts "INFO: swallowing/reporting method call #{sym}(#{arguments.size})"
    swallow_calls_hash[sym] = [] if !swallow_calls_hash[sym]
    swallow_calls_hash[sym] << arguments
  end
  return value
end

Instance Attribute Details

#swallow_calls_hashObject

:nodoc:



31
32
33
# File 'lib/toaster/chef/chef_node_inspector.rb', line 31

def swallow_calls_hash
  @swallow_calls_hash
end

Instance Method Details

#attribute?(arg) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/toaster/chef/chef_node_inspector.rb', line 58

def attribute?(arg)
  return !@user_values[arg].nil?
end

#default(arg = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/toaster/chef/chef_node_inspector.rb', line 37

def default(arg = nil)
  if arg
    if !self.include?(arg)
      store(arg, self.class.new)
    end
    return self[arg]
  end
  return self
end

#node(arg = nil) ⇒ Object



53
54
55
56
57
# File 'lib/toaster/chef/chef_node_inspector.rb', line 53

def node(arg = nil)
  new_hash = self.class.new
  new_hash.merge!(@user_values["node"]) if @user_values["node"]
  return new_hash
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/toaster/chef/chef_node_inspector.rb', line 61

def respond_to?(method, include_private = false)
  true
end

#set(arg = nil) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/toaster/chef/chef_node_inspector.rb', line 46

def set(arg = nil)
  if arg
    store(arg, self.class.new)
    return self[arg]
  end
  return self
end

#to_aryObject



70
71
72
# File 'lib/toaster/chef/chef_node_inspector.rb', line 70

def to_ary
  []
end

#to_sObject



67
68
69
# File 'lib/toaster/chef/chef_node_inspector.rb', line 67

def to_s
  ""
end

#to_strObject



64
65
66
# File 'lib/toaster/chef/chef_node_inspector.rb', line 64

def to_str
  ""
end