Class: Sunspot::Util::ContextBoundDelegate

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

Constant Summary collapse

BASIC_METHODS =

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(receiver, calling_context) ⇒ ContextBoundDelegate

Returns a new instance of ContextBoundDelegate.



222
223
224
# File '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

#method_missing(method, *args, &block) ⇒ Object



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

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

Class Method Details

.instance_eval_with_context(receiver, &block) ⇒ Object



203
204
205
206
207
208
209
# File '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__ if defined?(@__calling_context__)}
    calling_context = parent_calling_context
  end
  new(receiver, calling_context).instance_eval(&block)
end

Instance Method Details

#__proxy_method__(method, *args, &block) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
# File '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

#idObject



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

def id
  @__calling_context__.__send__(:id)
end

#sub(*args, &block) ⇒ Object

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



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

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