Module: Kinda::Core::This
- Included in:
- Object
- Defined in:
- lib/core/this.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
49
50
51
52
53
54
55
|
# File 'lib/core/this.rb', line 49
def method_missing(method_name, *args, &block)
if this != self && this.contextual_method?(method_name)
this.send(method_name, *args, &block)
else
super
end
end
|
Instance Attribute Details
#contextual_methods ⇒ Object
Returns the value of attribute contextual_methods.
33
34
35
|
# File 'lib/core/this.rb', line 33
def contextual_methods
@contextual_methods
end
|
Instance Method Details
#call_with_this(proc, *args, &block) ⇒ Object
4
5
6
7
8
9
10
11
12
13
14
15
|
# File 'lib/core/this.rb', line 4
def call_with_this(proc, *args, &block)
return unless proc
binding_self = proc.binding.eval('self')
raise "Object #{binding_self.inspect} doesn't respond to Kinda::Core::This methods " +
"(proc=#{proc.inspect})" unless binding_self.respond_to?(:push_this)
begin
binding_self.push_this(self)
proc.call(*args, &block)
ensure
binding_self.pop_this
end
end
|
#contextual_method(*methods) ⇒ Object
35
36
37
38
39
|
# File 'lib/core/this.rb', line 35
def contextual_method(*methods)
methods.each do |method|
(@contextual_methods ||= []) << method.to_sym
end
end
|
#contextual_method?(method) ⇒ Boolean
41
42
43
44
45
46
47
|
# File 'lib/core/this.rb', line 41
def contextual_method?(method)
to_class.self_and_ancestors.each do |ancestor|
return true if ancestor.contextual_methods &&
ancestor.contextual_methods.include?(method.to_sym)
end
false
end
|
21
22
23
|
# File 'lib/core/this.rb', line 21
def pop_this
@these.pop if @these
end
|
#push_this(object) ⇒ Object
17
18
19
|
# File 'lib/core/this.rb', line 17
def push_this(object)
(@these ||= []).push(object)
end
|
25
26
27
|
# File 'lib/core/this.rb', line 25
def this
(@these && @these.last) || self
end
|
#this_eval(&block) ⇒ Object
29
30
31
|
# File 'lib/core/this.rb', line 29
def this_eval(&block)
this.instance_eval(&block)
end
|