Class: ClusterChef::DslObject

Inherits:
Object
  • Object
show all
Defined in:
lib/cluster_chef/dsl_object.rb

Overview

Provides magic methods, defined with has_keys

Examples:

class Mom < ClusterChef::DslObject
  has_keys(:college, :combat_boots, :fat, :so_fat)
end

class Person
  def momma &block
    @momma ||= Mom.new
    @momma.configure(&block) if block
  end
end

yo = Person.new
yo.mamma.combat_boots :wears
yo.momma do
  fat    true
  so_fat 'When she sits around the house, she sits *AROUND* the house'
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}, &block) ⇒ DslObject

Returns a new instance of DslObject.


49
50
51
52
# File 'lib/cluster_chef/dsl_object.rb', line 49

def initialize(attrs={}, &block)
  @settings = Mash.new
  configure(attrs, &block)
end

Class Method Details

.has_keys(*key_names) ⇒ Object

Defines DSL attributes

Examples:

class Mom < ClusterChef::DslObject
  has_keys(:fat, :so_fat)
end
yer_mom = Mom.new
yer_mom.fat :quite

66
67
68
69
70
71
72
73
74
# File 'lib/cluster_chef/dsl_object.rb', line 66

def self.has_keys(*key_names)
  key_names.map!(&:to_sym)
  self.keys += key_names
  self.keys.uniq!
  key_names.each do |key|
    next if method_defined?(key)
    define_method(key){|*args| set(key, *args) }
  end
end

.uiObject

delegate to the knife ui presenter


109
# File 'lib/cluster_chef/dsl_object.rb', line 109

def self.ui() ClusterChef.ui ; end

Instance Method Details

#configure(hsh = {}, &block) ⇒ Object


100
101
102
103
104
# File 'lib/cluster_chef/dsl_object.rb', line 100

def configure(hsh={}, &block)
  @settings.merge!(hsh.to_hash)
  instance_eval(&block) if block
  self
end

#die(*args) ⇒ Object

helper method for bombing out of a script


116
# File 'lib/cluster_chef/dsl_object.rb', line 116

def die(*args) ClusterChef.die(*args) ; end

#dump(*args) ⇒ Object

helper method for debugging only


122
# File 'lib/cluster_chef/dsl_object.rb', line 122

def dump(*args) args.each{|arg| Chef::Log.debug( arg.inspect ) } end

#reverse_merge!(hsh) ⇒ Object


96
97
98
# File 'lib/cluster_chef/dsl_object.rb', line 96

def reverse_merge!(hsh)
  @settings.reverse_merge!(hsh.to_hash)
end

#safely(*args, &block) ⇒ Object

helper method for turning exceptions into warnings


119
# File 'lib/cluster_chef/dsl_object.rb', line 119

def safely(*args, &block) ClusterChef.safely(*args, &block) ; end

#set(key, val = nil) ⇒ Object

Sets the DSL attribute, unless the given value is nil.


79
80
81
82
# File 'lib/cluster_chef/dsl_object.rb', line 79

def set(key, val=nil)
  @settings[key.to_s] = val unless val.nil?
  @settings[key.to_s]
end

#step(desc, *style) ⇒ Object


111
112
113
# File 'lib/cluster_chef/dsl_object.rb', line 111

def step(desc, *style)
  ui.info("  #{"%-15s" % (name.to_s+":")}\t#{ui.color(desc.to_s, *style)}")
end

#to_hashObject


84
85
86
# File 'lib/cluster_chef/dsl_object.rb', line 84

def to_hash
  @settings.to_hash
end

#to_mashObject


88
89
90
# File 'lib/cluster_chef/dsl_object.rb', line 88

def to_mash
  @settings.dup
end

#to_sObject


92
93
94
# File 'lib/cluster_chef/dsl_object.rb', line 92

def to_s
  "<#{self.class} #{to_hash.inspect}>"
end

#uiObject

delegate to the knife ui presenter


107
# File 'lib/cluster_chef/dsl_object.rb', line 107

def ui()      ClusterChef.ui ; end