Last active
November 24, 2018 09:59
-
-
Save maicher/b132296766ddb97cb3219674830f8b63 to your computer and use it in GitHub Desktop.
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
class CreateUser | |
class Error < StandardError; end | |
# ... | |
end | |
# contoller | |
begin | |
user = CreateUser.new(params).call | |
p "User #{user.username} created succesfully" | |
rescue CreateUser::Error => e | |
p "Didn't work because #{e.to_s}" | |
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
require 'dry/monads/result' | |
require 'dry/matcher/result_matcher' | |
class CreateUser | |
include Dry::Monads::Result::Mixin | |
include Dry::Matcher.for(:call, with: Dry::Matcher::ResultMatcher) | |
def call(params) | |
# Failure is a method for constructing the Failure object | |
return Failure(message: 'smth') if smth_is_invalid? | |
Success(user: user) | |
end | |
end | |
create_user = CreateUser.new(other_service: OtherServce.new) | |
create_user.call(params) do |result| | |
result.success do |user:| | |
p "User #{user.username} created" | |
end | |
result.failure do |error_message:| | |
p "Operation failed because of #{error_message}" | |
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
# contoller | |
begin | |
user = CreateUser.new(params).call | |
p "User #{user.username} created succesfully" | |
rescue ActiveRecord::RecordInvalid => e | |
p "Didn't work because #{e.to_s}" | |
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
# controller | |
if CreateUser.new(params).call | |
p 'Operation succeeded | |
else | |
p 'Operation failed | |
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
# controller | |
create_user = CreateUser.new(params) | |
if create_user.call | |
user = create_user.user | |
p "User #{user.username} created" | |
else | |
p "Operation failed because of #{create_user.error_message}" | |
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
class CreateUser | |
def self.call(params) | |
new(params).call | |
end | |
def initialize(params) | |
... | |
end | |
def call | |
.. | |
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
# controller | |
result = CreateUser.call(params) | |
if result.success? | |
user = result.user | |
... |
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
# controller | |
result = CreateUser.run(params) | |
if result.success? | |
user = result.user | |
... |
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
# controller | |
result = CreateUser.run(params.merge(other_service: OtherServce.new)) | |
if result.success? | |
... |
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
# create_user.rb | |
class CreateUser | |
# Define result objects. | |
class Success < Hash; end | |
class Failure < Hash; end | |
def call(params) | |
return Failure.new(message: 'smth') if smth_is_invalid? | |
.... | |
Success.new(user: user) | |
end | |
end | |
# In controller, initialize the service using DI if needed. | |
create_user = CreateUser.new(other_service: OtherServce.new) | |
result = create_user.call(params) | |
if result.is_a? CreateUser::Success | |
user = create_user[:user] | |
p "User #{user.username} created" | |
else | |
p "Operation failed because of #{result[:error_message]}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment