Module: ServiceObjects::Helpers::Dependable

Included in:
Base
Defined in:
lib/service_objects/helpers/dependable.rb

Overview

Note:

A target class should be *extended* by the module

Features for service dependencies declaration

Instance Method Summary collapse

Instance Method Details

#depends_on(name, default: NULL) ⇒ :depends_on

Declares the dependency setter and getter

Examples:

class AddFoo
  extend ServiceObjects::Helpers::Dependable

  depends_on :get_item, default: GetItem
end

service = AddFoo.new
service.get_item
# => GetItem

# Depencency injection
service.get_item = FindItem
service.get_item
# => FindItem

# Resetting to default
service.get_item = nil
service.get_item
# => GetItem

Set to NULL Object by default

class AddFoo
  extend ServiceObjects::Helpers::Dependable

  depends_on :get_item
end

service = AddFoo.new
service.get_item
# => <ServiceObjects::NULL>

Parameters:

  • name (#to_sym)

    the name for the dependency

  • default (BaseObject)

    (ServiceObjects::NULL) default implementation for the dependency

Returns:

  • (:depends_on)

    the name of the method


54
55
56
57
# File 'lib/service_objects/helpers/dependable.rb', line 54

def depends_on(name, default: NULL)
  attr_writer name
  define_method(name) { instance_eval("@#{ name }") || default }
end