Class: Marko::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/marko/gadgets/service.rb

Overview

Service like ServiceObject, Command, etc.

Examples:

just call without parameters

class BoldService < Service
  def call
    42
  end
end

with parameters

class PlusService < Service
  def initialize(a, b)
    @a, @b = a, b
  end

  def call
    42
  end
end

Constant Summary collapse

Failure =
Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, **kwargs, &block) ⇒ Service

Returns a new instance of Service.



40
41
42
43
44
# File 'lib/marko/gadgets/service.rb', line 40

def initialize(*args, **kwargs, &block)
  @args = args
  @kwargs = kwargs
  @block = block
end

Class Method Details

.call(*args, **kwargs, &block) ⇒ Object



28
29
30
# File 'lib/marko/gadgets/service.rb', line 28

def call(*args, **kwargs, &block)
  new(*args, **kwargs, &block).call
end

.inherited(klass) ⇒ Object



32
33
34
35
# File 'lib/marko/gadgets/service.rb', line 32

def inherited(klass)
  klass.const_set(:Failure, Class.new(klass::Failure))
  super
end

Instance Method Details

#callObject



46
47
48
# File 'lib/marko/gadgets/service.rb', line 46

def call
  fail Failure, "#{self.class.name}#call must be overrided"
end