class BBServices::Service

The lightweight service object provided by BBServices.

Attributes

error[R]
object[R]
params[R]

Public Class Methods

call(params = {}, &block)

An alias to {BBServices::Service}'s run method

Alias for: run
call!(params = {}, &block)

An alias to {BBServices::Service}'s run! method

Alias for: run!
internal_service_class() click to toggle source

Gets the current service class @return [Class] returns the service class. Please note this is an internal method.

# File lib/bbservices/service.rb, line 55
def internal_service_class
  @service_class
end
new(params = {}) click to toggle source

Initializes the service with a hash of params @param [Hash] params The params which are passed to the service

# File lib/bbservices/service.rb, line 62
def initialize(params = {})
  @object = nil
  @successful = false
  @ran = false
  @error = nil
  @service_class = nil

  @params = params
end
run(params = {}, &block) click to toggle source

Creates the service instances and calls run upon said instance @param [Hash] params The params which are passed to the service @param [Block] block The block which will be called upon the service finishing running @return [BBServices.Service] returns the service instance

# File lib/bbservices/service.rb, line 24
def run(params = {}, &block)
  new(params).tap do |service|
    service.run(&block)
  end
end
Also aliased as: call
run!(params = {}, &block) click to toggle source

Creates the service instances and calls run! upon said instance @param [Hash] params The params which are passed to the service @param [Block] block The block which will be called upon the service finishing running @return [BBServices.Service] returns the service instance

# File lib/bbservices/service.rb, line 34
def run!(params = {}, &block)
  new(params).tap do |service|
    service.run!(&block)
  end
end
Also aliased as: call!
service_class(klass) click to toggle source

Sets the service class on the Class. Please note this is an internal method. @param [Class] klass The class which will be set as the service_class @return [BBServices.Service] returns the service instance

# File lib/bbservices/service.rb, line 49
def service_class(klass)
  @service_class = klass
end

Public Instance Methods

call(&block)

An alias to {BBServices::Service}'s run method

Alias for: run
call!(&block)

An alias to {BBServices::Service}'s run! method

Alias for: run!
error?() click to toggle source

Returns true / false if the service threw an error @return [Boolean] true/false on if an error has occurred

# File lib/bbservices/service.rb, line 222
def error?
  !!@error
end
failed?() click to toggle source

Returns true/false on if the service was unsuccessful. This will always be the inverse of successful? @return [Boolean] true/false on if the service failed.

# File lib/bbservices/service.rb, line 194
def failed?
  !succeeded?
end
failure() { |self| ... } click to toggle source

Calls the given block if the service failed

# File lib/bbservices/service.rb, line 204
def failure
  yield(self) if failed?
end
number_of_params() click to toggle source

Gets the number of params @return [Number] The number of params

# File lib/bbservices/service.rb, line 161
def number_of_params
  @params ? @params.length : 0
end
on(success: proc {}, failure: proc {}) click to toggle source

Calls success on success?, failure on !success? @param [Proc] success The proc to be called upon a successful service @param [Proc] failure @return [Boolean] true/false if the service has any params

# File lib/bbservices/service.rb, line 212
def on(success: proc {}, failure: proc {})
  if successful?
    success.call
  else
    failure.call
  end
end
param(key) click to toggle source

Gets a single param using a key @param [String/Symbol] key The key which is used to find the param @return [Hash] The param found using the key

# File lib/bbservices/service.rb, line 155
def param(key)
  @params[key] if @params
end
param_for(key) click to toggle source

Gets a single param using a key @param [String/Symbol] key The key which is used to find the param @return [Hash] The param found using the key

# File lib/bbservices/service.rb, line 148
def param_for(key)
  param(key)
end
params=(new_params) click to toggle source

Sets the params variable (@params) on the service. @param [Hash] new_params The new params Hash.

# File lib/bbservices/service.rb, line 141
def params=(new_params)
  set_params(new_params)
end
params?() click to toggle source

Returns true / false if the service has any params @return [Boolean] true/false if the service has any params

# File lib/bbservices/service.rb, line 173
def params?
  !!(@params && @params.length)
