Class: Chef::Decorator
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Chef::Decorator
- Defined in:
- lib/chef/decorator.rb,
lib/chef/decorator/lazy.rb,
lib/chef/decorator/unchain.rb,
lib/chef/decorator/lazy_array.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Lazy, LazyArray, Unchain
Constant Summary collapse
- NULL =
::Object.new
Instance Method Summary collapse
-
#__setobj__(obj) ⇒ Object
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).
-
#initialize(obj = NULL) ⇒ Decorator
constructor
A new instance of Decorator.
-
#is_a?(klass) ⇒ Boolean
if we wrap a Hash then decorator.is_a?(Hash) should be true.
-
#kind_of?(klass) ⇒ Boolean
if we wrap a Hash then decorator.kind_of?(Hash) should be true.
-
#method_missing(m, *args, &block) ⇒ Object
this is the ruby 2.2/2.3 implementation of Delegator#method_missing() with adding the define_singleton_method call and @defined_methods tracking.
-
#nil? ⇒ Boolean
if we wrap a nil then decorator.nil? should be true.
Constructor Details
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
this is the ruby 2.2/2.3 implementation of Delegator#method_missing() with adding the define_singleton_method call and @defined_methods tracking
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/chef/decorator.rb', line 53 def method_missing(m, *args, &block) r = true target = self.__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 |
Instance Method Details
#__setobj__(obj) ⇒ Object
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)
46 47 48 49 |
# File 'lib/chef/decorator.rb', line 46 def __setobj__(obj) __reset_methods__ super end |
#is_a?(klass) ⇒ Boolean
if we wrap a Hash then decorator.is_a?(Hash) should be true
35 36 37 |
# File 'lib/chef/decorator.rb', line 35 def is_a?(klass) __getobj__.is_a?(klass) || super end |
#kind_of?(klass) ⇒ Boolean
if we wrap a Hash then decorator.kind_of?(Hash) should be true
40 41 42 |
# File 'lib/chef/decorator.rb', line 40 def kind_of?(klass) __getobj__.kind_of?(klass) || super end |
#nil? ⇒ Boolean
if we wrap a nil then decorator.nil? should be true
30 31 32 |
# File 'lib/chef/decorator.rb', line 30 def nil? __getobj__.nil? end |