Module: Hijacker::MethodDefiner

Included in:
Hijacker
Defined in:
lib/hijacker/method_definer.rb

Instance Method Summary collapse

Instance Method Details

#define_hijacked(methods, receiver, uri) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/hijacker/method_definer.rb', line 4

def define_hijacked(methods, receiver, uri)
  methods.each do |met|
    receiver.send(:alias_method, :"__original_#{met}", :"#{met}")
    receiver.send(:undef_method, :"#{met}")
    writer = (met =~ /=$/)
    receiver.class_eval <<EOS
      def #{met}(#{writer ? 'arg' : '*args, &blk'})
        _args = #{writer ? '[arg]' : 'args'}
        _args += [blk] if block_given?
        begin
          __original_#{met}(#{writer ? 'arg' : '*args, &blk'}).tap do |retval|
            Hijacker.register :#{met}, _args, retval, nil, self, #{uri.inspect}
          end
        rescue=>error
          Hijacker.register :#{met}, _args, nil, error, self, #{uri.inspect}
          raise error
        end
      end
EOS
  end
end