module Lanes::Concerns::PubSub
Event subscription and publishing for Models Every model has certain built-in events (:save, :create, :update, :destroy) And may also implement custom events that reflect the models domain @example Send an email when a customer's name is updated
Customer.observe(:update) do |customer| Mailer.notify_billing(customer).deliver if customer.name_changed? end
@example Update some stats when a Sku's qty is changed
Sku.observe(:qty_changed) do | sku, location, old_qty, new_qty | Stats.refresh( location ) end
Protected Instance Methods
fire_after_create_pubsub_events()
click to toggle source
# File lib/lanes/concerns/pub_sub.rb, line 105 def fire_after_create_pubsub_events fire_pubsub_event(:create, self) end
fire_after_destroy_pubsub_events()
click to toggle source
# File lib/lanes/concerns/pub_sub.rb, line 97 def fire_after_destroy_pubsub_events fire_pubsub_event(:update, self) end
fire_after_save_pubsub_events()
click to toggle source
# File lib/lanes/concerns/pub_sub.rb, line 109 def fire_after_save_pubsub_events fire_pubsub_event(:save, self) end
fire_after_update_pubsub_events()
click to toggle source
# File lib/lanes/concerns/pub_sub.rb, line 101 def fire_after_update_pubsub_events fire_pubsub_event(:update, self) end
fire_pubsub_event(name, *arguments)
click to toggle source
# File lib/lanes/concerns/pub_sub.rb, line 113 def fire_pubsub_event(name, *arguments) return if self.class._event_listeners.nil? || !self.class._event_listeners.has_key?(name.to_sym) self.class._event_listeners[ name.to_sym ].each{ | block | block.call(*arguments) } end
Private Instance Methods
_fire_pubsub_event( name, *arguments )
click to toggle source
# File lib/lanes/concerns/pub_sub.rb, line 123 def _fire_pubsub_event( name, *arguments ) end