Module: Kinda::Core::This

Included in:
Object
Defined in:
lib/core/this.rb

Defined Under Namespace

Modules: ProcExtension

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



61
62
63
64
65
66
67
# File 'lib/core/this.rb', line 61

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_methodsObject (readonly)

Returns the value of attribute contextual_methods.



45
46
47
# File 'lib/core/this.rb', line 45

def contextual_methods
  @contextual_methods
end

Instance Method Details

#call_with_this(proc, *args, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/core/this.rb', line 16

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



47
48
49
50
51
# File 'lib/core/this.rb', line 47

def contextual_method(*methods)
  methods.each do |method|
    (@contextual_methods ||= []) << method.to_sym
  end
end

#contextual_method?(method) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
# File 'lib/core/this.rb', line 53

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

#pop_thisObject



33
34
35
# File 'lib/core/this.rb', line 33

def pop_this
  @these.pop if @these
end

#push_this(object) ⇒ Object



29
30
31
# File 'lib/core/this.rb', line 29

def push_this(object)
  (@these ||= []).push(object)
end

#thisObject



37
38
39
# File 'lib/core/this.rb', line 37

def this
  (@these && @these.last) || self
end

#this_eval(&block) ⇒ Object



41
42
43
# File 'lib/core/this.rb', line 41

def this_eval(&block)
  this.instance_eval(&block)
end