Class: Delegator

Inherits:
Object show all
Defined in:
lib/delegate.rb

Overview

Delegator is an abstract class used to build delegator pattern objects from subclasses. Subclasses should redefine __getobj__. For a concrete implementation, see SimpleDelegator.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Delegator) initialize(obj)

Pass in the obj to delegate method calls to. All methods supported by obj will be delegated to.



126
127
128
# File 'lib/delegate.rb', line 126

def initialize(obj)
  __setobj__(obj)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(m, *args, &block)

Handles the magic of delegation through __getobj__.



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/delegate.rb', line 131

def method_missing(m, *args, &block)
  begin
    target = self.__getobj__
    unless target.respond_to?(m)
      super(m, *args, &block)
    else
      target.__send__(m, *args, &block)
    end
  rescue Exception
    $@.delete_if{|s| %r"\A#{__FILE__}:\d+:in `method_missing'\z"o =~ s}
    ::Kernel::raise
  end
end

Class Method Details

+ (Object) delegating_block(mid)

:stopdoc:



234
235
236
237
238
239
240
241
242
243
244
# File 'lib/delegate.rb', line 234

def Delegator.delegating_block(mid)
  lambda do |*args, &block|
    begin
      __getobj__.__send__(mid, *args, &block)
    rescue
      re = /\A#{Regexp.quote(__FILE__)}:#{__LINE__-2}:/o
      $!.backtrace.delete_if {|t| re =~ t}
      raise
    end
  end
end

Instance Method Details

- (Object) ==(obj)

Returns true if two objects are considered same.



157
158
159
160
# File 'lib/delegate.rb', line 157

def ==(obj)
  return true if obj.equal?(self)
  self.__getobj__ == obj
end

- (Object) __getobj__

This method must be overridden by subclasses and should return the object method calls are being delegated to.

Raises:

  • (NotImplementedError)


166
167
168
# File 'lib/delegate.rb', line 166

def __getobj__
  raise NotImplementedError, "need to define `__getobj__'"
end

- (Object) __setobj__(obj)

This method must be overridden by subclasses and change the object delegate to obj.

Raises:

  • (NotImplementedError)


174
175
176
# File 'lib/delegate.rb', line 174

def __setobj__(obj)
  raise NotImplementedError, "need to define `__setobj__'"
end

- (Object) clone

Clone support for the object returned by __getobj__.



188
189
190
191
192
# File 'lib/delegate.rb', line 188

def clone
  new = super
  new.__setobj__(__getobj__.clone)
  new
end

- (Object) dup

Duplication support for the object returned by __getobj__.



194
195
196
197
198
# File 'lib/delegate.rb', line 194

def dup
  new = super
  new.__setobj__(__getobj__.dup)
  new
end

- (Object) marshal_dump

Serialization support for the object returned by __getobj__.



179
180
181
# File 'lib/delegate.rb', line 179

def marshal_dump
  __getobj__
end

- (Object) marshal_load(obj)

Reinitializes delegation from a serialized object.



183
184
185
# File 'lib/delegate.rb', line 183

def marshal_load(obj)
  __setobj__(obj)
end

- (Boolean) respond_to?(m, include_private = false)

Checks for a method provided by this the delegate object by fowarding the call through __getobj__.

Returns:

  • (Boolean)


149
150
151
152
# File 'lib/delegate.rb', line 149

def respond_to?(m, include_private = false)
  return true if super
  return self.__getobj__.respond_to?(m, include_private)
end