ventable 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -24,8 +24,8 @@ Or install it yourself as:
24
24
 
25
25
  1. Create your own plain ruby Event class that optionally carries some data important to the event. Include module ```Ventable::Event```.
26
26
  2. Create one or more observers. Observer can be any class that implements event handler method as a class method, such as a
27
- generic method ```self.handle_event(event)``` or a more specific method mapped to the event name: say for event UserRegistered the
28
- callback event would be ```self.handle_user_registered_event(event)```
27
+ generic method ```self.handle(event)``` or a more specific method mapped to the event name: say for event UserRegistered the
28
+ callback event would be ```self.handle_user_registered(event)```
29
29
  3. Register your observers with the event using ```notifies``` event method, or register groups using ```group``` method, and then
30
30
  use ```notify``` with options ```inside: :group_name```
31
31
  4. Instantiate your event class (optionally with some data), and call ```fire!``` method.
@@ -48,7 +48,7 @@ end
48
48
 
49
49
  # This class is an observer, interested in WakeUpEvents.
50
50
  class SleepingPerson
51
- def self.handle_wake_up_event(event)
51
+ def self.handle_alarm_sound event
52
52
  self.wake_up
53
53
  puts "snoozing at #{event.wakeup_time}"
54
54
  self.snooze(5)
@@ -64,10 +64,10 @@ AlarmSoundEvent.new(Date.new).fire!
64
64
 
65
65
  ## Using #configure and groups
66
66
 
67
- Events can be configured to call observers in groups, with an optional block around it.
67
+ Events can be configured to call observers in groups, with an optional block around it. Using groups
68
+ allows you (as in this example) wrap some observers in a transaction, and control the order of notification.
68
69
 
69
70
  ```ruby
70
-
71
71
  transaction = ->(b){
72
72
  ActiveRecord::Base.transaction do
73
73
  b.call
@@ -90,7 +90,6 @@ SomeEvent.configure do
90
90
  # perform block
91
91
  end
92
92
 
93
-
94
93
  # this observer gets notified after all observers inside :transactions are notified
95
94
  notifies AnotherObserverClass
96
95
 
@@ -102,22 +101,33 @@ end
102
101
  SomeEvent.new.fire!
103
102
  ```
104
103
 
104
+ ## Callback Method Name
105
+
106
+ When the observer is notified, Ventable library will call a class method on your observer, with the name determined
107
+ using the following logic:
108
+
109
+ 1. If your event defines ```EventClass.ventable_callback_method_name``` method, it's return value is used as a method name.
110
+ 2. If not, your event's fully qualified class name is converted to a method name with underscrores. This method name
111
+ always begings with ```handle_```. For example, a class ```User::RegistrationEvent``` will generate callback
112
+ method name ```ObserverClass.handle_user__registration(event)``` (note that '::' is converted to two underscores).
113
+ 3. If neither method is found in the observer, a generic ```ObserverClass.handle(event)``` method is called.
114
+
105
115
  ## Guidelines for Using Ventable with Rails
106
116
 
107
117
  You should start by defining your event library for your application (list of events
108
118
  that are important to you), you can place these files anywhere you like, such as
109
119
  ```lib/events``` or ```app/events```, etc.
110
120
 
111
- It is recommended to configure all events and observers in the ```event_initializer.rb``` file,
112
- inside the ```config/ininitalizers``` folder.
121
+ It is recommended to ```configure``` all events and their observers in the ```event_initializer.rb``` file,
122
+ inside the ```config/ininitalizers``` folder. You may need to require your events in that file also.
113
123
 
114
124
  When your event is tied to a creation of a "first class objects", such as user registration,
115
125
  it is recommended to create the User record first, commit it to the database, and then throw
116
- a ```ruby UserRegisteredEvent.new(user).fire!```, and have all subsequent logic broeken into
117
- their respective classes. For example, if you need to send email to the user, have a ```Mailer```
118
- class observe the ```UserRegisteredEvent```, and so all the mailing logic can live in the ```Mailer```
119
- class, instead of, say, registration controller. The callback method will receive the event, that
120
- wraps the User instance, or any other useful data necessary.
126
+ a ```UserRegisteredEvent.new(user).fire!```, and have all subsequent logic broeken into
127
+ their respective classes. For example, if you need to send an email to the user, have a ```Mailer```
128
+ class observe the ```UserRegisteredEvent```, and so all the mailing logic can live inside the ```Mailer```
129
+ class, instead of, say, registration controller directly calling ```Mailer.deliver_user_registration!(user)```.
130
+ The callback method will receive the event, that wraps the User instance, or any other useful data necessary.
121
131
 
122
132
  ## Further Discussion
123
133
 
@@ -88,7 +88,7 @@ module ::Ventable
88
88
  self.ventable_callback_method_name
89
89
  else
90
90
  target = self
91
- method = "handle_" + target.name.gsub(/::/,'__').underscore.gsub(/_event/, '') + "_event"
91
+ method = "handle_" + target.name.gsub(/::/,'__').underscore.gsub(/_event/, '')
92
92
  method.to_sym
93
93
  end
94
94
  end
@@ -1,3 +1,3 @@
1
1
  module Ventable
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -1,5 +1,5 @@
1
1
  require 'ventable'
2
- require 'pp'
2
+
3
3
  describe Ventable do
4
4
  before do
5
5
  class TestEvent
@@ -66,7 +66,7 @@ describe Ventable do
66
66
  end
67
67
  end
68
68
  class TestEventObserver
69
- def self.handle_test_event event
69
+ def self.handle_test event
70
70
  event.set_flag!
71
71
  end
72
72
  end
@@ -138,9 +138,9 @@ describe Ventable do
138
138
  end
139
139
 
140
140
  it "should properly set the callback method name" do
141
- SomeAwesomeEvent.default_callback_method.should == :handle_some_awesome_event
142
- Blah::AnotherSweetEvent.default_callback_method.should == :handle_blah__another_sweet_event
143
- SomeOtherStuffHappened.default_callback_method.should == :handle_some_other_stuff_happened_event
141
+ SomeAwesomeEvent.default_callback_method.should == :handle_some_awesome
142
+ Blah::AnotherSweetEvent.default_callback_method.should == :handle_blah__another_sweet
143
+ SomeOtherStuffHappened.default_callback_method.should == :handle_some_other_stuff_happened
144
144
  ClassWithCustomCallbackMethodEvent.default_callback_method.should == :handle_my_special_event
145
145
  end
146
146
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ventable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: