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.



271
272
273
# File 'lib/sunspot/util.rb', line 271

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, **kwargs, &block) ⇒ Object



292
293
294
# File 'lib/sunspot/util.rb', line 292

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

Class Method Details

.instance_eval_with_context(receiver, &block) ⇒ Object



252
253
254
255
256
257
258
# File 'lib/sunspot/util.rb', line 252

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, **kwargs, &block) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/sunspot/util.rb', line 300

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

#idObject



275
276
277
278
279
280
281
282
283
284
285
# File 'lib/sunspot/util.rb', line 275

def id
  begin
    @__calling_context__.__send__(:id)
  rescue ::NoMethodError => e
    begin
      @__receiver__.__send__(:id)
    rescue ::NoMethodError
      raise(e)
    end
  end
end

#respond_to_missing?(method, _) ⇒ Boolean

Returns:

  • (Boolean)


296
297
298
# File 'lib/sunspot/util.rb', line 296

def respond_to_missing?(method, _)
  @__receiver__.respond_to?(method, true) || super
end

#sub(*args, &block) ⇒ Object

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



288
289
290
# File 'lib/sunspot/util.rb', line 288

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