ventable 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +48 -0
- data/lib/ventable.rb +12 -0
- data/lib/ventable/event.rb +4 -3
- data/lib/ventable/version.rb +1 -1
- data/spec/ventable/ventable_spec.rb +35 -0
- metadata +16 -24
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 081c78cd805ad7b5dce14dbe88ad7177240152c6
|
4
|
+
data.tar.gz: 2a1a56d21086cdf8ce6dab880b2911af9bd8eb20
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 81de27f259688d1132d67a6f0d47500c47ad10f1d5a9624c8f0890f49376cdc738ea9abc16cf958034579b50ce7614b7341d0112df4c2df2ef14fb377a5fab0e
|
7
|
+
data.tar.gz: 4e418cbc41c2c154b22be27f2cffaec76952da3e2dc49391b64c56ba18d862ef0859ccad5f74ed5c55349d9bf6f1697a823f862527188054cdf9ef3870163219
|
data/README.md
CHANGED
@@ -1,4 +1,7 @@
|
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/ventable.png)](http://badge.fury.io/rb/ventable)
|
1
2
|
[![Build status](https://secure.travis-ci.org/kigster/ventable.png)](http://travis-ci.org/kigster/ventable)
|
3
|
+
[![Code Climate](https://codeclimate.com/github/kigster/ventable.png)](https://codeclimate.com/github/kigster/ventable)
|
4
|
+
|
2
5
|
|
3
6
|
# Ventable
|
4
7
|
|
@@ -129,6 +132,47 @@ class observe the ```UserRegisteredEvent```, and so all the mailing logic can li
|
|
129
132
|
class, instead of, say, registration controller directly calling ```Mailer.deliver_user_registration!(user)```.
|
130
133
|
The callback method will receive the event, that wraps the User instance, or any other useful data necessary.
|
131
134
|
|
135
|
+
## Integration with tests
|
136
|
+
|
137
|
+
There are times when it may be desirable to disable all eventing. For instance, when writing unit tests,
|
138
|
+
testing that events are fired may be useful, but integrating with all observers adds complexity and confusion.
|
139
|
+
In these cases, Ventable may be globally disabled.
|
140
|
+
|
141
|
+
```ruby
|
142
|
+
## in spec_helper
|
143
|
+
|
144
|
+
around :each, eventing: false do |example|
|
145
|
+
Ventable.disable
|
146
|
+
example.run
|
147
|
+
Ventable.enable
|
148
|
+
end
|
149
|
+
```
|
150
|
+
|
151
|
+
Now in a spec file:
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
describe "Stuff", eventing: false do
|
155
|
+
it 'does stuff' do
|
156
|
+
... my code that fires events, in isolation from event observers
|
157
|
+
end
|
158
|
+
|
159
|
+
it 'tests that events are fired, using stubs' do
|
160
|
+
event = double(fire!: true)
|
161
|
+
allow(MyEvent).to receive(:new).and_return(event)
|
162
|
+
... my code that should fire event
|
163
|
+
expect(event).to have_received(:fire!)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
describe 'Other stuff' do
|
168
|
+
it 'actually calls through to all observers, so valid data is required' do
|
169
|
+
end
|
170
|
+
end
|
171
|
+
```
|
172
|
+
|
173
|
+
Note that in the version of RSpec that Ventable has been tested with, tags on a `describe` block
|
174
|
+
override tags on an `it` block.
|
175
|
+
|
132
176
|
## Further Discussion
|
133
177
|
|
134
178
|
It is worth mentioning that in the current form this gem is simply a software design pattern. It helps
|
@@ -138,6 +182,10 @@ but unrelated to each other (such as sending email to the user).
|
|
138
182
|
Future versions of this gem may offer a way to further decouple observers, by allowing them to be notified
|
139
183
|
via a background queue, such as Sidekiq or Resque. If you are interested in helping, please email the author.
|
140
184
|
|
185
|
+
For more information, check out the following blog post:
|
186
|
+
|
187
|
+
[Detangling Business Logic in Rails Apps with Pure Ruby Observers](http://building.wanelo.com/post/57442907639/detangling-business-logic-in-rails-apps-with-poro).
|
188
|
+
|
141
189
|
## Contributing
|
142
190
|
|
143
191
|
1. Fork it
|
data/lib/ventable.rb
CHANGED
data/lib/ventable/event.rb
CHANGED
@@ -17,7 +17,7 @@ module ::Ventable
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def fire!
|
20
|
-
notify_observer_set(self.class.observers)
|
20
|
+
notify_observer_set(self.class.observers) if Ventable.enabled?
|
21
21
|
end
|
22
22
|
|
23
23
|
private
|
@@ -46,9 +46,10 @@ module ::Ventable
|
|
46
46
|
end
|
47
47
|
|
48
48
|
def notify_class_observer(observer)
|
49
|
-
|
49
|
+
default_handler = self.class.default_callback_method
|
50
|
+
return observer.send(default_handler, self) if observer.respond_to?(default_handler)
|
50
51
|
return observer.send(:handle_event, self) if observer.respond_to?(:handle_event)
|
51
|
-
raise Ventable::Error.new("suitable event handler method found in observer #{
|
52
|
+
raise Ventable::Error.new("no suitable event handler method found for #{self.class} in observer #{observer} (try adding #{default_handler} to this observer)")
|
52
53
|
end
|
53
54
|
|
54
55
|
module ClassMethods
|
data/lib/ventable/version.rb
CHANGED
@@ -7,6 +7,25 @@ describe Ventable do
|
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
|
+
describe '::enabled?' do
|
11
|
+
after { Ventable.enable }
|
12
|
+
|
13
|
+
it 'is true by default' do
|
14
|
+
expect(Ventable.enabled?).to be_true
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'is false after Ventable is disabled' do
|
18
|
+
Ventable.disable
|
19
|
+
expect(Ventable.enabled?).to be_false
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'is true after Ventable is re-enabled' do
|
23
|
+
Ventable.disable
|
24
|
+
Ventable.enable
|
25
|
+
expect(Ventable.enabled?).to be_true
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
10
29
|
describe "including Ventable::Event" do
|
11
30
|
it "should create a class instance variable to keep observers" do
|
12
31
|
TestEvent.observers.should_not be_nil
|
@@ -113,6 +132,22 @@ describe Ventable do
|
|
113
132
|
event_inside.should_not be_nil
|
114
133
|
event_inside.should be_a(TestEvent)
|
115
134
|
end
|
135
|
+
|
136
|
+
context 'when globally disabled' do
|
137
|
+
before { Ventable.disable }
|
138
|
+
after { Ventable.enable }
|
139
|
+
|
140
|
+
it 'does not notify observers' do
|
141
|
+
observers_notified = false
|
142
|
+
|
143
|
+
TestEvent.notifies do |event|
|
144
|
+
observers_notified = true
|
145
|
+
end
|
146
|
+
|
147
|
+
TestEvent.new.fire!
|
148
|
+
expect(observers_notified).to be_false
|
149
|
+
end
|
150
|
+
end
|
116
151
|
end
|
117
152
|
|
118
153
|
describe "#default_callback_method" do
|
metadata
CHANGED
@@ -1,62 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ventable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.6
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Konstantin Gredeskoul
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-04-02 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rspec
|
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: rspec-mocks
|
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: guard-rspec
|
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
|
description: Event/Observable design pattern in ruby
|
@@ -66,9 +59,9 @@ executables: []
|
|
66
59
|
extensions: []
|
67
60
|
extra_rdoc_files: []
|
68
61
|
files:
|
69
|
-
- .gitignore
|
70
|
-
- .rspec
|
71
|
-
- .travis.yml
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
72
65
|
- Gemfile
|
73
66
|
- Guardfile
|
74
67
|
- LICENSE
|
@@ -82,27 +75,26 @@ files:
|
|
82
75
|
- ventable.gemspec
|
83
76
|
homepage: https://github.com/kigster/ventable
|
84
77
|
licenses: []
|
78
|
+
metadata: {}
|
85
79
|
post_install_message:
|
86
80
|
rdoc_options: []
|
87
81
|
require_paths:
|
88
82
|
- lib
|
89
83
|
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
-
none: false
|
91
84
|
requirements:
|
92
|
-
- -
|
85
|
+
- - ">="
|
93
86
|
- !ruby/object:Gem::Version
|
94
87
|
version: '0'
|
95
88
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
-
none: false
|
97
89
|
requirements:
|
98
|
-
- -
|
90
|
+
- - ">="
|
99
91
|
- !ruby/object:Gem::Version
|
100
92
|
version: '0'
|
101
93
|
requirements: []
|
102
94
|
rubyforge_project:
|
103
|
-
rubygems_version:
|
95
|
+
rubygems_version: 2.2.0
|
104
96
|
signing_key:
|
105
|
-
specification_version:
|
97
|
+
specification_version: 4
|
106
98
|
summary: Event/Observable design pattern in ruby
|
107
99
|
test_files:
|
108
100
|
- spec/spec_helper.rb
|