Module: Orientdb4r::Aop2::ClassMethods2

Defined in:
lib/orientdb4r/utils.rb

Instance Method Summary collapse

Instance Method Details

#add_hook(type, original_method, *hooks) ⇒ Object

!!



142
143
144
145
146
# File 'lib/orientdb4r/utils.rb', line 142

def add_hook(type, original_method, *hooks) #!!
  Array(original_method).each do |method|
    store_hook(type, method, *hooks)
  end
end

#after(original_method, *hooks) ⇒ Object



134
135
136
# File 'lib/orientdb4r/utils.rb', line 134

def after(original_method, *hooks)
  add_hook(:after, original_method, *hooks)
end

#around(original_method, *hooks) ⇒ Object



137
138
139
# File 'lib/orientdb4r/utils.rb', line 137

def around(original_method, *hooks)
  add_hook(:around, original_method, *hooks)
end

#before(original_method, *hooks) ⇒ Object



131
132
133
# File 'lib/orientdb4r/utils.rb', line 131

def before(original_method, *hooks)
  add_hook(:before, original_method, *hooks)
end

#init_aop_extensionObject



106
107
108
109
110
111
112
113
114
115
# File 'lib/orientdb4r/utils.rb', line 106

def init_aop_extension
  @hooks = {}
  [:before, :after, :around].each { |where| @hooks[where] = Hash.new { |hash, key| hash[key] = [] }}
  # will be like this:
  # {:before=>{:disconnect=>[:assert_connected], :query=>[:assert_connected], :command=>[:assert_connected]}, :after=>{}, :around=>{}}
  class << self
    attr_reader :hooks
  end
  @@redefining = false # flag whether the process of method redefining is running
end

#invoke_arround_hooks(obj, method_name, hooks, &block) ⇒ Object



192
193
194
195
196
197
198
# File 'lib/orientdb4r/utils.rb', line 192

def invoke_arround_hooks(obj, method_name, hooks, &block)
  hook = hooks.slice! 0
  return block.call if hook.nil? # call original method if no more hook

  # invoke the hook with lambda containing recursion
  obj.send(hook.to_sym) { invoke_arround_hooks(obj, method_name, hooks, &block); }
end

#invoke_hooks(obj, hook_type, method_name) ⇒ Object



189
190
191
# File 'lib/orientdb4r/utils.rb', line 189

def invoke_hooks(obj, hook_type, method_name)
  hooks[hook_type][method_name.to_sym].each { |hook| obj.send hook }
end

#is_hooked?(method) ⇒ Boolean

—————————————————————– Helpers

Returns:

  • (Boolean)


124
125
126
127
# File 'lib/orientdb4r/utils.rb', line 124

def is_hooked?(method)
  # look into array of keys (method names) in 2nd level hashs (see above)
  hooks.values.map(&:keys).flatten.uniq.include? method
end

#method_added(method) ⇒ Object



116
117
118
119
120
# File 'lib/orientdb4r/utils.rb', line 116

def method_added(method)
  unless @@redefining # avoid recursion
    redefine_method(method) if is_hooked?(method)
  end
end

#redefine_method(orig_method) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/orientdb4r/utils.rb', line 151

def redefine_method(orig_method)
  @@redefining = true

  arity = instance_method(orig_method.to_sym).arity
  params = ''
  fixed_cnt = arity.abs
  fixed_cnt -= 1 if arity < 0
  # build up a list of params
  1.upto(fixed_cnt).each {|x| params << "p#{x},"}
  params << "*argv" if arity < 0
  params.gsub!(/,$/, '') # remove last ','

  alias_method "#{orig_method}_aop2_orig".to_sym, orig_method.to_sym

  class_eval "    def \#{orig_method}(\#{params})\n      self.aop_context = { :method => '\#{orig_method}', :class => self.class }\n      begin\n        self.class.invoke_hooks(self, :before, :\#{orig_method})\n        rslt = self.class.invoke_arround_hooks(self, :\#{orig_method}, self.class.hooks[:around][:\#{orig_method}].clone) {\n          \#{orig_method}_aop2_orig(\#{params})\n        }\n        self.class.invoke_hooks(self, :after, :\#{orig_method})\n#            rescue Exception => e\n#              # TODO use logging\n#              $stderr.puts '' << e.class.name << ': ' << e.message\n#              $stderr.puts e.backtrace.inspect\n#              raise e\n      ensure\n        self.aop_context = nil\n      end\n      rslt\n    end\n  FILTER\n\n  @@redefining = false\nend\n",__FILE__,__LINE__ + 1

#store_hook(type, method_name, *hook_methods) ⇒ Object

!!



147
148
149
# File 'lib/orientdb4r/utils.rb', line 147

def store_hook(type, method_name, *hook_methods) #!!
  hooks[type.to_sym][method_name.to_sym] += hook_methods.flatten.map(&:to_sym)
end