Class: Upframework::BaseService

Inherits:
Object
  • Object
show all
Defined in:
app/services/upframework/base_service.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attributes) ⇒ BaseService

Returns a new instance of BaseService.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/services/upframework/base_service.rb', line 12

def initialize(**attributes)
  @errors = []

  self.class.send(:attr_reader, *attributes.keys)

  #TODO: Remove. This should be handled on the child class.
  attributes.each do |key, value|
    instance_variable_set("@#{key}", value)
  end

  post_initialize(**attributes)
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



3
4
5
# File 'app/services/upframework/base_service.rb', line 3

def errors
  @errors
end

Class Method Details

.run(attributes = {}) ⇒ Object



6
7
8
9
# File 'app/services/upframework/base_service.rb', line 6

def run(attributes = {})
  attributes = Hash[attributes.to_h.map { |k, v| [k.to_sym, v] }]
  new(**attributes).tap(&:execute)
end

Instance Method Details

#error?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'app/services/upframework/base_service.rb', line 39

def error?
  @errors.present?
end

#error_messagesObject



43
44
45
# File 'app/services/upframework/base_service.rb', line 43

def error_messages
  @errors.join('. ')
end

#executeObject Also known as: run



28
29
# File 'app/services/upframework/base_service.rb', line 28

def execute
end

#post_initialize(**attributes) ⇒ Object



25
26
# File 'app/services/upframework/base_service.rb', line 25

def post_initialize(**attributes)
end

#resultObject



32
33
# File 'app/services/upframework/base_service.rb', line 32

def result
end

#success?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'app/services/upframework/base_service.rb', line 35

def success?
  @errors.blank?
end