Class: Cisco::ChefUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/cisco_node_utils/cisco_cmn_utils.rb

Overview

ChefUtils - helper class for Chef code generation

Class Method Summary collapse

Class Method Details

.generic_prop_set(klass, rlbname, props) ⇒ Object


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/cisco_node_utils/cisco_cmn_utils.rb', line 63

def self.generic_prop_set(klass, rlbname, props)
  props.each do |prop|
    klass.instance_eval do
      # Helper Chef setter method, e.g.:
      #   if @new_resource.foo.nil?
      #     def_prop = @rlb.default_foo
      #     @new_resource.foo(def_prop)
      #   end
      #   current = @rlb.foo
      #   if current != @new_resource.foo
      #     converge_by("update foo '#{current}' => " +
      #                 "'#{@new_resource.foo}'") do
      #       @rlb.foo=(@new_resource.foo)
      #     end
      #   end
      if @new_resource.send(prop).nil?
        def_prop = instance_variable_get(rlbname).send("default_#{prop}")
        # Set resource to default if recipe property is not specified
        @new_resource.send(prop, def_prop)
      end
      current = instance_variable_get(rlbname).send(prop)
      if current != @new_resource.send(prop)
        converge_by("update #{prop} '#{current}' => " \
                    "'#{@new_resource.send(prop)}'") do
          instance_variable_get(rlbname).send("#{prop}=",
                                              @new_resource.send(prop))
        end
      end
    end
  end
end