Class: Untied::Consumer::Observer
- Inherits:
-
Object
- Object
- Untied::Consumer::Observer
- Includes:
- Singleton
- Defined in:
- lib/untied-consumer/observer.rb
Constant Summary collapse
- CALLBACKS =
[ :after_initialize, :after_find, :after_touch, :before_validation, :after_validation, :before_save, :around_save, :after_save, :before_create, :around_create, :after_create, :before_update, :around_update, :after_update, :before_destroy, :around_destroy, :after_destroy, :after_commit, :after_rollback ]
Class Method Summary collapse
- .define_observed_classes(classes) ⇒ Object
- .define_observed_service(service) ⇒ Object
- .observe(*args) ⇒ Object
Instance Method Summary collapse
-
#notify(*args) ⇒ Object
Calls the proper callback method if the current observer is configured to observe the event_name from service to the klass.
- #observed_classes ⇒ Object
- #observed_service ⇒ Object
Class Method Details
.define_observed_classes(classes) ⇒ Object
23 24 25 26 |
# File 'lib/untied-consumer/observer.rb', line 23 def define_observed_classes(classes) remove_possible_method(:observed_classes) define_method(:observed_classes, Proc.new { classes }) end |
.define_observed_service(service) ⇒ Object
28 29 30 31 |
# File 'lib/untied-consumer/observer.rb', line 28 def define_observed_service(service) remove_possible_method(:observed_service) define_method(:observed_service, Proc.new { service }) end |
.observe(*args) ⇒ Object
17 18 19 20 21 |
# File 'lib/untied-consumer/observer.rb', line 17 def observe(*args) from, classes = deal_with_args(*args) define_observed_classes(classes) define_observed_service(from) end |
Instance Method Details
#notify(*args) ⇒ Object
Calls the proper callback method if the current observer is configured to observe the event_name from service to the klass.
class MyObserver < Untied::Consumer::Observer
observe User, :from => :core
def after_create(model); end
end
MyObserver.instance.notify(:after_create, :user, :core, { :user => { } })
# => calls after create method
MyObserver.instance.notify(:after_update, :user, :core, { :user => { } })
# => doesn't calls after create method
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/untied-consumer/observer.rb', line 62 def notify(*args) return nil unless args.length == 4 event_name = args.shift klass = args.shift service = args.shift entity = args.shift return nil unless CALLBACKS.include? event_name return nil unless service == observed_service return nil unless observed_classes.include? klass return nil unless respond_to?(event_name) self.send(event_name, entity) end |
#observed_classes ⇒ Object
78 79 80 |
# File 'lib/untied-consumer/observer.rb', line 78 def observed_classes self.class.observed_classes end |
#observed_service ⇒ Object
82 83 84 |
# File 'lib/untied-consumer/observer.rb', line 82 def observed_service self.class.observed_service end |