class Chef::Decorator

Public Class Methods

new(obj = NOT_PASSED) click to toggle source
Calls superclass method
# File lib/chef/decorator.rb, line 23
def initialize(obj = NOT_PASSED)
  @__defined_methods__ = []
  super unless obj.equal?(NOT_PASSED)
end

Public Instance Methods

__setobj__(obj) click to toggle source

reset our methods on the instance if the object changes under us (this also clears out the closure over the target we create in method_missing below)

Calls superclass method
# File lib/chef/decorator.rb, line 45
def __setobj__(obj)
  __reset_methods__
  super
end
is_a?(klass) click to toggle source

if we wrap a Hash then decorator.is_a?(Hash) should be true

Calls superclass method
# File lib/chef/decorator.rb, line 34
def is_a?(klass)
  __getobj__.is_a?(klass) || super
end
kind_of?(klass) click to toggle source

if we wrap a Hash then decorator.kind_of?(Hash) should be true

Calls superclass method
# File lib/chef/decorator.rb, line 39
def kind_of?(klass)
  __getobj__.is_a?(klass) || super
end
method_missing(m, *args, &block) click to toggle source

this is the ruby 2.2/2.3 implementation of Delegator#method_missing() with adding the define_singleton_method call and @defined_methods tracking

Calls superclass method
# File lib/chef/decorator.rb, line 52
def method_missing(m, *args, &block)
  r = true
  target = __getobj__ { r = false }

  if r && target.respond_to?(m)
    # these next 4 lines are the patched code
    define_singleton_method(m) do |*args, &block|
      target.__send__(m, *args, &block)
    end
    @__defined_methods__.push(m)
    target.__send__(m, *args, &block)
  elsif ::Kernel.respond_to?(m, true)
    ::Kernel.instance_method(m).bind(self).call(*args, &block)
  else
    super(m, *args, &block)
  end
end
nil?() click to toggle source

if we wrap a nil then decorator.nil? should be true

# File lib/chef/decorator.rb, line 29
def nil?
  __getobj__.nil?
end

Private Instance Methods

__reset_methods__() click to toggle source

used by __setobj__ to clear the methods we’ve built on the instance.

# File lib/chef/decorator.rb, line 73
def __reset_methods__
  @__defined_methods__.each do |m|
    singleton_class.send(:undef_method, m)
  end
  @__defined_methods__ = []
end