Class: Collective::Utilities::ObserverBase
- Inherits:
-
Object
- Object
- Collective::Utilities::ObserverBase
- Defined in:
- lib/collective/utilities/observer_base.rb
Direct Known Subclasses
LifecycleObserver, AirbrakeObserver, HoptoadObserver, LogObserver
Instance Attribute Summary collapse
-
#subject ⇒ Object
readonly
Returns the value of attribute subject.
Class Method Summary collapse
- .camelize(s) ⇒ Object
-
.resolve(factory_or_observer, *args) ⇒ Object
factory_or_observer can be something which responds to #notify or a block which responds to #call and can return a factory_or_observer or a class which can be instantiated or a string which can be resolved to a class or an array which can be resolved to a class with parameters.
Instance Method Summary collapse
-
#focus(subject) ⇒ Object
It is possible but unlikely that I would want an observer to observe multiple subjects.
- #notify(subject, *details) ⇒ Object
Instance Attribute Details
#subject ⇒ Object (readonly)
Returns the value of attribute subject.
5 6 7 |
# File 'lib/collective/utilities/observer_base.rb', line 5 def subject @subject end |
Class Method Details
.camelize(s) ⇒ Object
55 56 57 |
# File 'lib/collective/utilities/observer_base.rb', line 55 def self.camelize(s) s.to_s.gsub(/(?:^|_|\s)(.)/) { $1.upcase } end |
.resolve(factory_or_observer, *args) ⇒ Object
factory_or_observer can be something which responds to #notify or a block which responds to #call and can return a factory_or_observer or a class which can be instantiated or a string which can be resolved to a class or an array which can be resolved to a class with parameters
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/collective/utilities/observer_base.rb', line 27 def self.resolve( factory_or_observer, *args ) case when factory_or_observer.respond_to?(:notify) factory_or_observer when factory_or_observer.respond_to?(:call) resolve(factory_or_observer.call(*args)) else case factory_or_observer when :airbrake resolve(Collective::Utilities::AirbrakeObserver,*args) when :hoptoad resolve(Collective::Utilities::HoptoadObserver,*args) when :log resolve(Collective::Utilities::LogObserver,*args) when Class factory_or_observer.new(*args) when String resolve(Collective.resolve_class(factory_or_observer.to_s),*args) when Array args = factory_or_observer.dup fobs = args.shift factory = resolve( fobs, *args ) else return factory_or_observer # assume it supports the notifications natively end end end |
Instance Method Details
#focus(subject) ⇒ Object
It is possible but unlikely that I would want an observer to observe multiple subjects.
11 12 13 |
# File 'lib/collective/utilities/observer_base.rb', line 11 def focus( subject ) @subject = subject end |
#notify(subject, *details) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/collective/utilities/observer_base.rb', line 15 def notify( subject, *details ) if self.respond_to?(details.first) then details = details.dup self.send( details.shift, *details ) end end |