Class: ServiceObjects::Listener

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/service_objects/listener.rb

Overview

The base class for service listeners

Examples:

Calls #otherwise in case no method has been checked

class MyListener < ServiceObjects::Listener
  def on_success
    "success"
  end

  def otherwise
    "nothing"
  end
end

listener = MyListener.new
listener.finalize
# => "nothing"

Calls #otherwise in case an undefined method has been checked


listener = MyListener.new
listener.respond_to? :on_error
# => false

listener.finalize
# => "nothing"

Skips #otherwise in case a defined method has been checked


listener = MyListener.new
listener.respond_to? :on_success
# => true

listener.finalize
# => nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(object) ⇒ ServiceObjects::Listener

Listener object constructor

Decorates given object by adding methods to be called by service objects

Examples:

Decorates an object

object = Object.new

listener = ServiceObjects::Listener.new object
listener.send :__getobj__
# => object

# File 'lib/service_objects/listener.rb', line 14


Instance Method Details

#finalizeself

Calls #otherwise in case no existing method has been checked

Examples:

Calls #otherwise in case no method has been checked

class MyListener < ServiceObjects::Listener
  def on_success
    "success"
  end

  def otherwise
    "nothing"
  end
end

listener = MyListener.new
listener.finalize
# => "nothing"

Calls #otherwise in case an undefined method has been checked


listener = MyListener.new
listener.respond_to? :on_error
# => false

listener.finalize
# => "nothing"

Skips #otherwise in case a defined method has been checked


listener = MyListener.new
listener.respond_to? :on_success
# => true

listener.finalize
# => nil

77
78
79
80
81
# File 'lib/service_objects/listener.rb', line 77

def finalize
  otherwise unless @notified

  self
end

#otherwiseundefined

This method is abstract.

The method called by #finalize when no other method has been checked


38
39
# File 'lib/service_objects/listener.rb', line 38

def otherwise
end

#respond_to?Boolean

Checks whether the method is defined

Remembers the fact that any defined method has been checked

Examples:

respond_to? :finalize, false # => true

91
92
93
# File 'lib/service_objects/listener.rb', line 91

def respond_to?(*)
  super ? (@notified = true) : false
end