Created
November 21, 2018 11:36
-
-
Save ErnestK/50a15c958911bf13faba0322fb973af7 to your computer and use it in GitHub Desktop.
Service example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# project/app/controllers/api_controller.rb | |
class ApiController | |
def render_response(serializer:, result:) | |
if result.success? | |
render json: serializer.new(result.value!).serializable_hash | |
else | |
render json: { errors: [result.failure] } | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# project/app/services/create_user_service.rb | |
# All buiseness logic in serice objects | |
class CreateUserService | |
extend Dry::Initializer | |
include Dry::Monads::Result::Mixin | |
option :user_name | |
option :email | |
... | |
# also you can use monads using | |
# https://dry-rb.org/gems/dry-monads/do-notation/ | |
def call | |
write_user.bind do |user_id, email| | |
send_email(email).bind do | |
do_some_logic.bind | |
end | |
end | |
end | |
private | |
# each service return only Success or failure | |
# which why we always render success or error and dont fail after raise error | |
def write_user | |
WriteUserInDbSerice.new(@user_id).call | |
end | |
def send_email | |
SendEmailSerice.new(@user_id).call | |
end | |
def do_some_logic | |
v = do_smthng | |
Succcess(v) | |
rescue StandardError => e | |
Failure("Error in logic: #{v}, exception: #{e}") | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# project/app/services/some_query.rb | |
# In query objects only selects | |
class CreateUserQuery | |
extend Dry::Initializer | |
.... | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# project/app/controllers/users_controllers.rb | |
class UsersController < ApiController | |
# maybe we should call some validators from project/app/validators/* | |
# and in that validators validate params using dry-validate | |
def index | |
params.require(:consumer) | |
params.require(:configuration) | |
params.require(:custom) | |
params.permit! | |
result = CreateUserService.new(user_name: params[:user_name], | |
email: params[:email]).call | |
render_response serializer: ::SomeSerializer, result: result | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment