Class: Sunspot::Util::ContextBoundDelegate

Inherits:
Object
  • Object
show all
Defined in:
sunspot/lib/sunspot/util.rb

Constant Summary

BASIC_METHODS =
Set[:==, :equal?, :!", :!=", :instance_eval,
:object_id, :__send__, :__id__]

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (ContextBoundDelegate) initialize(receiver, calling_context)

A new instance of ContextBoundDelegate



222
223
224
# File 'sunspot/lib/sunspot/util.rb', line 222

def initialize(receiver, calling_context)
  @__receiver__, @__calling_context__ = receiver, calling_context
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



235
236
237
# File 'sunspot/lib/sunspot/util.rb', line 235

def method_missing(method, *args, &block)
  __proxy_method__(method, *args, &block)
end

Class Method Details

+ (Object) instance_eval_with_context(receiver, &block)



203
204
205
206
207
208
209
# File 'sunspot/lib/sunspot/util.rb', line 203

def instance_eval_with_context(receiver, &block)
  calling_context = eval('self', block.binding)
  if parent_calling_context = calling_context.instance_eval{@__calling_context__}
    calling_context = parent_calling_context
  end
  new(receiver, calling_context).instance_eval(&block)
end

Instance Method Details

- (Object) __proxy_method__(method, *args, &block)



239
240
241
242
243
244
245
246
247
248
249
# File 'sunspot/lib/sunspot/util.rb', line 239

def __proxy_method__(method, *args, &block)
  begin
    @__receiver__.__send__(method.to_sym, *args, &block)
  rescue ::NoMethodError => e
    begin
      @__calling_context__.__send__(method.to_sym, *args, &block)
    rescue ::NoMethodError
      raise(e)
    end
  end
end

- (Object) id



226
227
228
# File 'sunspot/lib/sunspot/util.rb', line 226

def id
  @__calling_context__.__send__(:id)
end

- (Object) sub(*args, &block)

Special case due to `Kernel#sub`’s existence



231
232
233
# File 'sunspot/lib/sunspot/util.rb', line 231

def sub(*args, &block)
  __proxy_method__(:sub, *args, &block)
end