watchable 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ /.rbenv-version
2
+ /pkg
3
+ Gemfile.lock
data/.kick ADDED
@@ -0,0 +1,15 @@
1
+ require "kicker/utils"
2
+
3
+ process do |files|
4
+ if files.any? { |f| /\.rb$/ =~ f }
5
+ execute "rake test"
6
+ end
7
+
8
+ files.clear
9
+ end
10
+
11
+ module Kicker::Utils
12
+ def log(message)
13
+ nil
14
+ end
15
+ end
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - jruby-18mode
7
+ - jruby-19mode
8
+ - jruby-head
9
+ - rbx-18mode
10
+ - rbx-19mode
11
+ - ree
12
+ - ruby-head
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source :rubygems
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem "rake"
7
+ end
data/README.markdown ADDED
@@ -0,0 +1,144 @@
1
+ [![Build Status](https://secure.travis-ci.org/jbarnette/watchable.png)](http://travis-ci.org/jbarnette/watchable)
2
+
3
+ # Watchable
4
+
5
+ A simple event/notification mixin, reluctantly extracted to a gem.
6
+ This is code I've had floating around for a few years now, but I've
7
+ also incorporated a few extras from node.js' [EventEmitter][ee],
8
+ [jQuery][jq], and [Backbone.Events][be].
9
+
10
+ [ee]: http://nodejs.org/api/events.html#events_class_events_eventemitter
11
+ [jq]: http://api.jquery.com/on
12
+ [be]: http://documentcloud.github.com/backbone/#Events
13
+
14
+ ## Examples
15
+
16
+ ### Fixtures
17
+
18
+ ```ruby
19
+ require "watchable"
20
+
21
+ class Frob
22
+ include Watchable
23
+ end
24
+
25
+ class Callable
26
+ def call *args
27
+ p :called! => args
28
+ end
29
+ end
30
+
31
+ ```
32
+
33
+ ### Watching and Firing
34
+
35
+ Events can have any number of watchers. Each watcher will be called
36
+ in order, and any args provided when the event is fired will be passed
37
+ along. Watchers will most commonly be blocks, but any object that
38
+ responds to `call` can be used instead.
39
+
40
+ ```ruby
41
+ frob = Frob.new
42
+
43
+ frob.on :twiddle do |name|
44
+ puts "#{name} twiddled the frob!"
45
+ end
46
+
47
+ frob.on :twiddle do |name|
48
+ puts "(not that there's anything wrong with that)"
49
+ end
50
+
51
+ frob.on :twiddle, Callable.new
52
+ frob.fire :twiddle, "John"
53
+ ```
54
+
55
+ #### Result
56
+
57
+ John twiddled the frob!
58
+ (not that there's anything wrong with that)
59
+ { :called! => ["John"] }
60
+
61
+ ### Watching Everything
62
+
63
+ Want to see every event? Register for `:all`. The first argument will
64
+ be the name of the fired event.
65
+
66
+ ```ruby
67
+ frob = Frob.new
68
+
69
+ frob.on :all do |event, culprit|
70
+ p :fired => [event, culprit]
71
+ end
72
+
73
+ frob.fire :foo, "John"
74
+ ```
75
+
76
+ #### Result
77
+
78
+ {:fired => [:foo, "John"]}
79
+
80
+ ### Watching Once
81
+
82
+ Only want to be notified the first time something happens? `once` is
83
+ like `on`, but fickle.
84
+
85
+ ```ruby
86
+ frob = Frob.new
87
+
88
+ frob.once :twiddle do
89
+ p :twiddled!
90
+ end
91
+
92
+ frob.fire :twiddle
93
+ frob.fire :twiddle
94
+ ```
95
+
96
+ #### Result
97
+
98
+ :twiddled!
99
+
100
+ ### Unwatching
101
+
102
+ Specific blocks or callable objects can be removed from an event's
103
+ watchers, or all the event's watchers can be removed.
104
+
105
+ ```ruby
106
+ b = lambda {}
107
+ frob = Frob.new
108
+
109
+ frob.on :twiddle, &b
110
+
111
+ frob.off :twiddle, b # removes the 'b' watcher, same as frob.off :twiddle, &b
112
+ frob.off :twiddle # removes all watchers for the 'twiddle' event
113
+ ```
114
+
115
+ ## Compatibility
116
+
117
+ Watchable is actively developed against MRI Ruby 1.8.7 as a least common
118
+ denominator, but is widely tested against other Ruby versions and
119
+ implementations. Check the [travis-ci][] page for details.
120
+
121
+ [travis-ci]: http://travis-ci.org/jbarnette/watchable
122
+
123
+ ## License (MIT)
124
+
125
+ Copyright 2012 John Barnette (john@jbarnette.com)
126
+
127
+ Permission is hereby granted, free of charge, to any person obtaining
128
+ a copy of this software and associated documentation files (the
129
+ 'Software'), to deal in the Software without restriction, including
130
+ without limitation the rights to use, copy, modify, merge, publish,
131
+ distribute, sublicense, and/or sell copies of the Software, and to
132
+ permit persons to whom the Software is furnished to do so, subject to
133
+ the following conditions:
134
+
135
+ The above copyright notice and this permission notice shall be
136
+ included in all copies or substantial portions of the Software.
137
+
138
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
139
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
140
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
141
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
142
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
143
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
144
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ desc "Run the tests."
4
+ task :test do
5
+ $: << "lib" << "test"
6
+ Dir["test/*_test.rb"].each { |f| require f[5..-4] }
7
+ end
8
+
9
+ task :default => :test
data/lib/watchable.rb ADDED
@@ -0,0 +1,34 @@
1
+ module Watchable
2
+ def watchers
3
+ @watchers ||= Hash.new { |h, k| h[k] = [] }
4
+ end
5
+
6
+ def fire event, *args
7
+ watchers[event].each { |w| w && w.call(*args) }
8
+ watchers[:all].each { |w| w && w.call(event, *args) }
9
+
10
+ self
11
+ end
12
+
13
+ def on event, callable = nil, &block
14
+ watchers[event] << (callable || block)
15
+
16
+ self
17
+ end
18
+
19
+ def once event, callable = nil, &block
20
+ wrapper = lambda do |*args|
21
+ off event, wrapper
22
+ (callable || block).call *args
23
+ end
24
+
25
+ on event, wrapper
26
+ end
27
+
28
+ def off event, callable = nil, &block
29
+ watcher = callable || block
30
+ watcher ? watchers[event].delete(watcher) : watchers[event].clear
31
+
32
+ self
33
+ end
34
+ end
@@ -0,0 +1,115 @@
1
+ require "minitest/autorun"
2
+ require "mocha"
3
+ require "watchable"
4
+
5
+ describe Watchable do
6
+ before do
7
+ @obj = Object.new
8
+ @obj.extend Watchable
9
+ end
10
+
11
+ it "has an empty list of watchers by default" do
12
+ assert @obj.watchers.empty?
13
+ end
14
+
15
+ it "returns an empty array of watchers for any event" do
16
+ assert_equal [], @obj.watchers[:foo]
17
+ end
18
+
19
+ describe :fire do
20
+ it "calls each watcher with optional args" do
21
+ @obj.on :foo, mock { expects(:call).with :bar, :baz }
22
+ @obj.fire :foo, :bar, :baz
23
+ end
24
+
25
+ it "calls multiple watchers in order" do
26
+ fires = sequence "fires"
27
+
28
+ @obj.on :foo, mock { expects(:call).in_sequence fires }
29
+ @obj.on :foo, mock { expects(:call).in_sequence fires }
30
+
31
+ @obj.fire :foo
32
+ end
33
+
34
+ it "ignores nil watchers" do
35
+ @obj.on :foo, nil
36
+ @obj.fire :foo
37
+ end
38
+
39
+ it "returns the watchable" do
40
+ assert_same @obj, @obj.fire(:foo)
41
+ end
42
+ end
43
+
44
+ describe :off do
45
+ it "can unregister a block" do
46
+ b = lambda {}
47
+
48
+ @obj.on :foo, &b
49
+ @obj.off :foo, &b
50
+
51
+ assert @obj.watchers[:foo].empty?
52
+ end
53
+
54
+ it "can unregister an object" do
55
+ b = lambda {}
56
+
57
+ @obj.on :foo, &b
58
+ @obj.off :foo, &b
59
+
60
+ assert @obj.watchers[:foo].empty?
61
+ end
62
+
63
+ it "can unregister all watchers for an event" do
64
+ @obj.on(:foo) {}
65
+ @obj.on(:foo) {}
66
+
67
+ assert_equal 2, @obj.watchers[:foo].size
68
+
69
+ @obj.off :foo
70
+ assert @obj.watchers[:foo].empty?
71
+ end
72
+
73
+ it "returns the watchable" do
74
+ assert_same @obj, @obj.off(:foo)
75
+ end
76
+ end
77
+
78
+ describe :on do
79
+ it "can register a block" do
80
+ b = lambda {}
81
+
82
+ @obj.on :foo, &b
83
+ assert_equal [b], @obj.watchers[:foo]
84
+ end
85
+
86
+ it "can register an object" do
87
+ b = lambda {}
88
+
89
+ @obj.on :foo, b
90
+ assert_equal [b], @obj.watchers[:foo]
91
+ end
92
+
93
+ it "can register for all events" do
94
+ @obj.on :all, mock { expects(:call).with :foo, :bar }
95
+ @obj.fire :foo, :bar
96
+ end
97
+
98
+ it "returns the watchable" do
99
+ assert_same @obj, @obj.on(:foo) {}
100
+ end
101
+ end
102
+
103
+ describe :once do
104
+ it "registers a watcher that's only called on the first fire" do
105
+ @obj.once :foo, mock { expects :call }
106
+
107
+ @obj.fire :foo
108
+ @obj.fire :foo
109
+ end
110
+
111
+ it "returns the watchable" do
112
+ assert_same @obj, @obj.once(:foo) {}
113
+ end
114
+ end
115
+ end
data/watchable.gemspec ADDED
@@ -0,0 +1,16 @@
1
+ Gem::Specification.new do |gem|
2
+ gem.authors = ["John Barnette"]
3
+ gem.email = ["john@jbarnette.com"]
4
+ gem.description = "A simple event mixin, reluctantly extracted to a gem."
5
+ gem.summary = "Watch an object for events."
6
+ gem.homepage = "https://github.com/jbarnette/watchable"
7
+
8
+ gem.files = `git ls-files`.split "\n"
9
+ gem.test_files = `git ls-files -- test/*`.split "\n"
10
+ gem.name = "watchable"
11
+ gem.require_paths = ["lib"]
12
+ gem.version = "0.0.0"
13
+
14
+ gem.add_development_dependency "minitest"
15
+ gem.add_development_dependency "mocha"
16
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: watchable
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 0
10
+ version: 0.0.0
11
+ platform: ruby
12
+ authors:
13
+ - John Barnette
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-03-08 00:00:00 -06:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: minitest
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: mocha
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ description: A simple event mixin, reluctantly extracted to a gem.
50
+ email:
51
+ - john@jbarnette.com
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files: []
57
+
58
+ files:
59
+ - .gitignore
60
+ - .kick
61
+ - .travis.yml
62
+ - Gemfile
63
+ - README.markdown
64
+ - Rakefile
65
+ - lib/watchable.rb
66
+ - test/watchable_test.rb
67
+ - watchable.gemspec
68
+ has_rdoc: true
69
+ homepage: https://github.com/jbarnette/watchable
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options: []
74
+
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ hash: 3
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ hash: 3
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ requirements: []
96
+
97
+ rubyforge_project:
98
+ rubygems_version: 1.6.2
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: Watch an object for events.
102
+ test_files:
103
+ - test/watchable_test.rb