sentinel 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +16 -2
- data/VERSION +1 -1
- data/lib/sentinel.rb +5 -1
- data/sentinel.gemspec +2 -2
- data/spec/fixtures/my_observer.rb +4 -0
- data/spec/fixtures/sentinel_subject.rb +9 -5
- data/spec/sentinel_spec.rb +18 -9
- metadata +3 -3
data/README.textile
CHANGED
@@ -16,7 +16,7 @@ $ [sudo] gem install sentinel
|
|
16
16
|
|
17
17
|
Then, add it as a dependency in your code using your favorite way (a simple require or mechanisms like the bundler gem).
|
18
18
|
|
19
|
-
To use it, first you'll need an observer class that responds to a class method called *_notify_
|
19
|
+
To use it, first you'll need an observer class that responds to a class method called *_notify_* by default. All the observed method's arguments will be passed to this method, hence the _args_ splat.
|
20
20
|
|
21
21
|
<pre>
|
22
22
|
class MyObserver
|
@@ -55,7 +55,21 @@ class MyClass
|
|
55
55
|
end
|
56
56
|
</pre>
|
57
57
|
|
58
|
-
And... that's it! Every time the observed method is called, the _notify_ method from the defined _observer class_ will be called *before* it, then it will run normally.
|
58
|
+
And... that's it! Every time the observed method is called, by default the _notify_ method from the defined _observer class_ will be called *before* it, then it will run normally.
|
59
|
+
|
60
|
+
You can also choose another method to call when the observed method is called, instead of the default _notify_ method:
|
61
|
+
|
62
|
+
<pre>
|
63
|
+
class MyObserver
|
64
|
+
include Sentinel
|
65
|
+
|
66
|
+
observe MyClass, :instance_method, :method_to_notify => :my_notifier
|
67
|
+
|
68
|
+
def self.my_notifier(*args)
|
69
|
+
puts "Do your thing!"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
</pre>
|
59
73
|
|
60
74
|
h2. Note on Patches/Pull Requests
|
61
75
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/lib/sentinel.rb
CHANGED
@@ -9,9 +9,13 @@ module Sentinel
|
|
9
9
|
def observe(class_name, method_name, options = {})
|
10
10
|
sentinel = self
|
11
11
|
|
12
|
+
options = {
|
13
|
+
:method_to_notify => :notify
|
14
|
+
}.merge(options)
|
15
|
+
|
12
16
|
body = <<-CODE
|
13
17
|
define_method "#{method_name}_with_observer" do |*args|
|
14
|
-
sentinel.
|
18
|
+
sentinel.send("#{options[:method_to_notify]}", *args)
|
15
19
|
self.send("#{method_name}_without_observer", *args)
|
16
20
|
end
|
17
21
|
|
data/sentinel.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{sentinel}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.3.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Lucas H\303\272ngaro"]
|
12
|
-
s.date = %q{2010-03-
|
12
|
+
s.date = %q{2010-03-26}
|
13
13
|
s.description = %q{Transparent (unobtrusive) Observers for your Ruby code}
|
14
14
|
s.email = %q{lucashungaro@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -2,23 +2,27 @@ class SentinelSubject
|
|
2
2
|
def instance_method
|
3
3
|
"hi from instance method!"
|
4
4
|
end
|
5
|
-
|
5
|
+
|
6
|
+
def new_method
|
7
|
+
"hi from new method!"
|
8
|
+
end
|
9
|
+
|
6
10
|
def self.class_method
|
7
11
|
"hi from class method!"
|
8
12
|
end
|
9
|
-
|
13
|
+
|
10
14
|
def instance_method_with_params(*params)
|
11
15
|
"hi from instance method with params!"
|
12
16
|
end
|
13
|
-
|
17
|
+
|
14
18
|
def self.class_method_with_params(*params)
|
15
19
|
"hi from class method with params!"
|
16
20
|
end
|
17
|
-
|
21
|
+
|
18
22
|
def instance_returning_something
|
19
23
|
42
|
20
24
|
end
|
21
|
-
|
25
|
+
|
22
26
|
def self.class_returning_something
|
23
27
|
42
|
24
28
|
end
|
data/spec/sentinel_spec.rb
CHANGED
@@ -6,7 +6,7 @@ describe Sentinel, "defining an observer for a method" do
|
|
6
6
|
before(:all) do
|
7
7
|
MyObserver.send(:include, Sentinel)
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
context "with the minimum required options" do
|
11
11
|
it "should notify the specified Observer when calling a class method" do
|
12
12
|
MyObserver.send(:observe, SentinelSubject, :instance_method)
|
@@ -14,24 +14,24 @@ describe Sentinel, "defining an observer for a method" do
|
|
14
14
|
|
15
15
|
SentinelSubject.new.instance_method
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
it "should notify the specified Observer when calling an instance method" do
|
19
19
|
MyObserver.send(:observe, SentinelSubject, :class_method, :class_method => true)
|
20
20
|
MyObserver.expects(:notify)
|
21
|
-
|
21
|
+
|
22
22
|
SentinelSubject.class_method
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
it "should not modify the observed methods output" do
|
26
26
|
MyObserver.send(:observe, SentinelSubject, :class_returning_something, :class_method => true)
|
27
27
|
MyObserver.send(:observe, SentinelSubject, :instance_returning_something)
|
28
|
-
|
28
|
+
|
29
29
|
MyObserver.expects(:notify).twice
|
30
30
|
|
31
31
|
SentinelSubject.class_returning_something.should == 42
|
32
32
|
SentinelSubject.new.instance_returning_something.should == 42
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
context "observing methods with parameters" do
|
36
36
|
it "should pass all parameters of the observed methods to the Observer" do
|
37
37
|
MyObserver.send(:observe, SentinelSubject, :class_method_with_params, :class_method => true)
|
@@ -43,20 +43,29 @@ describe Sentinel, "defining an observer for a method" do
|
|
43
43
|
SentinelSubject.new.instance_method_with_params("texto", 1)
|
44
44
|
end
|
45
45
|
end
|
46
|
+
|
47
|
+
context "observing methods using a different Observer method" do
|
48
|
+
it "should call the specified method in the observe call" do
|
49
|
+
MyObserver.send(:observe, SentinelSubject, :new_method, :method_to_notify => :another_method)
|
50
|
+
MyObserver.expects(:another_method)
|
51
|
+
|
52
|
+
SentinelSubject.new.new_method
|
53
|
+
end
|
54
|
+
end
|
46
55
|
end
|
47
|
-
|
56
|
+
|
48
57
|
context "without the minimum required options" do
|
49
58
|
it "should raise ArgumentError when the observer of an instance method is not defined" do
|
50
59
|
MyObserver.expects(:notify).never
|
51
60
|
SentinelSubject.expects(:observe).raises(ArgumentError)
|
52
|
-
|
61
|
+
|
53
62
|
SentinelSubject.send(:observe, :instance_method) rescue nil
|
54
63
|
end
|
55
64
|
|
56
65
|
it "should raise ArgumentError when the observer of a class method is not defined" do
|
57
66
|
MyObserver.expects(:notify).never
|
58
67
|
SentinelSubject.expects(:observe).raises(ArgumentError)
|
59
|
-
|
68
|
+
|
60
69
|
SentinelSubject.send(:observe, :class_method, :class_method => true) rescue nil
|
61
70
|
end
|
62
71
|
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 3
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.3.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- "Lucas H\xC3\xBAngaro"
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-03-
|
17
|
+
date: 2010-03-26 00:00:00 -03:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|