end
ran?() click to toggle source

Returns true/false on if the service has been ran @return [Boolean] True/False value on if the service has been ran

# File lib/bbservices/service.rb, line 167
def ran?
  @ran
end
Also aliased as: run?
run(&block) click to toggle source

Runs the service using 'safe' execution. The @run variable will be set to true, initialize_service and run_service will then be called. @param [Block] block The block which will be called upon the service finishing running @return [BBServices.Service] returns the service instance

# 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
Also aliased as: call
run!(&block) click to toggle source

Runs the service using 'unsafe' execution. The @run variable will be set to true, initialize_service and run_service will then be called. @param [Block] block The block which will be called upon the service finishing running @return [BBServices.Service] returns the service instance

# 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
Also aliased as: call!
run?()

An alias to {BBServices::Service}'s ran? method

Alias for: ran?
service_class() click to toggle source

Gets the current service class. This will use @service_class if set, otherwise will fallback to self.class.internal_service_class. @return [Class] new_service_class The new service class.

# File lib/bbservices/service.rb, line 127
def service_class
  @service_class ||= self.class.internal_service_class
end
service_class=(new_service_class) click to toggle source

Sets the service_class on the instance. This will override the self.class.internal_service_class. @param [Class] new_service_class The new service class.

# File lib/bbservices/service.rb, line 120
def service_class=(new_service_class)
  set_service_class(new_service_class)
end
set_params(new_params) click to toggle source

Sets the params variable (@params) on the service. @param [Hash] new_params The new params Hash.

# 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) click to toggle source

Sets the service_class on the instance. This will override the self.class.internal_service_class. @param [Class] new_service_class The new service class.

# File lib/bbservices/service.rb, line 114
def set_service_class(new_service_class)
  @service_class = new_service_class
end
succeeded?() click to toggle source

Returns true/false on if the service did succeed. @return [Boolean] true/false on if the service did succeed.

# File lib/bbservices/service.rb, line 182
def succeeded?
  successful?
end
success() { |self| ... } click to toggle source

Calls the given block if the service was successful

# File lib/bbservices/service.rb, line 199
def success
  yield(self) if succeeded?
end
successful?() click to toggle source

Returns true/false on if the service was successful. @return [Boolean] true/false on if the service was successful.

# File lib/bbservices/service.rb, line 188
def successful?
  @successful
end

Protected Instance Methods

initialize_service() click to toggle source

Called upon run / run!, should be overridden in order to setup any variable initalization

# File lib/bbservices/service.rb, line 230
def initialize_service() end
run_service() click to toggle source

Called upon run, should be overridden in order to setup any variable initalization

# File lib/bbservices/service.rb, line 234
def run_service
  set_successful
  set_object(nil)
end
run_service!() click to toggle source

Called upon run, should be overridden in order to setup any variable initalization

# File lib/bbservices/service.rb, line 241
def run_service!
  set_successful
  set_object(nil)
end
set_object(obj) click to toggle source

Sets the @object instance variable, the object will be accessible via the .object property outside of the service @param obj The object which will be assigned to the @object instance variable

# File lib/bbservices/service.rb, line 248
def set_object(obj)
  @object = obj
end

Private Instance Methods

call_block() { |self| ... } click to toggle source

Calls the block which has been passed

# File lib/bbservices/service.rb, line 273
def call_block
  yield(self) if block_given?
end
set_error(error) click to toggle source

Sets the internal @error instance variable @param [Error] error The error to be assigned

# File lib/bbservices/service.rb, line 268
def set_error(error)
  @error = error
end
set_ran(ran = true) click to toggle source

Sets the internal @ran instance variable @param [Boolean] ran True / False if the service has been ran

# File lib/bbservices/service.rb, line 256
def set_ran(ran = true)
  @ran = ran
end
set_successful(successful = true) click to toggle source

Sets the internal @successful instance variable @param [Boolean] successful True / False if the service has been successful

# File lib/bbservices/service.rb, line 262
def set_successful(successful = true)
  @successful = successful
end