on 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ rvm:
3
+ - ree
4
+ - "1.8.7"
5
+ - "1.9.2"
6
+ - "1.9.3"
7
+ - jruby-18mode
8
+ - jruby-19mode
9
+ - rbx-18mode
10
+ - rbx-19mode
11
+ - ruby-head
12
+ branches:
13
+ only:
14
+ - master
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,76 @@
1
+ = On {<img src="https://secure.travis-ci.org/neopoly/on.png?branch=master" alt="Build Status" />}[https://travis-ci.org/neopoly/on]
2
+
3
+ Dynamic callbacks for Ruby blocks.
4
+
5
+ Gem[https://rubygems.org/gems/on] |
6
+ Source[https://github.com/neopoly/on] |
7
+ Documentation[http://rubydoc.info/github/neopoly/on/master/file/README.rdoc]
8
+
9
+ Inspired by http://www.mattsears.com/articles/2011/11/27/ruby-blocks-as-dynamic-callbacks
10
+
11
+ == Usage
12
+
13
+ Basic usage.
14
+
15
+ require 'on'
16
+
17
+ def tweet(message, &block)
18
+ callback = On.new(:success, :failure, &block)
19
+ callback.call :success
20
+ rescue => e
21
+ callback.call :failure, e.message
22
+ end
23
+
24
+ tweet "hello world" do |callback|
25
+ callback.on :success do
26
+ # handle success
27
+ end
28
+ callback.on :failure do |error_message|
29
+ # handle error message
30
+ end
31
+ end
32
+
33
+
34
+ == Usage with proc
35
+
36
+ Syntatic sugar for creating an +on+ callback from Proc.
37
+
38
+ require 'on/proc'
39
+
40
+ def tweet(message, &block)
41
+ callback = block.on(:success, :failure)
42
+ callback.call :success
43
+ rescue => e
44
+ callback.call :failure, e.message
45
+ end
46
+
47
+ tweet "hello world" do |callback|
48
+ callback.on :success do
49
+ # handle success
50
+ end
51
+ callback.on :failure do |error_message|
52
+ # handle error message
53
+ end
54
+ end
55
+
56
+ == Installation
57
+
58
+ Add this line to your application's Gemfile:
59
+
60
+ gem 'on'
61
+
62
+ And then execute:
63
+
64
+ $ bundle
65
+
66
+ Or install it yourself as:
67
+
68
+ $ gem install on
69
+
70
+ == Contributing
71
+
72
+ 1. Fork it
73
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
74
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
75
+ 4. Push to the branch (`git push origin my-new-feature`)
76
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require 'bundler/setup'
2
+ require 'bundler/gem_tasks'
3
+
4
+ require 'rake/testtask'
5
+
6
+ task :default => :test
7
+
8
+ Rake::TestTask.new do |t|
9
+ t.libs << "test"
10
+ t.test_files = FileList['test/**/*_test.rb']
11
+ t.verbose = true
12
+ end
13
+
14
+ # RDoc
15
+ require 'rdoc/task'
16
+ RDoc::Task.new do |rdoc|
17
+ rdoc.title = "On"
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.main = 'README.rdoc'
20
+ rdoc.rdoc_files.include('README.rdoc', 'lib/**/*.rb')
21
+ end
data/lib/on/proc.rb ADDED
@@ -0,0 +1,26 @@
1
+ # Syntatic sugar for creating an +on+ callback from Proc.
2
+ #
3
+ # = Example
4
+ #
5
+ # require 'on/proc'
6
+ #
7
+ # def tweet(message, &block)
8
+ # callback = block.on(:success, :failure)
9
+ # callback.call :success
10
+ # rescue => e
11
+ # callback.call :failure, e.message
12
+ # end
13
+ #
14
+ # tweet "hello world" do |callback|
15
+ # callback.on :success do
16
+ # # handle success
17
+ # end
18
+ # callback.on :failure do |error_message|
19
+ # # handle error message
20
+ # end
21
+ # end
22
+ class Proc
23
+ def on(*callbacks)
24
+ On.new(*callbacks, &self)
25
+ end
26
+ end
data/lib/on/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ class On
2
+ VERSION = "0.0.1"
3
+ end
data/lib/on.rb ADDED
@@ -0,0 +1,63 @@
1
+ require 'on/version'
2
+ require 'set'
3
+
4
+ # Dynamic callbacks for Ruby blocks.
5
+ #
6
+ # = Example
7
+ #
8
+ # require 'on'
9
+ #
10
+ # def tweet(message, &block)
11
+ # callback = On.new(:success, :failure, &block)
12
+ # callback.call :success
13
+ # rescue => e
14
+ # callback.call :failure, e.message
15
+ # end
16
+ #
17
+ # tweet "hello world" do |callback|
18
+ # callback.on :success do
19
+ # # handle success
20
+ # end
21
+ # callback.on :failure do |error_message|
22
+ # # handle error message
23
+ # end
24
+ # end
25
+ class On
26
+ def initialize(*callbacks, &block)
27
+ raise ArgumentError, "please provide at least one callback" if callbacks.empty?
28
+ raise ArgumentError, "please provide a block" unless block
29
+ @callbacks = Set.new(callbacks)
30
+ @block = block
31
+ end
32
+
33
+ # Dispatch callback.
34
+ def call(name, *args)
35
+ validate_callback!(name)
36
+ @callback = Callback.new(name, args)
37
+ @block.call(self)
38
+ end
39
+
40
+ # Handle a callback.
41
+ def on(name, &block)
42
+ validate_callback!(name)
43
+ if @callback && @callback.name == name
44
+ block.call(*@callback.args)
45
+ end
46
+ end
47
+
48
+ Callback = Struct.new(:name, :args)
49
+
50
+ class InvalidCallback < StandardError # :nodoc:
51
+ def initialize(name)
52
+ super("Invalid callback #{name.inspect}")
53
+ end
54
+ end
55
+
56
+ private
57
+
58
+ def validate_callback!(name)
59
+ unless @callbacks.include?(name)
60
+ raise InvalidCallback, name
61
+ end
62
+ end
63
+ end
data/on.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'on/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "on"
8
+ gem.version = On::VERSION
9
+ gem.authors = ["Peter Suschlik"]
10
+ gem.email = ["ps@neopoly.de"]
11
+ gem.description = %q{Dynamic callbacks with Ruby blocks.}
12
+ gem.summary = %q{Inspired by http://www.mattsears.com/articles/2011/11/27/ruby-blocks-as-dynamic-callbacks}
13
+ gem.homepage = "https://github.com/neopoly/on"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency 'rake'
21
+ gem.add_development_dependency 'rdoc'
22
+
23
+ gem.add_development_dependency 'minitest'
24
+ gem.add_development_dependency 'testem'
25
+ end
data/on.rb ADDED
@@ -0,0 +1,52 @@
1
+ require 'minitest/autorun'
2
+ require 'testem'
3
+
4
+ class On
5
+ def initialize *callbacks
6
+ @callbacks = callbacks
7
+ end
8
+
9
+ def callback(*args)
10
+ @current = args
11
+ self
12
+ end
13
+
14
+ def method_missing method, *args, &block
15
+ if @callbacks.empty? || @callbacks.include?(method)
16
+ if @current[0] == method
17
+ yield *@current[1]
18
+ end
19
+ else
20
+ super
21
+ end
22
+ end
23
+ end
24
+
25
+ def tweet(ok, &block)
26
+ block.callbacks :success, :failure
27
+ if ok
28
+ block.callback :success, 23
29
+ else
30
+ block.callback :failure, 25
31
+ end
32
+ end
33
+
34
+ class Proc
35
+ def callbacks(*names)
36
+ @on = On.new(*names)
37
+ end
38
+
39
+ def callback(*args)
40
+ @on ||= On.new
41
+ call @on.callback(*args)
42
+ end
43
+ end
44
+
45
+ tweet([true, false].sample) do |on|
46
+ on.success do |number|
47
+ p :success => number
48
+ end
49
+ on.failure do |number|
50
+ p :fail => number
51
+ end
52
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'minitest/autorun'
2
+ require 'testem'
3
+
4
+ require 'on'
@@ -0,0 +1,81 @@
1
+ require 'helper'
2
+
3
+ class IntegrationTest < Testem
4
+
5
+ def tweet(message, &block)
6
+ callback = On.new(:success, :failure, &block)
7
+ case message
8
+ when NilClass
9
+ callback.call :failure, "blank"
10
+ when /^Sir,.*/
11
+ callback.call :success, message
12
+ end
13
+ end
14
+
15
+ let(:called) { [] }
16
+
17
+ before do
18
+ called.clear
19
+ end
20
+
21
+ test "it calls success" do
22
+ tweet "Sir, hello world" do |callback|
23
+ called! :method
24
+ callback.on :success do |message|
25
+ called! :success, message
26
+ end
27
+ callback.on :failure do |message|
28
+ called! :failure, message
29
+ end
30
+ end
31
+
32
+ assert_called [ :method ], [ :success, "Sir, hello world" ]
33
+ end
34
+
35
+ test "it calls failure" do
36
+ tweet nil do |callback|
37
+ called! :method
38
+ callback.on :success do |message|
39
+ called! :success, message
40
+ end
41
+ callback.on :failure do |message|
42
+ called! :failure, message
43
+ end
44
+ end
45
+
46
+ assert_called [ :method ], [ :failure, "blank" ]
47
+ end
48
+
49
+ test "no callback called" do
50
+ tweet "you're such a fool" do |callback|
51
+ called! :method
52
+ end
53
+
54
+ assert_called
55
+ end
56
+
57
+ test "invalid callback name" do
58
+ e = assert_raises On::InvalidCallback do
59
+ tweet "Sir, hi" do |callback|
60
+ called! :method
61
+
62
+ callback.on :invalid do
63
+ called! :invalid
64
+ end
65
+ end
66
+ end
67
+ assert_equal "Invalid callback :invalid", e.message
68
+
69
+ assert_called [:method]
70
+ end
71
+
72
+ private
73
+
74
+ def assert_called(*args)
75
+ assert_equal called, args
76
+ end
77
+
78
+ def called!(*args)
79
+ called << args
80
+ end
81
+ end
@@ -0,0 +1,49 @@
1
+ require 'helper'
2
+
3
+ require 'on/proc'
4
+
5
+ class OnProcTest < Testem
6
+ def oddeven(number, &block)
7
+ callback = block.on(:odd, :even)
8
+ if number % 2 == 0
9
+ callback.call :even
10
+ else
11
+ callback.call :odd
12
+ end
13
+ end
14
+
15
+ def verify(number)
16
+ oddeven(number) do |callback|
17
+ called! :block, number
18
+ callback.on :odd do
19
+ called! :odd
20
+ end
21
+ callback.on :even do
22
+ called! :even
23
+ end
24
+ end
25
+ end
26
+
27
+ let(:called) { [] }
28
+
29
+ before do
30
+ called.clear
31
+ end
32
+
33
+ test "call proc" do
34
+ verify(1)
35
+ verify(2)
36
+
37
+ assert_called [ :block, 1 ], [ :odd ], [ :block, 2 ], [ :even ]
38
+ end
39
+
40
+ private
41
+
42
+ def assert_called(*args)
43
+ assert_equal called, args
44
+ end
45
+
46
+ def called!(*args)
47
+ called << args
48
+ end
49
+ end
data/test/on_test.rb ADDED
@@ -0,0 +1,96 @@
1
+ require 'helper'
2
+
3
+ class OnTest < Testem
4
+ let(:called) { [] }
5
+
6
+ context "initialize" do
7
+ test "at least one callback" do
8
+ e = assert_raises ArgumentError do
9
+ On.new
10
+ end
11
+ assert_equal "please provide at least one callback", e.message
12
+ end
13
+
14
+ test "block missing" do
15
+ e = assert_raises ArgumentError do
16
+ On.new :success
17
+ end
18
+ assert_equal "please provide a block", e.message
19
+ end
20
+ end
21
+
22
+ context "with an instance" do
23
+ before do
24
+ called.clear
25
+ end
26
+
27
+ test "calls callback w/o args" do
28
+ on = On.new(:success) do |callback|
29
+ called! :block
30
+ callback.on :success do |*args|
31
+ called! :success, *args
32
+ end
33
+ end
34
+ on.call :success
35
+
36
+ assert_called [ :block ], [ :success ]
37
+ end
38
+
39
+ test "calls callback with args" do
40
+ on = On.new(:success) do |callback|
41
+ called! :block
42
+ callback.on :success do |*args|
43
+ called! :success, *args
44
+ end
45
+ end
46
+ on.call :success, :foo, :bar
47
+
48
+ assert_called [ :block ], [ :success, :foo, :bar ]
49
+ end
50
+
51
+ test "calls invalid callback" do
52
+ on = On.new(:correct) {}
53
+ e = assert_raises On::InvalidCallback do
54
+ on.call :wrong
55
+ end
56
+
57
+ assert_equal "Invalid callback :wrong", e.message
58
+ end
59
+
60
+ test "handles invalid callback" do
61
+ on = On.new(:correct) do |callback|
62
+ called! :block
63
+ callback.on :wrong
64
+ end
65
+ e = assert_raises On::InvalidCallback do
66
+ on.call :correct
67
+ end
68
+
69
+ assert_equal "Invalid callback :wrong", e.message
70
+ assert_called [ :block ]
71
+ end
72
+
73
+ test "does not explode when nothing called" do
74
+ on = On.new(:something) do
75
+ called! :block
76
+ end
77
+ on.on :something do
78
+ called! :something
79
+ end
80
+
81
+ assert_nothing_called
82
+ end
83
+ end
84
+
85
+ def called!(*args)
86
+ called << args
87
+ end
88
+
89
+ def assert_called(*args)
90
+ assert_equal called, args
91
+ end
92
+
93
+ def assert_nothing_called
94
+ assert_called
95
+ end
96
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: 'on'
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Peter Suschlik
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rdoc
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: minitest
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: testem
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Dynamic callbacks with Ruby blocks.
79
+ email:
80
+ - ps@neopoly.de
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .travis.yml
87
+ - Gemfile
88
+ - README.rdoc
89
+ - Rakefile
90
+ - lib/on.rb
91
+ - lib/on/proc.rb
92
+ - lib/on/version.rb
93
+ - on.gemspec
94
+ - on.rb
95
+ - test/helper.rb
96
+ - test/integration_test.rb
97
+ - test/on_proc_test.rb
98
+ - test/on_test.rb
99
+ homepage: https://github.com/neopoly/on
100
+ licenses: []
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ segments:
112
+ - 0
113
+ hash: 4033254695859622643
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ segments:
121
+ - 0
122
+ hash: 4033254695859622643
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 1.8.24
126
+ signing_key:
127
+ specification_version: 3
128
+ summary: Inspired by http://www.mattsears.com/articles/2011/11/27/ruby-blocks-as-dynamic-callbacks
129
+ test_files:
130
+ - test/helper.rb
131
+ - test/integration_test.rb
132
+ - test/on_proc_test.rb
133
+ - test/on_test.rb