Class: Servizio::Service

Inherits:
Object
  • Object
show all
Extended by:
MethodDecorators
Includes:
ActiveModel::Model, ActiveModel::Validations
Defined in:
lib/servizio/service.rb

Defined Under Namespace

Modules: DefineColumnType, MethodDecorators

Constant Summary collapse

OperationFailedError =
Class.new(StandardError)
OperationInvalidError =
Class.new(StandardError)
OperationNotCalledError =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MethodDecorators

inherited

Instance Attribute Details

#resultObject

Returns the value of attribute result.



9
10
11
# File 'lib/servizio/service.rb', line 9

def result
  @result
end

Class Method Details

.call(*args) ⇒ Object

shortcut to call operations, which returns the result or nik



21
22
23
24
25
26
27
# File 'lib/servizio/service.rb', line 21

def self.call(*args)
  operation = self.new(*args)

  if operation.valid? && operation.call!.succeeded?
    operation.result
  end
end

.call!(*args) ⇒ Object

shortcut to call operations, which returns the result and/or throws errors



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/servizio/service.rb', line 30

def self.call!(*args)
  operation = self.new(*args)

  if operation.valid?
    if operation.call!.succeeded?
      operation.result
    else
      raise Servizio::Service::OperationFailedError
    end
  else
    raise Servizio::Service::OperationInvalidError
  end
end

.nameObject



16
17
18
# File 'lib/servizio/service.rb', line 16

def self.name
  super ? super : "__anonymous_servizio_service_class__"
end

Instance Method Details

#called?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/servizio/service.rb', line 48

def called?
  @called == true
end

#failed!(error = :failed) ⇒ Object



52
53
54
55
# File 'lib/servizio/service.rb', line 52

def failed!(error = :failed)
  errors[:call] = error
  self # for chaining
end

#failed?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/servizio/service.rb', line 57

def failed?
  errors.present?
end

#succeeded?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/servizio/service.rb', line 61

def succeeded?
  called? && errors.blank?
end