module Skr::Concerns::PubSub

Event subscription and publishing for Stockor 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_events() click to toggle source
# File lib/skr/concerns/pub_sub.rb, line 85
def fire_after_create_events
    _fire_event(:create, self )
end
fire_after_destroy_events() click to toggle source
# File lib/skr/concerns/pub_sub.rb, line 79
def fire_after_destroy_events
    _fire_event(:update, self )
end
fire_after_save_events() click to toggle source
# File lib/skr/concerns/pub_sub.rb, line 88
def fire_after_save_events
    _fire_event( :save, self )
end
fire_after_update_events() click to toggle source
# File lib/skr/concerns/pub_sub.rb, line 82
def fire_after_update_events
    _fire_event(:update, self )
end
fire_event( name, *arguments ) click to toggle source
# File lib/skr/concerns/pub_sub.rb, line 92
def fire_event( name, *arguments )
    self.class._ensure_validate_event( name )
    arguments.unshift( self )
    _fire_event( name, *arguments )
end

Private Instance Methods

_fire_event( name, *arguments ) click to toggle source
# File lib/skr/concerns/pub_sub.rb, line 99
def _fire_event( name, *arguments )
    self.class.event_listeners[ name.to_sym ].each{ | block | block.call(*arguments) }
    return true
end