Sha256: b5d6f011fe57d299773f076692d46461fa19951667a001fb137c973691a0bb9a
Contents?: true
Size: 1.18 KB
Versions: 8
Compression:
Stored size: 1.18 KB
Contents
# This class delegates method calls to the given object. # If the returned value is an instance of the original object, # the smart delegator will return a new instance of itself with # the original object. # # This is in contrast to SimpleDelegator which will return the # original object. # # The chart below demonstrates the difference: # # `mymessage` -> # SimpleDelegator -> # OriginalObject -> # invokes `mymessage` returning self -> # returns OriginalObject # `mymessage` -> # SmartDelegator -> # OriginalObject -> # invokes `mymessage` returning self -> # returns SmartDelegator decorating OriginalObject class SmartDelegator attr_reader :object def initialize(object) @object = object end def respond_to?(method, include_private = false) super || object.respond_to?(method, include_private) end def inspect "SmartDelegator(#{object.inspect})" end def method_missing(method, *args, &block) returned_object = object.public_send(method, *args, &block) if returned_object.class == object.class self.class.new(returned_object) else returned_object end end end
Version data entries
8 entries across 8 versions & 1 rubygems