Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 113 lines (76 sloc) 3.631 kB
e8bd2c3 @txus first commit
authored
1 #hijacker
2
3 A little gem that hijacks any ruby object and broadcasts all its activity
4 to a particular hijacker server. Useful for logging and those awfully hardcore
5 debugging afternoons! There might be other uses to it, for sure. Just be
6 creative :)
7
8 Hijacker is tested with Ruby 1.8.7, 1.9.2, JRuby 1.5.3 and Rubinius 1.1.
9
10 ##Install and configure
11
12 In your Gemfile:
13
14 gem 'hijacker'
15
16 If you are using Rails, you might want to put this configuration snippet in an
17 initializer or something (you can always put it in any other part of the code
18 otherwise, as long as it's before hijacking any object):
19
20 Hijacker.configure do
21 uri '<YOUR HIJACKER SERVER URI>'
22 end
23
24 And that's it! Oooor not. You have to spawn your server. In the command line:
25
26 hijacker
27
28 And it will output the URI for this server. *Note this* and pass it to your
29 configuration block!
30
31 Some options you can pass to the server command:
32
33 hijacker --without-timestamps (don't show the timestamps)
34 hijacker --without-classes (don't show the object classes)
35 hijacker --port 1234 (spawn the server in port 1234 rather than 8787)
36
37 ##Ok, and now for some hijacking action!
38
39 require 'hijacker' # You don't have to when using Bundler :)
40
41 class MyClass
42 def foo(bar, baz)
43 bar + baz
44 end
45 end
46
47 some_object = Object.new
48
49 # These are the important lines:
50
51 Hijacker.spy(some_object)
52 Hijacker.spy(MyClass)
53
54 instance = MyClass.new
55 instance.foo(3, 4)
56
0dc8c0c @txus update readme
authored
57 Run this code and, if you look at the server output, you'll see nothing less than...
e8bd2c3 @txus first commit
authored
58
59 <a nice timestamp> MyClass (Class) received :new and returned #<MyClass:0x000000874> (MyClass)
0dc8c0c @txus update readme
authored
60 <a nice timestamp> #<MyClass:0x000000874> (MyClass) received :foo with 3 (Fixnum), 4 (Fixnum) and returned 7 (Fixnum)
e8bd2c3 @txus first commit
authored
61
62 If you want to un-hijack any object, just call #restore:
63
64 Hijacker.restore(MyClass)
65 Hijacker.restore(some_object)
66
67 If you don't want to have to remember every hijacked object you have to call
68 restore on it, you can just spy a particular object within the duration of a block:
69
70 Hijacker.spying(MyClass) do
71 # inside this block, MyClass will be spied
72 end
73 # here not anymore
74
75 Awesome! You can fine-tune your spying, for example by only spying on instance
76 methods or singleton methods only:
77
78 Hijacker.spy(MyClass, :only => :instance_methods) # or :singleton_methods
79
80 And, last but not least... you can specify a *particular hijacker server* for
81 a *particular object* you are spying on!
82
83 # All activity on MyClass and its instances will
84 # be sent to druby://localhost:9999
85 Hijacker.spy(MyClass, :uri => 'druby://localhost:9999')
86
87 # But for example, the activity of some_object will
88 # be sent to the default uri specified in the configuration
89 # back earlier (remember?)
90 Hijacker.spy(some_object)
91
92 Of course, you can specify a particular server uri for a block, with #spying:
93
94 Hijacker.spying(foo_object, :uri => 'druby://localhost:1234') do
95 # all the activity of foo_object inside this block
96 # will be sent to the hijacker server on druby://localhost:1234
97 end
98
99 ##Note on Patches/Pull Requests
100
101 * Fork the project.
102 * Make your feature addition or bug fix.
103 * Add specs for it. This is important so I don't break it in a
104 future version unintentionally.
105 * Commit, do not mess with rakefile, version, or history.
106 If you want to have your own version, that is fine but bump version
107 in a commit by itself I can ignore when I pull.
108 * Send me a pull request. Bonus points for topic branches.
109
110 ## Copyright
111
112 Copyright (c) 2010 Josep M. Bach. See LICENSE for details.
Something went wrong with that request. Please try again.