Module: Rinterceptor::ClassMethods

Defined in:
lib/rinterceptor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#exclude_i_methodsObject

Returns the value of attribute exclude_i_methods.



114
115
116
# File 'lib/rinterceptor.rb', line 114

def exclude_i_methods
  @exclude_i_methods
end

#exclude_s_methodsObject

Returns the value of attribute exclude_s_methods.



115
116
117
# File 'lib/rinterceptor.rb', line 115

def exclude_s_methods
  @exclude_s_methods
end

#include_i_methodsObject

target class’ methods



112
113
114
# File 'lib/rinterceptor.rb', line 112

def include_i_methods
  @include_i_methods
end

#include_s_methodsObject

Returns the value of attribute include_s_methods.



113
114
115
# File 'lib/rinterceptor.rb', line 113

def include_s_methods
  @include_s_methods
end

Instance Method Details

#included(base) ⇒ Object

for sub module



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rinterceptor.rb', line 53

def included(base)
  if base.is_a?(Class)
    rinter_before_include_class(base) #only available in last module for preparing data of Class which will include it
    base.extend(ClassMethods)
    base.ancestors[1..-1].reverse.each {|p| base.extend(p::ClassMethods) if p.respond_to?(:rinter_here?)}
    include_s_methods = base.include_s_methods || []
    include_i_methods = base.include_i_methods || []
    include_s_methods = RinterceptorHelper.process_include_methods(include_s_methods)
    include_i_methods = RinterceptorHelper.process_include_methods(include_i_methods)
    exclude_s_methods = (base.exclude_s_methods || []) + [/^rinter_/, /include_[is]_methods/, /exclude_[is]_methods/]
    exclude_i_methods = (base.exclude_i_methods || []) + [/^rinter_/, /include_[is]_methods/, /exclude_[is]_methods/]
    s_methods = (base.private_methods + base.singleton_methods).delete_if{|m| ! RinterceptorHelper.match_method?(m, include_s_methods, exclude_s_methods) }
    i_methods = (base.private_instance_methods + base.instance_methods).delete_if{|m| ! RinterceptorHelper.match_method?(m, include_i_methods, exclude_i_methods) }
    s_methods.each{|m| rinter_generate_proxy(base, m, include_s_methods.is_a?(Hash) ? include_s_methods : nil, true)}
    i_methods.each{|m| rinter_generate_proxy(base, m, include_i_methods.is_a?(Hash) ? include_i_methods : nil)}
    rinter_after_include_class(base)  #only available in last module for preparing data of Class which will include it
  else
    base.extend(ClassMethods)
  end
end

#rinter_after(method, result, e, *args) ⇒ Object

overridable



127
128
129
130
# File 'lib/rinterceptor.rb', line 127

def rinter_after(method, result, e, *args)
  raise e unless e.nil?
  result
end

#rinter_after_include_class(base) ⇒ Object

overridable only in last module



82
83
# File 'lib/rinterceptor.rb', line 82

def rinter_after_include_class(base)
end

#rinter_around(invocation) ⇒ Object

overridable



133
134
135
# File 'lib/rinterceptor.rb', line 133

def rinter_around(invocation)
  invocation.invoke
end

#rinter_before(method, *args) ⇒ Object

overridable



123
124
# File 'lib/rinterceptor.rb', line 123

def rinter_before(method, *args)
end

#rinter_before_include_class(base) ⇒ Object

overridable only in last module



78
79
# File 'lib/rinterceptor.rb', line 78

def rinter_before_include_class(base)
end

#rinter_generate_proxy(obj, method, methods, singleton = false) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rinterceptor.rb', line 85

def rinter_generate_proxy(obj, method, methods, singleton=false)
handler = nil
unless methods.nil?
  methods.each{|k, v| v.each{|vv| if method =~ vv then handler = method; break end }; unless handler.nil? then handler = k.nil? ? nil : "#{k}_"; break end}
  end
  obj.class_eval %{
  #{"class << self" if singleton}
  require "rinterceptor_invocation"
  alias_method :old4rinter_#{method}, :#{method} unless method_defined?(:old4rinter_#{method})
  def #{method}(*args)
    return old4rinter_#{method}(*args) if rinter_skip?
    rinter_before("#{method}", *args)
    result = nil
    e = nil
    invocation = RinterceptorInvocation.new(self, "#{method}", *args)
    begin
      result = rinter_#{handler}around(invocation)
    rescue => e
    end
    rinter_after("#{method}", result, e, *args)
    result
  end
  #{"end" if singleton}
}
end

#rinter_here?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/rinterceptor.rb', line 73

def rinter_here?
  true
end

#rinter_skip?Boolean

overridable

Returns:

  • (Boolean)


118
119
120
# File 'lib/rinterceptor.rb', line 118

def rinter_skip?
	false 
end