Class: SolidService::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/solidservice/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Base

Returns a new instance of Base.



37
38
39
40
# File 'lib/solidservice/base.rb', line 37

def initialize(params)
  @params = (params || {}).with_indifferent_access
  @state = State.new(:success)
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



35
36
37
# File 'lib/solidservice/base.rb', line 35

def params
  @params
end

#stateObject (readonly)

Returns the value of attribute state.



35
36
37
# File 'lib/solidservice/base.rb', line 35

def state
  @state
end

Class Method Details

.call(params = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/solidservice/base.rb', line 6

def call(params={})
  service = new(params)
  service.call
  service.state

rescue Success
  service.state

rescue Failure
  service.state

rescue => e
  service.fail(error: e)
  service.state
end

.call!(params = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/solidservice/base.rb', line 22

def call!(params={})
  state = call(params)
  return state unless state.fail?
  
  if state.error
    raise state.error
  else
    raise Error.new("Service failed", service_result: state)
  end
end

Instance Method Details

#callObject



42
43
44
# File 'lib/solidservice/base.rb', line 42

def call
  raise "Override please"
end

#fail(params = {}) ⇒ Object



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

def fail(params={})
  @state = State.new(:fail, params)
end

#fail!(params = {}) ⇒ Object

Raises:



55
56
57
58
# File 'lib/solidservice/base.rb', line 55

def fail!(params={})
  @state = State.new(:fail, params)
  raise Failure.new
end

#success(params = {}) ⇒ Object



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

def success(params={})
  @state = State.new(:success, params)
end

#success!(params = {}) ⇒ Object

Private

Raises:



50
51
52
53
# File 'lib/solidservice/base.rb', line 50

def success!(params={})
  @state = State.new(:success, params)
  raise Success.new
end