ventable 1.0.0 → 1.2.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.
- checksums.yaml +5 -5
- data/.gitignore +3 -0
- data/.travis.yml +16 -8
- data/Gemfile +0 -2
- data/README.md +62 -26
- data/Rakefile +21 -2
- data/lib/ventable.rb +4 -4
- data/lib/ventable/event.rb +20 -23
- data/lib/ventable/version.rb +1 -1
- data/spec/spec_helper.rb +4 -10
- data/spec/ventable/ventable_spec.rb +86 -49
- data/ventable.gemspec +1 -0
- metadata +17 -4
- data/Gemfile.lock +0 -72
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 57b42bb96d2c3c0bac1f4fc8e97ed42b3ee5eb9494a9f36f04cea0c0888765e3
|
4
|
+
data.tar.gz: 13b3f1b9860d9b26b812bf0ff6b2daa4d318af0906d71d4e02a17577067e7eca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a032511c88f1f9f2f62f2a0b3b39a2f81337585550596634eabb875c34731bfe458477a4246e47e2624363041fccb92fc6e98b7628d5e4bb32a6a62ab058511
|
7
|
+
data.tar.gz: 396af327f6e393cc9db4efd5f12c2787eeec7fa2cb1f83f9c600e389bd8296bb3092713a5e9056b252acd9e1129e5703853e0099cc82f9a58646c0bc5b1532a0
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -1,10 +1,18 @@
|
|
1
1
|
language: ruby
|
2
2
|
rvm:
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
3
|
+
- 2.3.8
|
4
|
+
- 2.4.5
|
5
|
+
- 2.5.3
|
6
|
+
- 2.6.1
|
7
|
+
script:
|
8
|
+
- bundle exec rspec
|
9
|
+
env:
|
10
|
+
global:
|
11
|
+
- secure: mjjlvfSCJpmD06E5DIkqhaYwjGP85cyBeM8NrxLsUXuEYLVNvL8pKoL5GJVPE8oqnQ93j2egLXZw50ZER7rBWDJhdHZ1LosQEKJgljRNc/wjqyQBTXRr3ZBRpWxKO4SJMhM65lUp1UYzIQj8AJDNehuZIIngFBTy01sHsI+hs8g=
|
12
|
+
before_script:
|
13
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64
|
14
|
+
> ./cc-test-reporter
|
15
|
+
- chmod +x ./cc-test-reporter
|
16
|
+
- "./cc-test-reporter before-build"
|
17
|
+
after_script:
|
18
|
+
- "./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT"
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,14 +1,47 @@
|
|
1
|
-
|
2
|
-
[](https://badge.fury.io/rb/ventable)
|
3
|
+
[](https://travis-ci.org/kigster/ventable)
|
4
|
+
[](https://codeclimate.com/github/kigster/ventable)
|
5
|
+
[](https://rubygems.org/gems/ventable)
|
6
|
+
[](https://gitter.im/kigster/ventable)
|
7
|
+
|
8
|
+
[](https://codeclimate.com/github/kigster/ventable/test_coverage)
|
4
9
|
|
5
10
|
|
6
11
|
# Ventable
|
7
12
|
|
8
|
-
|
9
|
-
|
10
|
-
|
13
|
+
This gem is a variation of the [Observer Design Pattern](https://en.wikipedia.org/wiki/Observer_pattern).
|
14
|
+
|
15
|
+
In particular:
|
16
|
+
|
17
|
+
* Ventable requires creation of simple event classes that may carry data and be serialized.
|
18
|
+
* Observers can be grouped together, and notified within a pre-defined `Proc`. For example, using grouping some observers may be called within the boundaries of a database transaction, while others maybe called outside of it.
|
19
|
+
* Ventable allows both compile time and run time observer binding.
|
20
|
+
* Ventable calls specific method on each observer, using automatically derived method name from the event class. A generic `#handle_event` method is also supported.
|
21
|
+
|
22
|
+
Limitations:
|
23
|
+
|
24
|
+
* At the moment, Ventable can only notify observers within the current ruby process.
|
25
|
+
|
26
|
+
## Plugins
|
27
|
+
|
28
|
+
Ventable has several plugins that add various functionality on top of the basic event dispatch mechanism.
|
11
29
|
|
30
|
+
* [ventable-statsd](https://github.com/kigster/ventable-statsd) is an extension that allows notifying Statsd whenever an event occurs.
|
31
|
+
* [simple-feed](https://github.com/kigster/simple-feed) is a generic implementation of the activity feed concept commonly seen on social networks, and it integrates nicely with Ventable.
|
32
|
+
|
33
|
+
## Ruby Versions
|
34
|
+
|
35
|
+
This gem has been verified to work in the following ruby versions:
|
36
|
+
|
37
|
+
* MRI Ruby
|
38
|
+
* 1.9.3-p551
|
39
|
+
* 2.2
|
40
|
+
* 2.3
|
41
|
+
* 2.4
|
42
|
+
|
43
|
+
The gem also likely works with non-MRI rubies, but it has not been tested.
|
44
|
+
|
12
45
|
## Installation
|
13
46
|
|
14
47
|
Add this line to your application's Gemfile:
|
@@ -25,13 +58,16 @@ Or install it yourself as:
|
|
25
58
|
|
26
59
|
## Usage
|
27
60
|
|
28
|
-
1. Create your own plain ruby Event class that optionally carries some data important to the event. Include module
|
61
|
+
1. Create your own plain ruby Event class that optionally carries some data important to the event. Include module `Ventable::Event`.
|
62
|
+
|
29
63
|
2. Create one or more observers. Observer can be any class that implements event handler method as a class method, such as a
|
30
|
-
generic method
|
31
|
-
callback event would be
|
32
|
-
|
33
|
-
|
34
|
-
|
64
|
+
generic method `self.handle(event)` or a more specific method mapped to the event name: say for event UserRegistered the
|
65
|
+
callback event would be `self.handle_user_registered(event)`
|
66
|
+
|
67
|
+
3. Register your observers with the event using `notifies` event method, or register groups using `group` method, and then
|
68
|
+
use `notify` with options `inside: :group_name`
|
69
|
+
|
70
|
+
4. Instantiate your event class (optionally with some data), and call `publish` or, a deprecated `fire!` method.
|
35
71
|
|
36
72
|
## Example
|
37
73
|
|
@@ -62,7 +98,7 @@ end
|
|
62
98
|
AlarmSoundEvent.notifies SleepingPerson
|
63
99
|
|
64
100
|
# Create and fire the event
|
65
|
-
AlarmSoundEvent.new(Date.new).
|
101
|
+
AlarmSoundEvent.new(Date.new).publish
|
66
102
|
```
|
67
103
|
|
68
104
|
## Using #configure and groups
|
@@ -101,7 +137,7 @@ SomeEvent.configure do
|
|
101
137
|
notifies ObserverClass1, ObserverClass2, inside: :transaction
|
102
138
|
end
|
103
139
|
|
104
|
-
SomeEvent.new.
|
140
|
+
SomeEvent.new.publish
|
105
141
|
```
|
106
142
|
|
107
143
|
## Callback Method Name
|
@@ -119,23 +155,23 @@ using the following logic:
|
|
119
155
|
|
120
156
|
You should start by defining your event library for your application (list of events
|
121
157
|
that are important to you), you can place these files anywhere you like, such as
|
122
|
-
|
158
|
+
`lib/events` or `app/events`, etc.
|
123
159
|
|
124
|
-
It is recommended to
|
125
|
-
inside the
|
160
|
+
It is recommended to `configure` all events and their observers in the `event_initializer.rb` file,
|
161
|
+
inside the `config/ininitalizers` folder. You may need to require your events in that file also.
|
126
162
|
|
127
163
|
When your event is tied to a creation of a "first class objects", such as user registration,
|
128
164
|
it is recommended to create the User record first, commit it to the database, and then throw
|
129
|
-
a
|
130
|
-
their respective classes. For example, if you need to send an email to the user, have a
|
131
|
-
class observe the
|
132
|
-
class, instead of, say, registration controller directly calling
|
165
|
+
a `UserRegisteredEvent.new(user).publish`, and have all subsequent logic broeken into
|
166
|
+
their respective classes. For example, if you need to send an email to the user, have a `Mailer`
|
167
|
+
class observe the `UserRegisteredEvent`, and so all the mailing logic can live inside the `Mailer`
|
168
|
+
class, instead of, say, registration controller directly calling `Mailer.deliver_user_registration!(user)`.
|
133
169
|
The callback method will receive the event, that wraps the User instance, or any other useful data necessary.
|
134
170
|
|
135
171
|
## Integration with tests
|
136
172
|
|
137
173
|
There are times when it may be desirable to disable all eventing. For instance, when writing unit tests,
|
138
|
-
testing that events are
|
174
|
+
testing that events are published may be useful, but integrating with all observers adds complexity and confusion.
|
139
175
|
In these cases, Ventable may be globally disabled.
|
140
176
|
|
141
177
|
```ruby
|
@@ -153,14 +189,14 @@ Now in a spec file:
|
|
153
189
|
```ruby
|
154
190
|
describe "Stuff", eventing: false do
|
155
191
|
it 'does stuff' do
|
156
|
-
... my code that
|
192
|
+
... my code that publishes events, in isolation from event observers
|
157
193
|
end
|
158
194
|
|
159
|
-
it 'tests that events are
|
160
|
-
event = double(
|
195
|
+
it 'tests that events are published, using stubs' do
|
196
|
+
event = double(publish: true)
|
161
197
|
allow(MyEvent).to receive(:new).and_return(event)
|
162
198
|
... my code that should fire event
|
163
|
-
expect(event).to have_received(:
|
199
|
+
expect(event).to have_received(:publish)
|
164
200
|
end
|
165
201
|
end
|
166
202
|
|
data/Rakefile
CHANGED
@@ -1,3 +1,22 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
|
3
|
+
def shell(*args)
|
4
|
+
puts "running: #{args.join(' ')}"
|
5
|
+
system(args.join(' '))
|
6
|
+
end
|
7
|
+
|
8
|
+
task :clean do
|
9
|
+
shell('rm -rf pkg/ tmp/ coverage/ doc/ ' )
|
10
|
+
end
|
11
|
+
|
12
|
+
task :gem => [:build] do
|
13
|
+
shell('gem install pkg/*')
|
14
|
+
end
|
15
|
+
|
16
|
+
task :permissions => [ :clean ] do
|
17
|
+
shell("chmod -v o+r,g+r * */* */*/* */*/*/* */*/*/*/* */*/*/*/*/*")
|
18
|
+
shell("find . -type d -exec chmod o+x,g+x {} \\;")
|
19
|
+
end
|
20
|
+
|
21
|
+
task :build => :permissions
|
3
22
|
|
data/lib/ventable.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'ventable/version'
|
2
|
+
require 'ventable/event'
|
3
3
|
|
4
4
|
module Ventable
|
5
5
|
|
@@ -12,7 +12,7 @@ module Ventable
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def self.enabled?
|
15
|
-
|
15
|
+
!@disabled
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
@@ -21,7 +21,7 @@ class String
|
|
21
21
|
self.gsub(/::/, '/').
|
22
22
|
gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
|
23
23
|
gsub(/([a-z\d])([A-Z])/, '\1_\2').
|
24
|
-
tr(
|
24
|
+
tr('-', '_').
|
25
25
|
downcase
|
26
26
|
end
|
27
27
|
end
|
data/lib/ventable/event.rb
CHANGED
@@ -20,6 +20,8 @@ module ::Ventable
|
|
20
20
|
notify_observer_set(self.class.observers) if Ventable.enabled?
|
21
21
|
end
|
22
22
|
|
23
|
+
alias publish fire!
|
24
|
+
|
23
25
|
private
|
24
26
|
|
25
27
|
def notify_observer_set(observer_set)
|
@@ -28,25 +30,23 @@ module ::Ventable
|
|
28
30
|
around_block = observer_entry[:around_block]
|
29
31
|
inside_block = -> { notify_observer_set(observer_entry[:observers]) }
|
30
32
|
around_block.call(inside_block)
|
31
|
-
elsif observer_entry
|
32
|
-
notify_observer(observer_entry)
|
33
33
|
else
|
34
|
-
|
34
|
+
notify_observer(observer_entry)
|
35
35
|
end
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
39
|
def notify_observer(observer)
|
40
40
|
case observer
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
41
|
+
when Proc
|
42
|
+
observer.call(self)
|
43
|
+
else # class
|
44
|
+
notify_class_observer(observer)
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
48
|
def notify_class_observer(observer)
|
49
|
-
default_handler = self.class.default_callback_method
|
49
|
+
default_handler = self.class.send(:default_callback_method)
|
50
50
|
return observer.send(default_handler, self) if observer.respond_to?(default_handler)
|
51
51
|
return observer.send(:handle_event, self) if observer.respond_to?(:handle_event)
|
52
52
|
raise Ventable::Error.new("no suitable event handler method found for #{self.class} in observer #{observer} (try adding #{default_handler} to this observer)")
|
@@ -57,43 +57,40 @@ module ::Ventable
|
|
57
57
|
class_eval(&block)
|
58
58
|
end
|
59
59
|
|
60
|
-
def notifies(*observer_list, &block)
|
61
|
-
options = {}
|
62
|
-
options.merge! observer_list.pop if observer_list.last.is_a?(Hash)
|
60
|
+
def notifies(*observer_list, **options, &block)
|
63
61
|
observer_set = self.observers
|
64
62
|
if options[:inside]
|
65
63
|
observer_entry = self.find_observer_group(options[:inside])
|
66
|
-
if observer_entry.nil?
|
67
|
-
raise Ventable::Error.new("No group with name #{options[:inside]} found.")
|
68
|
-
end
|
64
|
+
raise Ventable::Error.new("No group with name #{options[:inside]} found.") if observer_entry.nil?
|
69
65
|
observer_set = observer_entry[:observers]
|
70
66
|
end
|
71
|
-
raise Ventable::Error.new("found nil observer in params #{observer_list.inspect}") if observer_list.any?
|
67
|
+
raise Ventable::Error.new("found nil observer in params #{observer_list.inspect}") if observer_list.any?(&:nil?)
|
72
68
|
observer_list.compact.each { |o| observer_set << o } unless observer_list.empty?
|
73
69
|
observer_set << block if block
|
74
70
|
end
|
75
71
|
|
76
72
|
def group(name, &block)
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
around_block: block,
|
82
|
-
observers: Set.new
|
83
|
-
}
|
73
|
+
self.observers << { name: name,
|
74
|
+
around_block: block,
|
75
|
+
observers: Set.new }
|
76
|
+
|
84
77
|
end
|
85
78
|
|
79
|
+
protected
|
80
|
+
|
86
81
|
def find_observer_group(name)
|
87
82
|
self.observers.find { |o| o.is_a?(Hash) && o[:name] == name }
|
88
83
|
end
|
89
84
|
|
85
|
+
private
|
86
|
+
|
90
87
|
# Determine method name to call when notifying observers from this event.
|
91
88
|
def default_callback_method
|
92
89
|
if respond_to?(:ventable_callback_method_name)
|
93
90
|
self.ventable_callback_method_name
|
94
91
|
else
|
95
92
|
target = self
|
96
|
-
method =
|
93
|
+
method = 'handle_' + target.name.gsub(/::/, '__').underscore.gsub(/_event/, '')
|
97
94
|
method.to_sym
|
98
95
|
end
|
99
96
|
end
|
data/lib/ventable/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -1,17 +1,11 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
#
|
6
|
-
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
1
|
+
require 'simplecov'
|
2
|
+
require 'rspec'
|
3
|
+
|
4
|
+
SimpleCov.start
|
7
5
|
|
8
|
-
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
|
9
|
-
require 'rubygems'
|
10
|
-
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
|
11
6
|
require 'ventable'
|
12
7
|
|
13
8
|
RSpec.configure do |config|
|
14
|
-
config.treat_symbols_as_metadata_keys_with_true_values = true
|
15
9
|
config.run_all_when_everything_filtered = true
|
16
10
|
config.filter_run :focus
|
17
11
|
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Ventable do
|
4
4
|
before do
|
@@ -26,13 +26,13 @@ describe Ventable do
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
describe
|
30
|
-
it
|
29
|
+
describe 'including Ventable::Event' do
|
30
|
+
it 'should create a class instance variable to keep observers' do
|
31
31
|
expect(TestEvent.observers).not_to be_nil
|
32
|
-
expect(TestEvent.observers.class.name).to eq(
|
32
|
+
expect(TestEvent.observers.class.name).to eq('Set')
|
33
33
|
end
|
34
34
|
|
35
|
-
it
|
35
|
+
it 'should see observers variable from instance methods' do
|
36
36
|
observers = nil
|
37
37
|
TestEvent.new.instance_eval do
|
38
38
|
observers = self.class.observers
|
@@ -40,7 +40,7 @@ describe Ventable do
|
|
40
40
|
expect(observers).to_not be_nil
|
41
41
|
end
|
42
42
|
|
43
|
-
it
|
43
|
+
it 'should maintain separate sets of observers for each event' do
|
44
44
|
class AnotherEvent
|
45
45
|
include Ventable::Event
|
46
46
|
end
|
@@ -48,19 +48,19 @@ describe Ventable do
|
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
|
-
describe
|
51
|
+
describe '#fire' do
|
52
52
|
before do
|
53
53
|
class TestEvent
|
54
54
|
include Ventable::Event
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
|
-
it
|
58
|
+
it 'should properly call a Proc observer' do
|
59
59
|
run_block = false
|
60
|
-
event
|
60
|
+
event = nil
|
61
61
|
TestEvent.notifies do |e|
|
62
62
|
run_block = true
|
63
|
-
event
|
63
|
+
event = e
|
64
64
|
end
|
65
65
|
expect(run_block).to eq(false)
|
66
66
|
expect(event).to be_nil
|
@@ -72,33 +72,69 @@ describe Ventable do
|
|
72
72
|
expect(event).not_to be_nil
|
73
73
|
end
|
74
74
|
|
75
|
-
|
76
|
-
|
77
|
-
class
|
78
|
-
|
75
|
+
describe 'class observer' do
|
76
|
+
before do
|
77
|
+
class TestEvent
|
78
|
+
class << self
|
79
|
+
attr_accessor :flag
|
80
|
+
end
|
81
|
+
|
82
|
+
self.flag = 'unset'
|
83
|
+
|
84
|
+
def flag=(value)
|
85
|
+
self.class.flag = value
|
86
|
+
end
|
79
87
|
end
|
80
|
-
|
81
|
-
|
82
|
-
self.
|
88
|
+
|
89
|
+
class TestEventObserver
|
90
|
+
def self.handle_test(event)
|
91
|
+
event.flag = 'boo'
|
92
|
+
end
|
83
93
|
end
|
84
|
-
end
|
85
94
|
|
86
|
-
|
87
|
-
|
88
|
-
|
95
|
+
class GlobalObserver
|
96
|
+
class << self
|
97
|
+
def handle_event(event)
|
98
|
+
puts event.inspect
|
99
|
+
end
|
100
|
+
end
|
89
101
|
end
|
102
|
+
|
103
|
+
TestEvent.notifies TestEventObserver, GlobalObserver
|
90
104
|
end
|
91
|
-
TestEvent.notifies TestEventObserver
|
92
|
-
expect(TestEvent.flag).to eq("unset")
|
93
105
|
|
94
|
-
TestEvent.new
|
95
|
-
|
106
|
+
let(:event) { TestEvent.new }
|
107
|
+
|
108
|
+
before do
|
109
|
+
expect(TestEvent.flag).to eq('unset')
|
110
|
+
expect(GlobalObserver).to receive(:handle_event).with(event)
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'should set the flag and call observers' do
|
114
|
+
event.fire!
|
115
|
+
expect(TestEvent.flag).to eq('boo')
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'should also fire via the publish alias' do
|
119
|
+
event.publish
|
120
|
+
end
|
121
|
+
|
122
|
+
context 'observer without a handler' do
|
123
|
+
before {
|
124
|
+
class ObserverWithoutAHandler
|
125
|
+
end
|
126
|
+
TestEvent.notifies ObserverWithoutAHandler
|
127
|
+
}
|
128
|
+
it 'should raise an exception' do
|
129
|
+
expect { event.publish }.to raise_error(Ventable::Error)
|
130
|
+
end
|
131
|
+
end
|
96
132
|
end
|
97
133
|
|
98
|
-
it
|
99
|
-
transaction_called
|
134
|
+
it 'should properly call a group of observers' do
|
135
|
+
transaction_called = false
|
100
136
|
transaction_completed = false
|
101
|
-
transaction
|
137
|
+
transaction = ->(observer_block) {
|
102
138
|
transaction_called = true
|
103
139
|
observer_block.call
|
104
140
|
transaction_completed = true
|
@@ -110,11 +146,11 @@ describe Ventable do
|
|
110
146
|
# this flag ensures that this block really runs inside
|
111
147
|
# the transaction group block
|
112
148
|
transaction_already_completed = false
|
113
|
-
event_inside
|
149
|
+
event_inside = nil
|
114
150
|
TestEvent.notifies inside: :transaction do |event|
|
115
|
-
observer_block_called
|
151
|
+
observer_block_called = true
|
116
152
|
transaction_already_completed = transaction_completed
|
117
|
-
event_inside
|
153
|
+
event_inside = event
|
118
154
|
end
|
119
155
|
|
120
156
|
expect(transaction_called).to be false
|
@@ -123,9 +159,9 @@ describe Ventable do
|
|
123
159
|
|
124
160
|
TestEvent.new.fire!
|
125
161
|
|
126
|
-
expect(transaction_called).to be
|
127
|
-
expect(observer_block_called).to be
|
128
|
-
expect(transaction_called).to be
|
162
|
+
expect(transaction_called).to be true
|
163
|
+
expect(observer_block_called).to be true
|
164
|
+
expect(transaction_called).to be true
|
129
165
|
expect(transaction_already_completed).to be false
|
130
166
|
expect(event_inside).to_not be_nil
|
131
167
|
expect(event_inside).to be_a(TestEvent)
|
@@ -148,7 +184,7 @@ describe Ventable do
|
|
148
184
|
end
|
149
185
|
end
|
150
186
|
|
151
|
-
describe
|
187
|
+
describe '#default_callback_method' do
|
152
188
|
before do
|
153
189
|
class SomeAwesomeEvent
|
154
190
|
include Ventable::Event
|
@@ -163,6 +199,7 @@ describe Ventable do
|
|
163
199
|
class SomeOtherStuffHappened
|
164
200
|
include Ventable::Event
|
165
201
|
end
|
202
|
+
|
166
203
|
class ClassWithCustomCallbackMethodEvent
|
167
204
|
include Ventable::Event
|
168
205
|
|
@@ -172,16 +209,16 @@ describe Ventable do
|
|
172
209
|
end
|
173
210
|
end
|
174
211
|
|
175
|
-
it
|
176
|
-
expect(SomeAwesomeEvent.default_callback_method).to eq(:handle_some_awesome)
|
177
|
-
expect(Blah::AnotherSweetEvent.default_callback_method).to eq(:handle_blah__another_sweet)
|
178
|
-
expect(SomeOtherStuffHappened.default_callback_method).to eq(:handle_some_other_stuff_happened)
|
179
|
-
expect(ClassWithCustomCallbackMethodEvent.default_callback_method).to eq(:handle_my_special_event)
|
212
|
+
it 'should properly set the callback method name' do
|
213
|
+
expect(SomeAwesomeEvent.send(:default_callback_method)).to eq(:handle_some_awesome)
|
214
|
+
expect(Blah::AnotherSweetEvent.send(:default_callback_method)).to eq(:handle_blah__another_sweet)
|
215
|
+
expect(SomeOtherStuffHappened.send(:default_callback_method)).to eq(:handle_some_other_stuff_happened)
|
216
|
+
expect(ClassWithCustomCallbackMethodEvent.send(:default_callback_method)).to eq(:handle_my_special_event)
|
180
217
|
end
|
181
218
|
end
|
182
219
|
|
183
|
-
describe
|
184
|
-
it
|
220
|
+
describe '#configure' do
|
221
|
+
it 'properly configures the event with observers' do
|
185
222
|
notified_observer = false
|
186
223
|
TestEvent.configure do
|
187
224
|
notifies do
|
@@ -192,11 +229,11 @@ describe Ventable do
|
|
192
229
|
expect(notified_observer).to be true
|
193
230
|
end
|
194
231
|
|
195
|
-
it
|
196
|
-
notified_observer
|
232
|
+
it 'configures observers with groups' do
|
233
|
+
notified_observer = false
|
197
234
|
called_transaction = false
|
198
235
|
TestEvent.configure do
|
199
|
-
group :transaction, &->(b){
|
236
|
+
group :transaction, &->(b) {
|
200
237
|
b.call
|
201
238
|
called_transaction = true
|
202
239
|
}
|
@@ -209,24 +246,24 @@ describe Ventable do
|
|
209
246
|
expect(called_transaction).to be true
|
210
247
|
end
|
211
248
|
|
212
|
-
it
|
249
|
+
it 'throws exception if :inside references unknown group' do
|
213
250
|
begin
|
214
251
|
TestEvent.configure do
|
215
252
|
notifies inside: :transaction do
|
216
253
|
# some stuff
|
217
254
|
end
|
218
255
|
end
|
219
|
-
fail
|
256
|
+
fail 'Shouldn\'t reach here, must throw a valid exception'
|
220
257
|
rescue Exception => e
|
221
258
|
expect(e.class).to eq(Ventable::Error)
|
222
259
|
end
|
223
260
|
end
|
224
|
-
it
|
261
|
+
it 'throws exception if nil observer added to the list' do
|
225
262
|
begin
|
226
263
|
TestEvent.configure do
|
227
264
|
notifies nil
|
228
265
|
end
|
229
|
-
fail
|
266
|
+
fail 'Shouldn\'t reach here, must throw a valid exception'
|
230
267
|
rescue Exception => e
|
231
268
|
expect(e.class).to eq(Ventable::Error)
|
232
269
|
end
|
data/ventable.gemspec
CHANGED
@@ -17,6 +17,7 @@ Gem::Specification.new do |gem|
|
|
17
17
|
gem.version = Ventable::VERSION
|
18
18
|
|
19
19
|
gem.add_development_dependency "rake"
|
20
|
+
gem.add_development_dependency "simplecov"
|
20
21
|
gem.add_development_dependency "rspec"
|
21
22
|
gem.add_development_dependency "rspec-mocks"
|
22
23
|
gem.add_development_dependency 'guard-rspec'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ventable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Konstantin Gredeskoul
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-02-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: simplecov
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rspec
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -78,7 +92,6 @@ files:
|
|
78
92
|
- ".rspec"
|
79
93
|
- ".travis.yml"
|
80
94
|
- Gemfile
|
81
|
-
- Gemfile.lock
|
82
95
|
- Guardfile
|
83
96
|
- LICENSE
|
84
97
|
- README.md
|
@@ -108,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
121
|
version: '0'
|
109
122
|
requirements: []
|
110
123
|
rubyforge_project:
|
111
|
-
rubygems_version: 2.
|
124
|
+
rubygems_version: 2.7.7
|
112
125
|
signing_key:
|
113
126
|
specification_version: 4
|
114
127
|
summary: Event/Observable design pattern in ruby
|
data/Gemfile.lock
DELETED
@@ -1,72 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
ventable (1.0.0)
|
5
|
-
|
6
|
-
GEM
|
7
|
-
remote: https://rubygems.org/
|
8
|
-
specs:
|
9
|
-
coderay (1.1.0)
|
10
|
-
diff-lcs (1.2.5)
|
11
|
-
ffi (1.9.10)
|
12
|
-
formatador (0.2.5)
|
13
|
-
guard (2.13.0)
|
14
|
-
formatador (>= 0.2.4)
|
15
|
-
listen (>= 2.7, <= 4.0)
|
16
|
-
lumberjack (~> 1.0)
|
17
|
-
nenv (~> 0.1)
|
18
|
-
notiffany (~> 0.0)
|
19
|
-
pry (>= 0.9.12)
|
20
|
-
shellany (~> 0.0)
|
21
|
-
thor (>= 0.18.1)
|
22
|
-
guard-compat (1.2.1)
|
23
|
-
guard-rspec (4.6.4)
|
24
|
-
guard (~> 2.1)
|
25
|
-
guard-compat (~> 1.1)
|
26
|
-
rspec (>= 2.99.0, < 4.0)
|
27
|
-
listen (3.0.5)
|
28
|
-
rb-fsevent (>= 0.9.3)
|
29
|
-
rb-inotify (>= 0.9)
|
30
|
-
lumberjack (1.0.10)
|
31
|
-
method_source (0.8.2)
|
32
|
-
nenv (0.2.0)
|
33
|
-
notiffany (0.0.8)
|
34
|
-
nenv (~> 0.1)
|
35
|
-
shellany (~> 0.0)
|
36
|
-
pry (0.10.3)
|
37
|
-
coderay (~> 1.1.0)
|
38
|
-
method_source (~> 0.8.1)
|
39
|
-
slop (~> 3.4)
|
40
|
-
rake (10.5.0)
|
41
|
-
rb-fsevent (0.9.7)
|
42
|
-
rb-inotify (0.9.5)
|
43
|
-
ffi (>= 0.5.0)
|
44
|
-
rspec (3.4.0)
|
45
|
-
rspec-core (~> 3.4.0)
|
46
|
-
rspec-expectations (~> 3.4.0)
|
47
|
-
rspec-mocks (~> 3.4.0)
|
48
|
-
rspec-core (3.4.1)
|
49
|
-
rspec-support (~> 3.4.0)
|
50
|
-
rspec-expectations (3.4.0)
|
51
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
52
|
-
rspec-support (~> 3.4.0)
|
53
|
-
rspec-mocks (3.4.1)
|
54
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
55
|
-
rspec-support (~> 3.4.0)
|
56
|
-
rspec-support (3.4.1)
|
57
|
-
shellany (0.0.1)
|
58
|
-
slop (3.6.0)
|
59
|
-
thor (0.19.1)
|
60
|
-
|
61
|
-
PLATFORMS
|
62
|
-
ruby
|
63
|
-
|
64
|
-
DEPENDENCIES
|
65
|
-
guard-rspec
|
66
|
-
rake
|
67
|
-
rspec
|
68
|
-
rspec-mocks
|
69
|
-
ventable!
|
70
|
-
|
71
|
-
BUNDLED WITH
|
72
|
-
1.11.2
|