Class: MiniProgram::ServiceResult

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_program/service_result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(success: false, error: nil, message: nil, message_type: nil, message_kind: nil, data: {}) ⇒ ServiceResult

Returns a new instance of ServiceResult.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mini_program/service_result.rb', line 12

def initialize(success: false,
               error: nil,
               message: nil,
               message_type: nil,
               message_kind: nil,
               data: {})

  self.success = success
  self.data = (data.presence || {}).with_indifferent_access
  self.error = error
  self.message = message
  self.message_type = message_type
  self.message_kind = message_kind
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



3
4
5
# File 'lib/mini_program/service_result.rb', line 3

def data
  @data
end

#errorObject

Returns the value of attribute error.



3
4
5
# File 'lib/mini_program/service_result.rb', line 3

def error
  @error
end

#messageObject

Returns the value of attribute message.



3
4
5
# File 'lib/mini_program/service_result.rb', line 3

def message
  @message
end

#message_kindObject

Returns the value of attribute message_kind.



3
4
5
# File 'lib/mini_program/service_result.rb', line 3

def message_kind
  @message_kind
end

#message_typeObject

Returns the value of attribute message_type.



3
4
5
# File 'lib/mini_program/service_result.rb', line 3

def message_type
  @message_type
end

#successObject Also known as: success?

Returns the value of attribute success.



3
4
5
# File 'lib/mini_program/service_result.rb', line 3

def success
  @success
end

Instance Method Details

#as_json(options = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/mini_program/service_result.rb', line 53

def as_json(options = nil)
  {
    success: ,
    data: ,
    message:,
    message_type: get_message_type,
    message_kind:,
    error:,
  }
end

#failure?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/mini_program/service_result.rb', line 29

def failure?
  !success?
end

#get_message_typeObject



43
44
45
46
47
48
49
50
51
# File 'lib/mini_program/service_result.rb', line 43

def get_message_type
  if message_type.present?
    message_type.to_sym
  elsif success?
    :notice
  else
    :error
  end
end

#on_failure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



38
39
40
41
# File 'lib/mini_program/service_result.rb', line 38

def on_failure
  yield(self) if failure?
  self
end

#on_success {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



33
34
35
36
# File 'lib/mini_program/service_result.rb', line 33

def on_success
  yield(self) if success?
  self
end