Sha256: 21f1b4cdaca25e45b43919b44d1ea1f6bc6ad4c1f685d0b03159f249d798f942

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

module Bushido
  class Data #:nodoc:
    @@observers = []

    def self.add_observer(observer)
      puts "Subscribing #{observer} to Bushido data calls"
      @@observers << observer
    end
    
    def self.fire(data, event)
      puts "Bushido Hooks Firing #{event} with => #{data.inspect}"

      processed = false

      @@observers.each do |observer|
        puts "#{observer}.respond_to?(#{event}) => #{observer.respond_to?(event)}"

        if observer.respond_to?(event)
          processed = true

          # Make a copy of the data so it's not mutated as the events
          # pass through the observers
          observer.instance_variable_set("@params", data.dup)

          result = observer.send(event)

          # Allow an observer to halt event propagation
          if result == :halt
            puts "Observer #{observer} halted event propagation"
            break
          end
        end
      end

      # We've checked all the observers to see if they respond to the
      # named events, so if the event is still unprocessed then let's
      # fall back on the first catch_all event we find
      if !processed
        @@observers.each do |observer|
          if observer.respond_to?(:catch_all)
            observer.instance_variable_set("@params", data.dup)

            observer.send(:catch_all)
            break
          end
        end
      end
    end
  end
end


Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bushido-0.0.36 lib/bushido/data.rb