trigger 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +45 -0
- data/lib/trigger.rb +66 -0
- data/test/ts_all.rb +117 -0
- metadata +56 -0
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require "rake/rdoctask"
|
2
|
+
require "rake/testtask"
|
3
|
+
require "rake/gempackagetask"
|
4
|
+
|
5
|
+
require "rubygems"
|
6
|
+
|
7
|
+
task :default => [:test]
|
8
|
+
|
9
|
+
Rake::TestTask.new do |test|
|
10
|
+
test.libs << "test"
|
11
|
+
test.test_files = [ 'test/ts_all.rb' ]
|
12
|
+
test.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
Rake::RDocTask.new do |rdoc|
|
16
|
+
rdoc.main = 'README'
|
17
|
+
rdoc.rdoc_files.include 'README', 'lib/'
|
18
|
+
rdoc.rdoc_dir = 'doc'
|
19
|
+
rdoc.title = 'Trigger Documentation'
|
20
|
+
end
|
21
|
+
|
22
|
+
spec = Gem::Specification.new do |spec|
|
23
|
+
spec.name = "trigger"
|
24
|
+
spec.version = "0.0.1"
|
25
|
+
spec.platform = Gem::Platform::RUBY
|
26
|
+
spec.summary = "Trigger is a library for declaring and handling before and after events on method calls."
|
27
|
+
spec.files = Dir.glob("{lib,test}/**/*.rb").
|
28
|
+
delete_if { |item| item.include?("svn") } +
|
29
|
+
["Rakefile"]
|
30
|
+
spec.test_suite_file = "test/ts_all.rb"
|
31
|
+
spec.has_rdoc = false
|
32
|
+
spec.require_path = 'lib'
|
33
|
+
spec.autorequire = "trigger"
|
34
|
+
spec.author = "Michel Martens"
|
35
|
+
spec.email = "michel@soveran.org"
|
36
|
+
spec.homepage = "http://www.soveran.com"
|
37
|
+
spec.description = "A simple library for declaring and handling before and after events on method calls."
|
38
|
+
spec.add_dependency('metaid')
|
39
|
+
end
|
40
|
+
|
41
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
42
|
+
pkg.need_zip = true
|
43
|
+
pkg.need_tar = true
|
44
|
+
pkg.gem_spec = spec
|
45
|
+
end
|
data/lib/trigger.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'metaid'
|
3
|
+
|
4
|
+
# Allows the declaration of events to be triggered.
|
5
|
+
#
|
6
|
+
# Example:
|
7
|
+
#
|
8
|
+
# class User < Person
|
9
|
+
# def save
|
10
|
+
# ...
|
11
|
+
# end
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# user = User.new
|
15
|
+
# user.before(:save) { puts "Saving user..." }
|
16
|
+
# user.after(:save) { puts "User saved." }
|
17
|
+
# user.save
|
18
|
+
#
|
19
|
+
# The block yields the object itself, so it's possible to
|
20
|
+
# construct callbacks like this:
|
21
|
+
#
|
22
|
+
# user.before :save do |u|
|
23
|
+
# u.encrypt_password!
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
class Object
|
27
|
+
def before sym, &block
|
28
|
+
append_triggerable sym, :before, &block
|
29
|
+
end
|
30
|
+
|
31
|
+
def after sym, &block
|
32
|
+
append_triggerable sym, :after, &block
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def append_triggerable sym, moment, &block
|
38
|
+
update_triggerable_index sym, moment, &block
|
39
|
+
meta_def sym do |*attrs|
|
40
|
+
call_triggerable_methods sym, :before
|
41
|
+
result = call_triggerable_redefined_method(sym, *attrs)
|
42
|
+
call_triggerable_methods sym, :after
|
43
|
+
result
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def update_triggerable_index sym, moment, &block
|
48
|
+
initialize_triggerable_index unless @triggerable_index
|
49
|
+
@triggerable_index[sym][:old] = method(sym) if @triggerable_index[sym][:old].eql?([])
|
50
|
+
@triggerable_index[sym][moment] << block
|
51
|
+
end
|
52
|
+
|
53
|
+
def call_triggerable_methods sym, moment
|
54
|
+
@triggerable_index[sym][moment].each do |block|
|
55
|
+
block.call(self)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def call_triggerable_redefined_method sym, *attrs
|
60
|
+
@triggerable_index[sym][:old].call(*attrs)
|
61
|
+
end
|
62
|
+
|
63
|
+
def initialize_triggerable_index
|
64
|
+
@triggerable_index ||= Hash.new { |hash, key| hash[key] = Hash.new { |h, k| h[k] = Array.new }}
|
65
|
+
end
|
66
|
+
end
|
data/test/ts_all.rb
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
Dir.chdir File.dirname(__FILE__)
|
3
|
+
Dir.chdir '..'
|
4
|
+
require 'lib/trigger'
|
5
|
+
|
6
|
+
class Service
|
7
|
+
attr_accessor :status
|
8
|
+
attr_accessor :last_started
|
9
|
+
attr_accessor :working
|
10
|
+
attr_accessor :finalizer
|
11
|
+
|
12
|
+
def start
|
13
|
+
@status = 'Started'
|
14
|
+
end
|
15
|
+
|
16
|
+
def stop
|
17
|
+
@status = 'Stopped'
|
18
|
+
end
|
19
|
+
|
20
|
+
def change
|
21
|
+
@finalizer
|
22
|
+
end
|
23
|
+
|
24
|
+
def restart
|
25
|
+
start
|
26
|
+
end
|
27
|
+
|
28
|
+
def ping
|
29
|
+
end
|
30
|
+
|
31
|
+
def yielder
|
32
|
+
yield @status
|
33
|
+
end
|
34
|
+
|
35
|
+
def [] key
|
36
|
+
key
|
37
|
+
end
|
38
|
+
|
39
|
+
def method_missing method_symbol, *params
|
40
|
+
'1'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
class TestTrigger < Test::Unit::TestCase
|
46
|
+
def setup
|
47
|
+
@service = Service.new
|
48
|
+
@service.before :start do |service|
|
49
|
+
service.last_started = Time.now
|
50
|
+
service.working = true
|
51
|
+
end
|
52
|
+
|
53
|
+
@service.before :stop do |service|
|
54
|
+
service.working = false
|
55
|
+
end
|
56
|
+
|
57
|
+
@service.after :ping do
|
58
|
+
'pong'
|
59
|
+
end
|
60
|
+
|
61
|
+
@service.after :change do |service|
|
62
|
+
service.finalizer = true
|
63
|
+
end
|
64
|
+
|
65
|
+
@temp_status = ''
|
66
|
+
@service.before :yielder do |service|
|
67
|
+
@temp_status = service.status
|
68
|
+
service.status = "Hay!"
|
69
|
+
end
|
70
|
+
|
71
|
+
@service.after :yielder do |service|
|
72
|
+
service.status = @temp_status
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_trigger_subclassed_start
|
77
|
+
@service.start
|
78
|
+
assert_equal 'Started', @service.status
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_trigger_subclassed_start_triggered
|
82
|
+
@service.restart
|
83
|
+
assert_equal true, @service.working
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_trigger_subclassed_stop_triggered
|
87
|
+
@service.stop
|
88
|
+
assert_equal false, @service.working
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_trigger_subclassed_instance_restart_trigger
|
92
|
+
@last_started = nil
|
93
|
+
@service.on :restart do |service|
|
94
|
+
@last_started = service.last_started
|
95
|
+
end
|
96
|
+
@service.restart
|
97
|
+
assert_not_equal @last_started, @service.last_started
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_trigger_subclassed_return_value
|
101
|
+
assert_equal nil, @service.ping
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_trigger_changed_in_after_call_doesnt_affect_returned_value
|
105
|
+
assert_equal nil, @service.change
|
106
|
+
assert_equal true, @service.change
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_trigger_should_not_interefere_with_methods_that_accept_blocks
|
110
|
+
assert_nothing_raised do
|
111
|
+
@service.yielder do |i|
|
112
|
+
puts i
|
113
|
+
puts "Hello!"
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.0
|
3
|
+
specification_version: 1
|
4
|
+
name: trigger
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.0.1
|
7
|
+
date: 2007-08-23 00:00:00 -03:00
|
8
|
+
summary: Trigger is a library for declaring and handling before and after events on method calls.
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: michel@soveran.org
|
12
|
+
homepage: http://www.soveran.com
|
13
|
+
rubyforge_project:
|
14
|
+
description: A simple library for declaring and handling before and after events on method calls.
|
15
|
+
autorequire: trigger
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: false
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- Michel Martens
|
31
|
+
files:
|
32
|
+
- lib/trigger.rb
|
33
|
+
- test/ts_all.rb
|
34
|
+
- Rakefile
|
35
|
+
test_files:
|
36
|
+
- test/ts_all.rb
|
37
|
+
rdoc_options: []
|
38
|
+
|
39
|
+
extra_rdoc_files: []
|
40
|
+
|
41
|
+
executables: []
|
42
|
+
|
43
|
+
extensions: []
|
44
|
+
|
45
|
+
requirements: []
|
46
|
+
|
47
|
+
dependencies:
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: metaid
|
50
|
+
version_requirement:
|
51
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 0.0.0
|
56
|
+
version:
|