Class: Backframe::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/backframe/service.rb,
lib/backframe/service/result/base.rb,
lib/backframe/service/result/failure.rb,
lib/backframe/service/result/success.rb

Defined Under Namespace

Modules: Result

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(*args) ⇒ Object



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

def build(*args)
  new(*args)
end

.perform(*args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/backframe/service.rb', line 13

def perform(*args)
  service = build(*args)

  message = nil
  errors = {}
  result = nil

  ActiveRecord::Base.transaction do
    begin
      result = service.perform
    rescue StandardError => e
      message = e.message
      # errors = e.errors
      service.before_rollback
      raise ActiveRecord::Rollback
    end
    service.before_commit
  end

  if message.nil?
    service.after_commit
  else
    service.after_rollback
  end

  return (message.present?) ? Result::Failure.new(message: message, errors: errors) : Result::Success.new(result)
end

Instance Method Details

#after_commitObject



55
56
# File 'lib/backframe/service.rb', line 55

def after_commit
end

#after_rollbackObject



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

def after_rollback
end

#before_commitObject



52
53
# File 'lib/backframe/service.rb', line 52

def before_commit
end

#before_rollbackObject



46
47
# File 'lib/backframe/service.rb', line 46

def before_rollback
end

#performObject



43
44
# File 'lib/backframe/service.rb', line 43

def perform
end