Class: BBServices::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/bbservices/service.rb

Overview

The lightweight service object provided by BBServices. The basic functionality includes:

  • Ability to access user defined initalization. E.g. initalize(one = 1, two = 2)

  • Ability to chain services via ‘.then’

  • Ability to check success / failure

  • Ability to store errors thrown via services

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(*args, **kwargs, &block) ⇒ BBServices.Service

Creates the service instances and calls run upon said instance

Parameters:

  • args (Array)

    The array of params which has been passed to run

  • block (Block)

    The block which will be called upon the service finishing running

Returns:



21
22
23
24
25
# File 'lib/bbservices/service.rb', line 21

def run(*args, **kwargs, &block)
  new(*args, **kwargs).tap do |service|
    service.run(&block)
  end
end

.run!(*args, **kwargs, &block) ⇒ BBServices.Service

Creates the service instances and calls run! upon said instance

Parameters:

  • args (Array)

    The array of params which has been passed to run!

  • block (Block)

    The block which will be called upon the service finishing running successfully

Returns:



31
32
33
34
35
# File 'lib/bbservices/service.rb', line 31

def run!(*args, **kwargs, &block)
  new(*args, **kwargs).tap do |service|
    service.run!(&block)
  end
end

Instance Method Details

#errorObject



130
131
132
# File 'lib/bbservices/service.rb', line 130

def error
  errors.first
end

#error?Boolean

Returns true / false if the service threw an error.

Returns:

  • (Boolean)

    true/false on if an error has occurred



126
127
128
# File 'lib/bbservices/service.rb', line 126

def error?
  errors.count > 0
end

#errorsObject



134
135
136
# File 'lib/bbservices/service.rb', line 134

def errors
  @errors ||= []
end

#failed?Boolean

Returns true/false on if the service was unsuccessful. This will always be the inverse of successful?

Returns:

  • (Boolean)

    true/false on if the service failed.



98
99
100
# File 'lib/bbservices/service.rb', line 98

def failed?
  (@successful == nil ? false : !@successful) 
end

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

Calls the given block if the service failed

Yields:

  • (_self)

Yield Parameters:



108
109
110
# File 'lib/bbservices/service.rb', line 108

def failure(&block)
  yield(self) if failed?
end

#on(success: proc {}, failure: proc {}) ⇒ Boolean

Calls success on success?, failure on !success?

Parameters:

  • success (Proc) (defaults to: proc {})

    The proc to be called upon a successful service

  • failure (Proc) (defaults to: proc {})

Returns:

  • (Boolean)

    true/false if the service has any params



116
117
118
119
120
121
122
# File 'lib/bbservices/service.rb', line 116

def on(success: proc {}, failure: proc {})
  if successful?
    success.call
  elsif failed?
    failure.call
  end
end

#ran?Boolean

Returns true/false on if the service has been ran

Returns:

  • (Boolean)

    True/False value on if the service has been ran



82
83
84
# File 'lib/bbservices/service.rb', line 82

def ran?
  !!@ran
end

#run(&block) ⇒ BBServices.Service Also known as: call

Runs the service using ‘safe’ execution. The @run variable will be set to true, then the run method will be called

Parameters:

  • block (Block)

    The block which will be called upon the service finishing running

Returns:



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bbservices/service.rb', line 42

def run(&block)
  begin
    @ran = true
    successful = on_run
    set_successful(successful == nil ? true : !!successful)
  rescue => e
    set_successful(false)
    register_error(e)
  ensure
    call_block(block) if block_given?
  end
end

#run!(&block) ⇒ BBServices.Service Also known as: call!

Runs the service using ‘unsafe’ execution. The @run variable will be set to true, then the run! method will be called

Parameters:

  • block (Block)

    The block which will be called upon the service finishing running

Returns:



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/bbservices/service.rb', line 59

def run!(&block)
  begin
    @ran = true
    successful = on_run!
    set_successful(successful == nil ? true : !!successful)
    call_block(block) if block_given?
  rescue => e
    set_successful(false)
    register_error(e)
    raise e
  end
end

#run?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/bbservices/service.rb', line 86

def run?
  !!@ran
end

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

Calls the given block if the service was successful

Yields:

  • (_self)

Yield Parameters:



103
104
105
# File 'lib/bbservices/service.rb', line 103

def success(&block)
  yield(self) if succeeded?
end

#successful?Boolean Also known as: succeeded?

Returns true/false on if the service was successful.

Returns:

  • (Boolean)

    true/false on if the service was successful.



92
93
94
# File 'lib/bbservices/service.rb', line 92

def successful?
  (@successful == nil ? false : @successful) 
end

#then(*args, &block) ⇒ Object

Returns a ServiceChain with the service as a registered service. The service must have been ran in order to call this chaining method.



74
75
76
77
78
# File 'lib/bbservices/service.rb', line 74

def then(*args, &block)
  raise BBServices::ServiceMustRunBeforeChainingError if !self.ran?

  BBServices::ServiceChain.new(self).then(block)
end