Class: Bushido::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/bushido/data.rb

Overview

:nodoc:

Constant Summary collapse

@@observers =
[]

Class Method Summary collapse

Class Method Details

.add_observer(observer) ⇒ Object



5
6
7
8
# File 'lib/bushido/data.rb', line 5

def self.add_observer(observer)
  puts "Subscribing #{observer} to Bushido data calls"
  @@observers << observer
end

.fire(data, event) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/bushido/data.rb', line 10

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