Module: Shout::Listener::ClassMethods

Defined in:
lib/shout/listener.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#callbacksObject

Returns the value of attribute callbacks.



43
44
45
# File 'lib/shout/listener.rb', line 43

def callbacks
  @callbacks
end

#observesObject

Internal Methods



42
43
44
# File 'lib/shout/listener.rb', line 42

def observes
  @observes
end

Instance Method Details

#callbacks_for_event(n) ⇒ Object



65
66
67
# File 'lib/shout/listener.rb', line 65

def callbacks_for_event(n)
  self.callbacks.select{|name,method| name == n}.map{|name,method| method}
end

#observes_classObject



44
45
46
47
48
# File 'lib/shout/listener.rb', line 44

def observes_class
  Shout.constantize(self.observes.to_s)
rescue NameError => e
  nil
end

#shout_callback(name, method) ⇒ Object

Registers a callback which will be executed when the observes_class’s instances call run_callbacks(name).



32
33
34
35
# File 'lib/shout/listener.rb', line 32

def shout_callback(name, method)
  valid_event?(name)
  callbacks.push([name,method])
end

#test_event(name, instance, *params) ⇒ Object



36
37
38
39
# File 'lib/shout/listener.rb', line 36

def test_event(name, instance, *params)
  listener = new(instance)
  listener.update_with_shout_event(name, *params)
end

#valid_event?(event) ⇒ Boolean



57
58
59
60
61
62
63
64
# File 'lib/shout/listener.rb', line 57

def valid_event?(event)
  if observes_class.events_notified_misspellings.has_key?(event)
    raise ArgumentError.new("#{self.class}: Well well well, you've found an area of cognitive dissonance! Use #{observes_class.events_notified_misspellings[event].inspect} instead of #{event}.")
  end
  unless observes_class.events_notified.include?(event)
    raise ArgumentError.new("#{self.class}: Oh lordy, this is embarassing. #{event.inspect} is totally not available in #{observes_class}.\nAvailable events:\n#{observes_class.events_notified.inspect}")
  end
end

#valid_observable?Boolean



49
50
51
52
53
54
55
56
# File 'lib/shout/listener.rb', line 49

def valid_observable?
  unless observes_class
    raise ArgumentError.new("#{self}.observes = #{observes.inspect}. #{observes.inspect} can't be found as a constant.")
  end
  unless observes_class < ::Shout::Observable
    raise ArgumentError.new("#{self}.observes == #{self.observes} does not inherit from ::Shout::Observable.")
  end
end