Class: ServiceObject::Base
- Inherits:
-
Object
- Object
- ServiceObject::Base
- 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
-
#errors ⇒ ServiceObject::Errors
readonly
Errors object of the current service.
Class Method Summary collapse
-
.transaction(&block) ⇒ Object
Shorthand for ActiveRecord::Base.transaction.
Instance Method Summary collapse
-
#error_messages ⇒ Array
Error messages of the service process so far.
-
#initialize(*args) ⇒ Base
constructor
A new instance of Base.
-
#result ⇒ true, false
(also: #executed_successfully?, #ran_successfully?)
Check if the service process is going well or not so far.
-
#run ⇒ true, false
(also: #execute)
This runs your logic without exposing unessential processes to your controllers.
-
#transaction(&block) ⇒ Object
Shorthand for ActiveRecord::Base.transaction.
Constructor Details
Instance Attribute Details
#errors ⇒ ServiceObject::Errors (readonly)
Returns Errors object of the current service.
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_messages ⇒ Array
Error messages of the service process so far
80 81 82 |
# File 'lib/service_object/base.rb', line 80 def @errors. end |
#result ⇒ true, false Also known as: executed_successfully?, ran_successfully?
Check if the service process is going well or not so far
86 87 88 |
# File 'lib/service_object/base.rb', line 86 def result @result && @errors.empty? end |
#run ⇒ true, 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. }
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.,
isbn: @isbn
)
end
end
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 |