Class: Chef::Decorator::Unchain

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/decorator/unchain.rb

Overview

This decorator unchains method call chains and turns them into method calls with variable args. So this:

node.set_unless["foo"]["bar"] = "baz"

Can become:

node.set_unless("foo", "bar", "baz")

While this is a decorator it is not a Decorator and does not inherit because it deliberately does not need or want the method_missing magic. It is not legal to call anything on the intermediate values and only supports method chaining with

[] until the chain comes to an end with #[]=, so does not behave like a hash or

array... e.g.

node.default['foo'].keys is legal node.set_unless['foo'].keys is not legal now or ever

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, method) ⇒ Unchain

Returns a new instance of Unchain.



26
27
28
29
30
# File 'lib/chef/decorator/unchain.rb', line 26

def initialize(obj, method)
  @__path__        = []
  @__method__      = method
  @delegate_sd_obj = obj
end

Instance Attribute Details

#__method__Object

Returns the value of attribute method.



24
25
26
# File 'lib/chef/decorator/unchain.rb', line 24

def __method__
  @__method__
end

#__path__Object

Returns the value of attribute path.



23
24
25
# File 'lib/chef/decorator/unchain.rb', line 23

def __path__
  @__path__
end

Instance Method Details

#[](key) ⇒ Object



32
33
34
35
# File 'lib/chef/decorator/unchain.rb', line 32

def [](key)
  __path__.push(key)
  self
end

#[]=(key, value) ⇒ Object



37
38
39
40
# File 'lib/chef/decorator/unchain.rb', line 37

def []=(key, value)
  __path__.push(key)
  @delegate_sd_obj.public_send(__method__, *__path__, value)
end