Class: ChainOptions::Util::ContextBoundDelegate

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

Overview

Shamelessly copied from sunspot/sunspot with some alterations

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.



60
61
62
# File 'lib/chain_options/util.rb', line 60

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



79
80
81
# File 'lib/chain_options/util.rb', line 79

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

Class Method Details

.instance_eval_with_context(receiver, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/chain_options/util.rb', line 37

def instance_eval_with_context(receiver, &block)
  calling_context = eval("self", block.binding, __FILE__, __LINE__)

  parent_calling_context = calling_context.instance_eval do
    @__calling_context__ if defined?(@__calling_context__)
  end

  calling_context = parent_calling_context if parent_calling_context
  new(receiver, calling_context).instance_eval(&block)
end

Instance Method Details

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



87
88
89
90
91
92
93
94
95
# File 'lib/chain_options/util.rb', line 87

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

#idObject



64
65
66
67
68
69
70
71
72
# File 'lib/chain_options/util.rb', line 64

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

#respond_to_missing?(meth) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/chain_options/util.rb', line 83

def respond_to_missing?(meth)
  @__receiver__.respond_to?(meth) || @__calling_context__.respond_to?(meth)
end

#sub(*args, &block) ⇒ Object

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



75
76
77
# File 'lib/chain_options/util.rb', line 75

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