sentinel 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.textile +71 -0
- data/Rakefile +47 -0
- data/VERSION +1 -0
- data/lib/sentinel.rb +34 -0
- data/sentinel.gemspec +62 -0
- data/spec/fixtures/my_observer.rb +5 -0
- data/spec/fixtures/subject.rb +25 -0
- data/spec/sentinel_spec.rb +63 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- metadata +90 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Lucas HĂșngaro
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.textile
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
h1. Sentinel
|
2
|
+
|
3
|
+
Transparent (unobtrusive) Observers for your Rubies.
|
4
|
+
|
5
|
+
h2. Why?
|
6
|
+
|
7
|
+
Developed for an specific need we had at "Busk":http://www.busk.com, Sentinel is a very small library that provides a way to add what we call "Transparent Observers" to your Ruby code. This means that you do not need to modify the observed methods (following the most common implementation of Observers), just use a mixin and declare your observers.
|
8
|
+
|
9
|
+
h2. How?
|
10
|
+
|
11
|
+
First, install the gem:
|
12
|
+
|
13
|
+
<pre>
|
14
|
+
$ [sudo] gem install sentinel
|
15
|
+
</pre>
|
16
|
+
|
17
|
+
Then, add it as a dependency in your code using your favorite way (a simple require or mechanisms the bundler gem).
|
18
|
+
|
19
|
+
To use it, first you'll need an observer class that responds to a method called *_notify_* (which can be a class or instance method). All the observed method's arguments will be passed to this method, hence the _args_ splat.
|
20
|
+
|
21
|
+
<pre>
|
22
|
+
class MyObserver
|
23
|
+
def self.notify(*args)
|
24
|
+
puts "Do your thing!"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
</pre>
|
28
|
+
|
29
|
+
Then, simple include the _Sentinel_ module in the class and declare the observer. Sentinel can observe both class and instance methods:
|
30
|
+
|
31
|
+
<pre>
|
32
|
+
class MyClass
|
33
|
+
include Sentinel
|
34
|
+
|
35
|
+
def instance_method
|
36
|
+
puts "Hi from the instance method!"
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.class_method
|
40
|
+
puts "Hi from the class method!"
|
41
|
+
end
|
42
|
+
|
43
|
+
observe :instance_method, :notify => MyObserver
|
44
|
+
observe :class_method, :notify => MyObserver, :class_method => true #to observe a class method
|
45
|
+
end
|
46
|
+
</pre>
|
47
|
+
|
48
|
+
If we wanted to use instances of the Observer instead of the class itself, the declarations would be:
|
49
|
+
|
50
|
+
<pre>
|
51
|
+
observe :instance_method, :notify => MyObserver.new
|
52
|
+
observe :class_method, :notify => MyObserver.new, :class_method => true #to observe a class method
|
53
|
+
</pre>
|
54
|
+
|
55
|
+
In summary, any object that responds to *_notify_* will be accepted.
|
56
|
+
|
57
|
+
And... that's it! Every time the observed method is called, the _notify_ method from the defined Observer will be called *before* it, then it will run normally.
|
58
|
+
|
59
|
+
h2. Note on Patches/Pull Requests
|
60
|
+
|
61
|
+
* Fork the project.
|
62
|
+
* Make your feature addition or bug fix.
|
63
|
+
* Add tests for it. This is important so I don't break it in a
|
64
|
+
future version unintentionally.
|
65
|
+
* Commit, do not mess with rakefile, version, or history.
|
66
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
67
|
+
* Send me a pull request. Bonus points for topic branches.
|
68
|
+
|
69
|
+
h3. Copyright
|
70
|
+
|
71
|
+
Copyright (c) 2010 Lucas HĂșngaro. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'rubygems'
|
3
|
+
require 'rake'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'jeweler'
|
7
|
+
Jeweler::Tasks.new do |gem|
|
8
|
+
gem.name = "sentinel"
|
9
|
+
gem.summary = %Q{Transparent (unobtrusive) Observers for your Ruby code}
|
10
|
+
gem.description = %Q{Transparent (unobtrusive) Observers for your Ruby code}
|
11
|
+
gem.email = "lucashungaro@gmail.com"
|
12
|
+
gem.homepage = "http://github.com/lucashungaro/sentinel"
|
13
|
+
gem.authors = ["Lucas HĂșngaro"]
|
14
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
15
|
+
gem.add_development_dependency "mocha", ">= 0.9.8"
|
16
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
17
|
+
end
|
18
|
+
Jeweler::GemcutterTasks.new
|
19
|
+
rescue LoadError
|
20
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
21
|
+
end
|
22
|
+
|
23
|
+
require 'spec/rake/spectask'
|
24
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
25
|
+
spec.libs << 'lib' << 'spec'
|
26
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
27
|
+
end
|
28
|
+
|
29
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
30
|
+
spec.libs << 'lib' << 'spec'
|
31
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
32
|
+
spec.rcov = true
|
33
|
+
end
|
34
|
+
|
35
|
+
task :spec => :check_dependencies
|
36
|
+
|
37
|
+
task :default => :spec
|
38
|
+
|
39
|
+
require 'rake/rdoctask'
|
40
|
+
Rake::RDocTask.new do |rdoc|
|
41
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
42
|
+
|
43
|
+
rdoc.rdoc_dir = 'rdoc'
|
44
|
+
rdoc.title = "sentinel #{version}"
|
45
|
+
rdoc.rdoc_files.include('README*')
|
46
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
47
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/sentinel.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
module Sentinel
|
2
|
+
def self.included(base)
|
3
|
+
base.extend(ClassMethods)
|
4
|
+
end
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def observe(method_name, options = {})
|
8
|
+
unless options[:notify]
|
9
|
+
raise ArgumentError.new("You should provide the :notify option")
|
10
|
+
end
|
11
|
+
body = <<-CODE
|
12
|
+
define_method "#{method_name}_with_observer" do |*args|
|
13
|
+
options[:notify].notify(*args)
|
14
|
+
self.send("#{method_name}_without_observer", *args)
|
15
|
+
end
|
16
|
+
|
17
|
+
alias_method "#{method_name}_without_observer", method_name
|
18
|
+
alias_method method_name, "#{method_name}_with_observer"
|
19
|
+
CODE
|
20
|
+
|
21
|
+
if options[:class_method]
|
22
|
+
|
23
|
+
metaclass = class << self; self; end
|
24
|
+
|
25
|
+
metaclass.instance_eval do
|
26
|
+
eval body
|
27
|
+
end
|
28
|
+
|
29
|
+
else
|
30
|
+
eval body
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/sentinel.gemspec
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{sentinel}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Lucas H\303\272ngaro"]
|
12
|
+
s.date = %q{2010-01-19}
|
13
|
+
s.description = %q{Transparent (unobtrusive) Observers for your Ruby code}
|
14
|
+
s.email = %q{lucashungaro@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.textile"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.textile",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"lib/sentinel.rb",
|
27
|
+
"sentinel.gemspec",
|
28
|
+
"spec/fixtures/my_observer.rb",
|
29
|
+
"spec/fixtures/subject.rb",
|
30
|
+
"spec/sentinel_spec.rb",
|
31
|
+
"spec/spec.opts",
|
32
|
+
"spec/spec_helper.rb"
|
33
|
+
]
|
34
|
+
s.homepage = %q{http://github.com/lucashungaro/sentinel}
|
35
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.rubygems_version = %q{1.3.5}
|
38
|
+
s.summary = %q{Transparent (unobtrusive) Observers for your Ruby code}
|
39
|
+
s.test_files = [
|
40
|
+
"spec/fixtures/my_observer.rb",
|
41
|
+
"spec/fixtures/subject.rb",
|
42
|
+
"spec/sentinel_spec.rb",
|
43
|
+
"spec/spec_helper.rb"
|
44
|
+
]
|
45
|
+
|
46
|
+
if s.respond_to? :specification_version then
|
47
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
48
|
+
s.specification_version = 3
|
49
|
+
|
50
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
51
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
52
|
+
s.add_development_dependency(%q<mocha>, [">= 0.9.8"])
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
55
|
+
s.add_dependency(%q<mocha>, [">= 0.9.8"])
|
56
|
+
end
|
57
|
+
else
|
58
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
59
|
+
s.add_dependency(%q<mocha>, [">= 0.9.8"])
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class Subject
|
2
|
+
def instance_method
|
3
|
+
"hi from instance method!"
|
4
|
+
end
|
5
|
+
|
6
|
+
def self.class_method
|
7
|
+
"hi from class method!"
|
8
|
+
end
|
9
|
+
|
10
|
+
def instance_method_with_params(*params)
|
11
|
+
"hi from instance method with params!"
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.class_method_with_params(*params)
|
15
|
+
"hi from class method with params!"
|
16
|
+
end
|
17
|
+
|
18
|
+
def instance_returning_something
|
19
|
+
42
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.class_returning_something
|
23
|
+
42
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/fixtures/my_observer')
|
3
|
+
require File.expand_path(File.dirname(__FILE__) + '/fixtures/subject')
|
4
|
+
|
5
|
+
describe Sentinel, "defining an observer for a method" do
|
6
|
+
before(:all) do
|
7
|
+
Subject.send(:include, Sentinel)
|
8
|
+
end
|
9
|
+
|
10
|
+
context "with the minimum required options" do
|
11
|
+
it "should notify the specified Observer when calling a class method" do
|
12
|
+
Subject.send(:observe, :instance_method, :notify => MyObserver)
|
13
|
+
MyObserver.expects(:notify)
|
14
|
+
|
15
|
+
Subject.new.instance_method
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should notify the specified Observer when calling an instance method" do
|
19
|
+
Subject.send(:observe, :class_method, :notify => MyObserver, :class_method => true)
|
20
|
+
MyObserver.expects(:notify)
|
21
|
+
|
22
|
+
Subject.class_method
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should not modify the observed methods output" do
|
26
|
+
Subject.send(:observe, :class_returning_something, :notify => MyObserver, :class_method => true)
|
27
|
+
Subject.send(:observe, :instance_returning_something, :notify => MyObserver)
|
28
|
+
|
29
|
+
MyObserver.expects(:notify).twice
|
30
|
+
|
31
|
+
Subject.class_returning_something.should == 42
|
32
|
+
Subject.new.instance_returning_something.should == 42
|
33
|
+
end
|
34
|
+
|
35
|
+
context "observing methods with parameters" do
|
36
|
+
it "should pass all parameters of the observed methods to the Observer" do
|
37
|
+
Subject.send(:observe, :class_method_with_params, :notify => MyObserver, :class_method => true)
|
38
|
+
Subject.send(:observe, :instance_method_with_params, :notify => MyObserver)
|
39
|
+
|
40
|
+
MyObserver.expects(:notify).twice.with("texto", 1)
|
41
|
+
|
42
|
+
Subject.class_method_with_params("texto", 1)
|
43
|
+
Subject.new.instance_method_with_params("texto", 1)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context "without the minimum required options" do
|
49
|
+
it "should raise ArgumentError when the observer of an instance method is not defined" do
|
50
|
+
MyObserver.expects(:notify).never
|
51
|
+
Subject.expects(:observe).raises(ArgumentError)
|
52
|
+
|
53
|
+
Subject.send(:observe, :instance_method) rescue nil
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should raise ArgumentError when the observer of a class method is not defined" do
|
57
|
+
MyObserver.expects(:notify).never
|
58
|
+
Subject.expects(:observe).raises(ArgumentError)
|
59
|
+
|
60
|
+
Subject.send(:observe, :class_method, :class_method => true) rescue nil
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sentinel
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "Lucas H\xC3\xBAngaro"
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-19 00:00:00 -02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.2.9
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: mocha
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.9.8
|
34
|
+
version:
|
35
|
+
description: Transparent (unobtrusive) Observers for your Ruby code
|
36
|
+
email: lucashungaro@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.textile
|
44
|
+
files:
|
45
|
+
- .document
|
46
|
+
- .gitignore
|
47
|
+
- LICENSE
|
48
|
+
- README.textile
|
49
|
+
- Rakefile
|
50
|
+
- VERSION
|
51
|
+
- lib/sentinel.rb
|
52
|
+
- sentinel.gemspec
|
53
|
+
- spec/fixtures/my_observer.rb
|
54
|
+
- spec/fixtures/subject.rb
|
55
|
+
- spec/sentinel_spec.rb
|
56
|
+
- spec/spec.opts
|
57
|
+
- spec/spec_helper.rb
|
58
|
+
has_rdoc: true
|
59
|
+
homepage: http://github.com/lucashungaro/sentinel
|
60
|
+
licenses: []
|
61
|
+
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options:
|
64
|
+
- --charset=UTF-8
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: "0"
|
72
|
+
version:
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: "0"
|
78
|
+
version:
|
79
|
+
requirements: []
|
80
|
+
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 1.3.5
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: Transparent (unobtrusive) Observers for your Ruby code
|
86
|
+
test_files:
|
87
|
+
- spec/fixtures/my_observer.rb
|
88
|
+
- spec/fixtures/subject.rb
|
89
|
+
- spec/sentinel_spec.rb
|
90
|
+
- spec/spec_helper.rb
|