Class: ServiceObject::Base

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

Overview

Service object base class which provides interfaces to controllers so that they can access the result of service processing and its errors if any. Uses ServiceObject::Errors as the error container.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Base

Returns a new instance of Base.



11
12
13
14
# File 'lib/service_object/base.rb', line 11

def initialize(*args)
  @result = true
  @errors = Errors.new
end

Instance Attribute Details

#errorsServiceObject::Errors (readonly)

Returns Errors object of the current service.

Returns:



8
9
10
# File 'lib/service_object/base.rb', line 8

def errors
  @errors
end

Class Method Details

.transaction(&block) ⇒ Object

Shorthand for ActiveRecord::Base.transaction



100
101
102
# File 'lib/service_object/base.rb', line 100

def transaction(&block)
  ActiveRecord::Base.transaction(&block)
end

Instance Method Details

#error_messagesArray

Error messages of the service process so far

Returns:

  • (Array)

    Array of error messages



80
81
82
# File 'lib/service_object/base.rb', line 80

def error_messages
  @errors.full_messages
end

#resulttrue, false Also known as: executed_successfully?, ran_successfully?

Check if the service process is going well or not so far

Returns:

  • (true, false)


86
87
88
# File 'lib/service_object/base.rb', line 86

def result
  @result && @errors.empty?
end

#runtrue, false Also known as: execute

This runs your logic without exposing unessential processes to your controllers. Examples: # Controller

def some_action_on_book
  service = CreateMyBookService.new(params[:isbn])
  service.run do |s|
    s.get_info_from_isbn_api
    s.get_availability_with_library
    s.save_my_book
  end
  if service.result
    render json: { result: 'success', message: 'Successfully saved the book data' }
  else
    render json: { result: 'failure', messages: service.error_messages }
  end
end

# Service class CreateMyBookService < ServiceObject::Base

def initialize(isbn)
  super # This is necessary
  @isbn = isbn
  @book_info = nil
  @my_book = MyBook.new # ActiveRecord Model
  @isbn_api = IsbnApi.new(@isbn) # Non-AR Model
  @library_api = LibraryApi.new # Non-AR Model
end

def get_info_from_isbn_api
  @book_info = @isbn_api.get_all_info
end

def get_availability_with_library
  @availability = @library_api.get_availability(@isbn)
rescue Net::HTTPError => e
  # You can re-throw you own error, too.
  raise YourError, 'Failed to get availability from library'
end

def save_my_book
  @my_book.update!(
      available: @availability,
      name: @book_info.title,
      author: @book_info.author,
      isbn: @isbn
 )
end

end

Returns:

  • (true, false)


67
68
69
70
71
72
73
74
75
# File 'lib/service_object/base.rb', line 67

def run
  before_run
  yield self
  after_run
  @result
rescue => e
  process_exception(e)
  @result = false
end

#transaction(&block) ⇒ Object

Shorthand for ActiveRecord::Base.transaction



93
94
95
# File 'lib/service_object/base.rb', line 93

def transaction(&block)
  self.class.transaction(&block)
end