on 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1fcbfef233adfda1d5c83cc0cf03d2d885badc8f
4
+ data.tar.gz: e0e0c6018ff8575415dac387dd969bc760fb06d6
5
+ SHA512:
6
+ metadata.gz: 43c9760275254b810483f07a652b3ed16105ad29602d903ea1c912e2ceb33e745530257d173abb2bbf2bea4fd497f30604f0829f23e8527b1d08a1c9277fb171
7
+ data.tar.gz: 90f68dc570a1738a7c8f76a7af6a4e3ba7442692e7c15e30c9f140d3c9703355d7e904cd15912a472f3b90f27a07f25adc8cd6fbbedc34e789b1c6e3cceb901a
data/.travis.yml CHANGED
@@ -1,12 +1,20 @@
1
1
  language: ruby
2
+ sudo: false
3
+ cache: bundler
4
+ script: bundle exec rake test:all
2
5
  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
6
+ - 1.9.3
7
+ - 2.0
8
+ - 2.1
9
+ - 2.2
11
10
  - ruby-head
12
- script: rake test:all
11
+ - jruby
12
+ - jruby-head
13
+ - rbx-2
14
+ env:
15
+ global:
16
+ - JRUBY_OPTS='--dev -J-Xmx1024M'
17
+ matrix:
18
+ allow_failures:
19
+ - rvm: ruby-head
20
+ - rvm: jruby-head
data/README.md ADDED
@@ -0,0 +1,135 @@
1
+ [github]: https://github.com/neopoly/on
2
+ [doc]: http://rubydoc.info/github/neopoly/on/master/file/README.md
3
+ [gem]: https://rubygems.org/gems/on
4
+ [travis]: https://travis-ci.org/neopoly/on
5
+ [codeclimate]: https://codeclimate.com/github/neopoly/on
6
+ [inchpages]: https://inch-ci.org/github/neopoly/on
7
+
8
+ # On
9
+
10
+ [![Travis](https://img.shields.io/travis/neopoly/on.svg?branch=master)][travis]
11
+ [![Gem Version](https://img.shields.io/gem/v/on.svg)][gem]
12
+ [![Code Climate](https://img.shields.io/codeclimate/github/neopoly/on.svg)][codeclimate]
13
+ [![Test Coverage](https://codeclimate.com/github/neopoly/on/badges/coverage.svg)][codeclimate]
14
+ [![Inline docs](https://inch-ci.org/github/neopoly/on.svg?branch=master&style=flat)][inchpages]
15
+
16
+ Dynamic callbacks for Ruby blocks.
17
+
18
+ [Gem][gem] |
19
+ [Source][github] |
20
+ [Documentation][doc]
21
+
22
+ Inspired by
23
+ http://www.mattsears.com/articles/2011/11/27/ruby-blocks-as-dynamic-callbacks
24
+
25
+ ## Usage
26
+
27
+ Basic usage.
28
+
29
+ ```ruby
30
+ require 'on'
31
+
32
+ def tweet(message, &block)
33
+ callback = On.new(:success, :failure, &block)
34
+ callback.call :success
35
+ rescue => e
36
+ callback.call :failure, e.message
37
+ end
38
+
39
+ tweet "hello world" do |callback|
40
+ callback.on :success do
41
+ # handle success
42
+ end
43
+ callback.on :failure do |error_message|
44
+ # handle error message
45
+ end
46
+ end
47
+ ```
48
+
49
+ ## Usage with proc
50
+
51
+ Syntatic sugar for creating an `on` callback from Proc.
52
+
53
+ ```ruby
54
+ require 'on/proc'
55
+
56
+ def tweet(message, &block)
57
+ callback = block.on(:success, :failure)
58
+ callback.call :success
59
+ rescue => e
60
+ callback.call :failure, e.message
61
+ end
62
+
63
+ tweet "hello world" do |callback|
64
+ callback.on :success do
65
+ # handle success
66
+ end
67
+ callback.on :failure do |error_message|
68
+ # handle error message
69
+ end
70
+ end
71
+ ```
72
+
73
+ ## Testing with On::TestHelper
74
+
75
+ ```ruby
76
+ require 'minitest/autorun'
77
+ require 'on'
78
+ require 'on/test_helper'
79
+
80
+ class SomeTest < MiniTest::Spec
81
+ include On::TestHelper
82
+
83
+ let(:recorder) { On::TestHelper::Recorder.new }
84
+
85
+ it "records everything" do
86
+ on = On.new(:success, :failure, &recorder)
87
+ on.call :success, :some, :args
88
+ assert_callback recorder, :success, :some, :args
89
+ end
90
+
91
+ it "calls nothing" do
92
+ on = On.new(:success, :failure, &recorder)
93
+ # nothing called
94
+ refute_callbacks recorder
95
+ end
96
+
97
+ it "records everything manually" do
98
+ on = On.new(:success, :failure) do |result|
99
+ recorder.record_block
100
+ recorder.record_callback(result, :success, :failure)
101
+ end
102
+ on.call :success, :some, :args
103
+ assert_callback recorder, :success, :some, :args
104
+ end
105
+ end
106
+ ```
107
+
108
+ ## Installation
109
+
110
+ Add this line to your application's Gemfile:
111
+
112
+ ```ruby
113
+ gem 'on'
114
+ ```
115
+
116
+ And then execute:
117
+
118
+ ```shell
119
+ $ bundle
120
+ ```
121
+
122
+ Or install it yourself as:
123
+
124
+ ```shell
125
+ $ gem install on
126
+ ```
127
+
128
+ ## Contributing
129
+
130
+ 1. Fork it
131
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
132
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
133
+ 4. Push to the branch (`git push origin my-new-feature`)
134
+ 5. Create new Pull Request
135
+
@@ -56,12 +56,13 @@ class On
56
56
  raise ArgumentError, "Provide args or block but not both" if block_given? && !args.empty?
57
57
 
58
58
  callback = recorder.last_record
59
+ assertion_message = proc { "Callback was #{callback}" }
59
60
  assert callback, "No callbacks found"
60
- assert_equal name, callback.name, "Callback was #{callback}"
61
+ assert_equal name, callback.name, assertion_message
61
62
  if block_given?
62
63
  yield *callback.args
63
64
  else
64
- assert_equal args, callback.args, "Callback was #{callback}"
65
+ assert_equal args, callback.args, assertion_message
65
66
  end
66
67
  end
67
68
 
@@ -124,8 +125,9 @@ class On
124
125
  end
125
126
  end
126
127
 
127
- def callback_recorded(name, args)
128
- @callbacks << Callback.new(name, args)
128
+ def callback_recorded(name, args=[])
129
+ callback = Callback === name ? name : Callback.new(name, args)
130
+ @callbacks << callback
129
131
  end
130
132
 
131
133
  # Short-hand for &+recorder.record_all+.
data/lib/on/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class On
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
@@ -9,6 +9,11 @@ class OnTestHelperTest < Testem
9
9
  assert_callback recorder, :foo
10
10
  end
11
11
 
12
+ test "evaluates assertion messages lazily" do
13
+ record_boom :foo
14
+ assert_callback recorder, :foo
15
+ end
16
+
12
17
  test "match with args" do
13
18
  record :foo, :bar, :baz
14
19
  assert_callback recorder, :foo, :bar, :baz
@@ -99,6 +104,17 @@ class OnTestHelperTest < Testem
99
104
 
100
105
  private
101
106
 
107
+ class BoomCallback < On::TestHelper::Recorder::Callback
108
+ def to_s
109
+ raise "I should NOT be called"
110
+ end
111
+ end
112
+
113
+ def record_boom(name, *args)
114
+ callback = BoomCallback.new(name, args)
115
+ recorder.callback_recorded(callback)
116
+ end
117
+
102
118
  def record(name, *args)
103
119
  recorder.callback_recorded(name, args)
104
120
  end
metadata CHANGED
@@ -1,78 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: 'on'
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
5
- prerelease:
4
+ version: 0.3.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - Peter Suschlik
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-12-07 00:00:00.000000000 Z
11
+ date: 2015-02-11 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rake
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rdoc
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: minitest
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - ">="
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - ">="
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: testem
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - ">="
68
60
  - !ruby/object:Gem::Version
69
61
  version: '0'
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ! '>='
66
+ - - ">="
76
67
  - !ruby/object:Gem::Version
77
68
  version: '0'
78
69
  description: Dynamic callbacks with Ruby blocks.
@@ -82,17 +73,16 @@ executables: []
82
73
  extensions: []
83
74
  extra_rdoc_files: []
84
75
  files:
85
- - .gitignore
86
- - .travis.yml
76
+ - ".gitignore"
77
+ - ".travis.yml"
87
78
  - Gemfile
88
- - README.rdoc
79
+ - README.md
89
80
  - Rakefile
90
81
  - lib/on.rb
91
82
  - lib/on/proc.rb
92
83
  - lib/on/test_helper.rb
93
84
  - lib/on/version.rb
94
85
  - on.gemspec
95
- - on.rb
96
86
  - test/helper.rb
97
87
  - test/integration_test.rb
98
88
  - test/on_proc_test.rb
@@ -100,33 +90,26 @@ files:
100
90
  - test/on_test_helper_test.rb
101
91
  homepage: https://github.com/neopoly/on
102
92
  licenses: []
93
+ metadata: {}
103
94
  post_install_message:
104
95
  rdoc_options: []
105
96
  require_paths:
106
97
  - lib
107
98
  required_ruby_version: !ruby/object:Gem::Requirement
108
- none: false
109
99
  requirements:
110
- - - ! '>='
100
+ - - ">="
111
101
  - !ruby/object:Gem::Version
112
102
  version: '0'
113
- segments:
114
- - 0
115
- hash: 1483705294558873466
116
103
  required_rubygems_version: !ruby/object:Gem::Requirement
117
- none: false
118
104
  requirements:
119
- - - ! '>='
105
+ - - ">="
120
106
  - !ruby/object:Gem::Version
121
107
  version: '0'
122
- segments:
123
- - 0
124
- hash: 1483705294558873466
125
108
  requirements: []
126
109
  rubyforge_project:
127
- rubygems_version: 1.8.24
110
+ rubygems_version: 2.2.2
128
111
  signing_key:
129
- specification_version: 3
112
+ specification_version: 4
130
113
  summary: Inspired by http://www.mattsears.com/articles/2011/11/27/ruby-blocks-as-dynamic-callbacks
131
114
  test_files:
132
115
  - test/helper.rb
data/README.rdoc DELETED
@@ -1,110 +0,0 @@
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
- == Testing with On::TestHelper
57
-
58
- require 'minitest/autorun'
59
- require 'on'
60
- require 'on/test_helper'
61
-
62
- class SomeTest < MiniTest::Spec
63
- include On::TestHelper
64
-
65
- let(:recorder) { On::TestHelper::Recorder.new }
66
-
67
- it "records everything" do
68
- on = On.new(:success, :failure, &recorder)
69
- on.call :success, :some, :args
70
- assert_callback recorder, :success, :some, :args
71
- end
72
-
73
- it "calls nothing" do
74
- on = On.new(:success, :failure, &recorder)
75
- # nothing called
76
- refute_callbacks recorder
77
- end
78
-
79
- it "records everything manually" do
80
- on = On.new(:success, :failure) do |result|
81
- recorder.record_block
82
- recorder.record_callback(result, :success, :failure)
83
- end
84
- on.call :success, :some, :args
85
- assert_callback recorder, :success, :some, :args
86
- end
87
- end
88
-
89
-
90
- == Installation
91
-
92
- Add this line to your application's Gemfile:
93
-
94
- gem 'on'
95
-
96
- And then execute:
97
-
98
- $ bundle
99
-
100
- Or install it yourself as:
101
-
102
- $ gem install on
103
-
104
- == Contributing
105
-
106
- 1. Fork it
107
- 2. Create your feature branch (`git checkout -b my-new-feature`)
108
- 3. Commit your changes (`git commit -am 'Add some feature'`)
109
- 4. Push to the branch (`git push origin my-new-feature`)
110
- 5. Create new Pull Request
data/on.rb DELETED
@@ -1,52 +0,0 @@
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