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



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

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.



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

def manual_status_code
  @manual_status_code
end

#resultObject

Returns the value of attribute result.



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

def result
  @result
end

Class Method Details

.execute(args) ⇒ Object

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



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
111
112
113
114
115
# File 'lib/hc/service/base.rb', line 70

def self.execute(args)

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

  # Service executions should be wrapped in a transaction, and gracefully handle errors
  #
  exception = nil
  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
      exception = 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

  raise exception if exception
  return service

end

Instance Method Details

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



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

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)


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

def errors?
  errors.count.positive?
end

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



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

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

#raise_error!(field, message, code = 400) ⇒ Object

Raises:

  • (ActiveRecord::Rollback)


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

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

#raise_errors!(child_errors) ⇒ Object

Raises:

  • (ActiveRecord::Rollback)


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

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



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

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

#unauthorized!Object

Raises:

  • (ActiveRecord::Rollback)


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

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