Class: LukitaService::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/lukita_service/base.rb

Overview

Base class

Class Method Summary collapse

Class Method Details

.doneObject

Run services in pipeline

Example:

>> LukitaService::Base.pipe(SimpleSumService, {a: 1, b: 2}).pipe(SimpleSumService, {a: 5, b: 1}).done
=> #<LukitaService::Result:0x00000002af4950 @valid=true, @result={:sum=>9}, @error=nil, @exception=nil>


68
69
70
71
72
# File 'lib/lukita_service/base.rb', line 68

def self.done
  final_outcome = @outcome
  @outcome = nil
  final_outcome
end

.executeObject



4
# File 'lib/lukita_service/base.rb', line 4

def self.execute; end

.execute!Object



5
# File 'lib/lukita_service/base.rb', line 5

def self.execute!; end

.pipe(service, params = {}) ⇒ Object

Run services in pipeline

Example:

>> LukitaService::Base.pipe(SimpleSumService, {a: 1, b: 2}).pipe(LogService).done
=> #<LukitaService::Result:0x00000002af4950 @valid=true, @result={:sum=>3}, @error=nil, @exception=nil>


49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/lukita_service/base.rb', line 49

def self.pipe(service, params = {})
  if @outcome != nil && @outcome.valid?
    prev_result = @outcome.result

    @outcome = service.run(params.merge(prev_result))
    @outcome.result ||= {}
    @outcome.result = prev_result.merge(@outcome.result)
  elsif @outcome == nil
    @outcome = service.run(params)
  end

  self
end

.run(params = {}) ⇒ Object

Run service with params

Example:

>> SumServiceExample.run(num1: 1, num2: 2)
=> #<LukitaService::Result:0x0000000368b110 @valid?=true, @result={:sum=>3}, @error=nil, @exception=nil>


12
13
14
15
16
17
18
19
20
21
# File 'lib/lukita_service/base.rb', line 12

def self.run(params = {})
  outcome = self.execute(params)
  outcome = self.fill_outcome(outcome)

  if outcome[:valid?]
    Result.new(valid?: outcome[:valid?], result: outcome[:result], error: nil)
  else
    Result.new(valid?: outcome[:valid?], result: outcome[:result], error: outcome[:error])
  end  
end

.run!(params = {}) ⇒ Object

Run service with params, rescue an exception if there is exception

Example:

>> SumServiceExample.run(num1: 1, num2: 2)
=> #<LukitaService::Result:0x0000000368b110 @valid?=true, @result={:sum=>3}, @error=nil, @exception=nil>


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lukita_service/base.rb', line 28

def self.run!(params = {})
  ActiveRecord::Base.transaction do
    outcome = self.execute(params)
  end
  outcome = self.fill_outcome(outcome)

  if outcome[:valid?]
    Result.new(valid?: outcome[:valid?], result: outcome[:result], error: nil, exception: nil)
  else
    Result.new(valid?: outcome[:valid?], result: outcome[:result], error: outcome[:error], exception: outcome[:exception])
  end

  rescue Exception => e 
    Result.new(valid?: outcome[:valid?], result: outcome[:result], error: e.message, exception: e)
end