Class: ServiceObjects::Base
- Inherits:
-
Object
- Object
- ServiceObjects::Base
- Includes:
- Attestor::Validations, AttrCoerced, Virtus::Model::Core, Wisper::Publisher
- Defined in:
- lib/service_objects/base.rb
Overview
Base class for service objects
Class Method Summary collapse
-
.dependency(name, default: nil) ⇒ undefined
Declares the dependency from another class.
Instance Method Summary collapse
-
#message(type, key, options) ⇒ Object
Creates the message of given type.
-
#publish(notification, *args) ⇒ undefined
Notifies subscribers and then throws :published.
-
#run ⇒ undefined
Runs the service by calling #run! and catching its :published throws.
-
#run_service(service, listener) ⇒ undefined
Runs the service and receives its notifications to listener.
-
#subscribe(listener, prefix: nil) ⇒ undefined
Subscribes a listener for the service object’s notifications.
-
#validate(context) ⇒ undefined
Validates the object in given context.
Class Method Details
.dependency(name, default: nil) ⇒ undefined
Declares the dependency from another class
82 83 84 85 86 |
# File 'lib/service_objects/base.rb', line 82 def self.dependency(name, default: nil) attr_accessor(name) return unless default define_method(name) { instance_eval "@#{ name } ||= #{ default }" } end |
Instance Method Details
#message(type, key, options) ⇒ ServiceObjects::Message #message(type, key) ⇒ ServiceObjects::Message
Creates the message of given type
173 174 175 |
# File 'lib/service_objects/base.rb', line 173 def (type, *args) Message.new type, translate(type, *args) end |
#publish(notification, *args) ⇒ undefined
Notifies subscribers and then throws :published
188 189 190 191 |
# File 'lib/service_objects/base.rb', line 188 def publish(*) super throw :published end |
#run ⇒ undefined
Runs the service by calling #run! and catching its :published throws
103 104 105 |
# File 'lib/service_objects/base.rb', line 103 def run catch(:published) { run! } end |
#run_service(service, listener) ⇒ undefined
Runs the service and receives its notifications to listener
126 127 128 129 130 |
# File 'lib/service_objects/base.rb', line 126 def run_service(service, listener) service.subscribe listener, prefix: :on service.run listener.finalize end |
#subscribe(listener, prefix: nil) ⇒ undefined
Subscribes a listener for the service object’s notifications
|
# File 'lib/service_objects/base.rb', line 88
|
#validate(context) ⇒ undefined
Validates the object in given context
Publishes error if validation fails
114 115 116 117 118 |
# File 'lib/service_objects/base.rb', line 114 def validate(context) validate!(context) rescue Attestor::InvalidError => error publish :error, error..map { |text| Message.new :error, text } end |