Class: BBServices::Service

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

Overview

The lightweight service object provided by BBServices.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Service

Initializes the service with a hash of params

Parameters:

  • params (Hash) (defaults to: {})

    The params which are passed to the service



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

def initialize(params = {})
  @object = nil
  @successful = false
  @ran = false
  @error = nil
  @service_class = nil

  @params = params
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



16
17
18
# File 'lib/bbservices/service.rb', line 16

def error
  @error
end

#objectObject (readonly)

Returns the value of attribute object.



16
17
18
# File 'lib/bbservices/service.rb', line 16

def object
  @object
end

#paramsObject

Returns the value of attribute params.



16
17
18
# File 'lib/bbservices/service.rb', line 16

def params
  @params
end

Class Method Details

.callBBServices.Service

Creates the service instances and calls run upon said instance An alias to BBServices::Service‘s run method

Parameters:

  • params (Hash)

    The params which are passed to the service

  • block (Block)

    The block which will be called upon the service finishing running

Returns:



41
42
43
44
45
# File 'lib/bbservices/service.rb', line 41

def run(params = {}, &block)
  new(params).tap do |service|
    service.run(&block)
  end
end

.call!BBServices.Service

Creates the service instances and calls run! upon said instance An alias to BBServices::Service‘s run! method

Parameters:

  • params (Hash)

    The params which are passed to the service

  • block (Block)

    The block which will be called upon the service finishing running

Returns:



44
45
46
47
48
# File 'lib/bbservices/service.rb', line 44

def run!(params = {}, &block)
  new(params).tap do |service|
    service.run!(&block)
  end
end

.internal_service_classClass

Gets the current service class

Returns:

  • (Class)

    returns the service class. Please note this is an internal method.



55
56
57
# File 'lib/bbservices/service.rb', line 55

def internal_service_class
  @service_class
end

.run(params = {}, &block) ⇒ BBServices.Service

Creates the service instances and calls run upon said instance

Parameters:

  • params (Hash) (defaults to: {})

    The params which are passed to the service

  • block (Block)

    The block which will be called upon the service finishing running

Returns:



24
25
26
27
28
# File 'lib/bbservices/service.rb', line 24

def run(params = {}, &block)
  new(params).tap do |service|
    service.run(&block)
  end
end

.run!(params = {}, &block) ⇒ BBServices.Service

Creates the service instances and calls run! upon said instance

Parameters:

  • params (Hash) (defaults to: {})

    The params which are passed to the service

  • block (Block)

    The block which will be called upon the service finishing running

Returns:



34
35
36
37
38
# File 'lib/bbservices/service.rb', line 34

def run!(params = {}, &block)
  new(params).tap do |service|
    service.run!(&block)
  end
end

.service_class(klass) ⇒ BBServices.Service

Sets the service class on the Class. Please note this is an internal method.

Parameters:

  • klass (Class)

    The class which will be set as the service_class

Returns:



49
50
51
# File 'lib/bbservices/service.rb', line 49

def service_class(klass)
  @service_class = klass
end

Instance Method Details

#error?Boolean

Returns true / false if the service threw an error

Returns:

  • (Boolean)

    true/false on if an error has occurred



222
223
224
# File 'lib/bbservices/service.rb', line 222

def error?
  !!@error
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.



194
195
196
# File 'lib/bbservices/service.rb', line 194

def failed?
  !succeeded?
end

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

Calls the given block if the service failed

Yields:

  • (_self)

Yield Parameters:



204
205
206
# File 'lib/bbservices/service.rb', line 204

def failure
  yield(self) if failed?
end

#number_of_paramsNumber

Gets the number of params

Returns:

  • (Number)

    The number of params



161
162
163
# File 'lib/bbservices/service.rb', line 161

def number_of_params
  @params ? @params.length : 0
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



212
213
214
215
216
217
218
# File 'lib/bbservices/service.rb', line 212

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

#param(key) ⇒ Hash

Gets a single param using a key

Parameters:

  • key (String/Symbol)

    The key which is used to find the param

Returns:

  • (Hash)

    The param found using the key



155
156
157
# File 'lib/bbservices/service.rb', line 155

def param(key)
  @params[key] if @params
end

#param_for(key) ⇒ Hash

Gets a single param using a key

Parameters:

  • key (String/Symbol)

    The key which is used to find the param

Returns:

  • (Hash)

    The param found using the key



148
149
150
# File 'lib/bbservices/service.rb', line 148

def param_for(key)
  param(key)
end

#params?Boolean

Returns true / false if the service has any params

Returns:

  • (Boolean)

    true/false if the service has any params



173
174
175
# File 'lib/bbservices/service.rb', line 173

def params?
  !!(@params && @params.length)
end

#ran?Boolean Also known as: run?

Returns true/false on if the service has been ran

Returns:

  • (Boolean)

    True/False value on if the service has been ran



167
168
169
# File 'lib/bbservices/service.rb', line 167

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, initialize_service and run_service will then be called.

Parameters:

  • block (Block)

    The block which will be called upon the service finishing running

Returns:



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/bbservices/service.rb', line 76

def run(&block)
  set_ran
  begin
    initialize_service
    run_service
  rescue => e
    set_successful(false)
    set_error(e)
  ensure
    call_block(&block)
  end
end

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

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

Parameters:

  • block (Block)

    The block which will be called upon the service finishing running

Returns:



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/bbservices/service.rb', line 93

def run!(&block)
  set_ran
  begin
    initialize_service
    run_service!
    call_block(&block)
  rescue => e
    set_successful(false)
    set_error(e)
    raise e
  end
end

#service_classClass

Gets the current service class. This will use @service_class if set, otherwise will fallback to self.class.internal_service_class.

Returns:

  • (Class)

    new_service_class The new service class.



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

def service_class
  @service_class ||= self.class.internal_service_class
end

#service_class=(new_service_class) ⇒ Object

Sets the service_class on the instance. This will override the self.class.internal_service_class.

Parameters:

  • new_service_class (Class)

    The new service class.



120
121
122
# File 'lib/bbservices/service.rb', line 120

def service_class=(new_service_class)
  set_service_class(new_service_class)
end

#set_params(new_params) ⇒ Object

Sets the params variable (@params) on the service.

Parameters:

  • new_params (Hash)

    The new params Hash.

Raises:



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

def set_params(new_params)
  raise BBServices::ServiceHashTypeError unless new_params.is_a?(Hash)

  @params = new_params
end

#set_service_class(new_service_class) ⇒ Object

Sets the service_class on the instance. This will override the self.class.internal_service_class.

Parameters:

  • new_service_class (Class)

    The new service class.



114
115
116
# File 'lib/bbservices/service.rb', line 114

def set_service_class(new_service_class)
  @service_class = new_service_class
end

#succeeded?Boolean

Returns true/false on if the service did succeed.

Returns:

  • (Boolean)

    true/false on if the service did succeed.



182
183
184
# File 'lib/bbservices/service.rb', line 182

def succeeded?
  successful?
end

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

Calls the given block if the service was successful

Yields:

  • (_self)

Yield Parameters:



199
200
201
# File 'lib/bbservices/service.rb', line 199

def success
  yield(self) if succeeded?
end

#successful?Boolean

Returns true/false on if the service was successful.

Returns:

  • (Boolean)

    true/false on if the service was successful.



188
189
190
# File 'lib/bbservices/service.rb', line 188

def successful?
  @successful
end