Class: HC::Service::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Callbacks, ActiveModel::Validations
Defined in:
lib/hc/service/base.rb

Overview

Used for abstraction and clarity of service objects

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Base

Set instance variables based on arg smearing



17
18
19
20
21
# File 'lib/hc/service/base.rb', line 17

def initialize(args)
  args.each do |k, v|
    instance_variable_set("@#{k}", v)
  end
end

Instance Attribute Details

#manual_status_codeObject

Returns the value of attribute manual_status_code.



13
14
15
# File 'lib/hc/service/base.rb', line 13

def manual_status_code
  @manual_status_code
end

#resultObject

Returns the value of attribute result.



13
14
15
# File 'lib/hc/service/base.rb', line 13

def result
  @result
end

Class Method Details

.execute(args) ⇒ Object

Abstracted execution instanciates class, sets variables, runs validations, and handles errors



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/hc/service/base.rb', line 69

def self.execute(args)

  # Build the new service
  #
  service = new(args)

  # Service executions should be wrapped in a transaction, and gracefully handle errors
  #
  ActiveRecord::Base.transaction do

    # Validate service args
    #
    service.try(:before_validation)
    return service unless service.valid?

    # Attempt execution of service
    #
    begin
      service.execute
    rescue ActiveRecord::RecordInvalid => ex
      if service.errors.blank?
        service.errors.add(:validation, ex.record.errors)
        service.manual_status_code = 422
      end
      raise ActiveRecord::Rollback
    rescue ActiveRecord::Rollback => ex
      raise ex
    rescue StandardError => ex
      Rails.logger.debug '================================='
      Rails.logger.debug ex.inspect
      Rails.logger.debug ex.backtrace.join("\n")
      Rails.logger.debug '================================='
      if service.errors.blank?
        service.errors.add(:exception, 'An error occurred')
        service.manual_status_code = 500
      end
      raise ActiveRecord::Rollback
    end
  end
  return service

end

Instance Method Details

#enqueue_job(job_class:, params:, current_user:) ⇒ Object



63
64
65
# File 'lib/hc/service/base.rb', line 63

def enqueue_job(job_class:, params:, current_user:)
  HC::Jobs.enqueue(job_class: job_class, user: current_user, params: params)
end

#errors?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/hc/service/base.rb', line 59

def errors?
  errors.count.positive?
end

#present(object, method: nil, options: {}) ⇒ Object



55
56
57
# File 'lib/hc/service/base.rb', line 55

def present(object, method: nil, options: {})
  HC::Presenter.present(object, method: method, options: options)
end

#raise_error!(field:, message:, code: 0) ⇒ Object

Raises:

  • (ActiveRecord::Rollback)


32
33
34
35
36
# File 'lib/hc/service/base.rb', line 32

def raise_error!(field:, message:, code: 0)
  errors.add(field, message)
  self.manual_status_code = code
  raise ActiveRecord::Rollback
end

#raise_errors!(child_errors) ⇒ Object

Raises:

  • (ActiveRecord::Rollback)


38
39
40
41
42
43
44
45
46
47
# File 'lib/hc/service/base.rb', line 38

def raise_errors!(child_errors)
  Array.wrap(child_errors).each do |error|
    error.messages.each_key do |field|
      error.messages[field].each do |message|
        errors.add(field, message)
      end
    end
  end
  raise ActiveRecord::Rollback
end

#status_codeObject

Abstraction for getting a stable service code



25
26
27
28
29
30
# File 'lib/hc/service/base.rb', line 25

def status_code
  return manual_status_code if manual_status_code
  return 422 if errors.present?
  return 200 if result
  return 0
end

#unauthorized!Object

Raises:

  • (ActiveRecord::Rollback)


49
50
51
52
53
# File 'lib/hc/service/base.rb', line 49

def unauthorized!
  errors.add(:user, 'Unauthorized')
  self.manual_status_code = 401
  raise ActiveRecord::Rollback
end