Module: Shout::ClassMethods

Defined in:
lib/shout/observable.rb

Overview

extend me

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#events_notifiedObject

Returns the value of attribute events_notified.



57
58
59
# File 'lib/shout/observable.rb', line 57

def events_notified
  @events_notified
end

#events_notified_misspellingsObject

Returns the value of attribute events_notified_misspellings.



58
59
60
# File 'lib/shout/observable.rb', line 58

def events_notified_misspellings
  @events_notified_misspellings
end

#observer_class_namesObject

Internal Methods



55
56
57
# File 'lib/shout/observable.rb', line 55

def observer_class_names
  @observer_class_names
end

#observer_class_names_checkObject

Returns the value of attribute observer_class_names_check.



56
57
58
# File 'lib/shout/observable.rb', line 56

def observer_class_names_check
  @observer_class_names_check
end

Class Method Details

.extended(mod) ⇒ Object



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

def self.extended(mod)
  mod.observer_class_names = []
  mod.observer_class_names_check = []
  mod.events_notified = []
  mod.events_notified_misspellings = {}
end

Instance Method Details

#check_observer_classesObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/shout/observable.rb', line 65

def check_observer_classes
  return if @checked
  here_not_there = (observer_class_names - observer_class_names_check)
  there_not_here = (observer_class_names_check - observer_class_names)
  unless here_not_there == there_not_here
    missing = (here_not_there + there_not_here)

    meths = "#{self}.shout_observers(#{missing.map(&:inspect)}) and "+
      missing.map{|observer|
        "#{observer}.observes=#{self.name.to_sym.inspect}"
      }.join(' and ')
    
    raise ArgumentError.new("#{self}: Please call both #{meths} to observe #{self}. This is done to ensure determinism in the load order in development, test, and production environments.")
  end
  @checked = true
end

#load_observers(instance) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/shout/observable.rb', line 81

def load_observers(instance)
  check_observer_classes
  self.observer_class_names.map{|name|
    k = Shout.constantize(name.to_s)
    instance.idemp_observer(k,k.new(instance))
  }
end

#shout_events(event_names) ⇒ Object

Implementor Methods



44
45
46
# File 'lib/shout/observable.rb', line 44

def shout_events(event_names)
  self.events_notified += event_names.map(&:to_sym)
end

#shout_misspellings(event_misspellings) ⇒ Object



47
48
49
# File 'lib/shout/observable.rb', line 47

def shout_misspellings(event_misspellings)
  self.events_notified_misspellings.merge!(event_misspellings)
end

#shout_observers(listener_classes) ⇒ Object



50
51
52
# File 'lib/shout/observable.rb', line 50

def shout_observers(listener_classes)
  self.observer_class_names += listener_classes
end