Created
February 16, 2021 11:53
-
-
Save nfilzi/c97b56829f305bce22b51f47c3d0744b to your computer and use it in GitHub Desktop.
This file contains 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 CreateUserService | |
def initialize(user_attributes) | |
@user_attributes = user_attributes | |
end | |
def call | |
puts "Creating user with following attributes.." | |
p @user_attributes | |
end | |
end | |
user_attributes = { | |
email: '[email protected]', | |
password: 'secret' | |
} | |
CreateUserService.new(user_attributes).call | |
# OUTPUT in terminal | |
# Creating user with following attributes.. | |
# {:email=>"[email protected]", :password=>"secret"} | |
adhoc_proc = ->(u_attributes) do | |
puts "Creating user with following attributes.." | |
p u_attributes | |
end | |
# p adhoc_proc.class # => Proc | |
adhoc_proc.call(user_attributes) | |
# OUTPUT in terminal, exact same! | |
# Creating user with following attributes.. | |
# {:email=>"[email protected]", :password=>"secret"} | |
# Another way of building your service would give you | |
# the exact same signature as a proc/lambda | |
class CreateUserServiceWithAttributesGivenToCallMethod | |
def call(user_attributes) | |
puts "Creating user with following attributes.." | |
p user_attributes | |
end | |
end | |
CreateUserServiceWithAttributesGivenToCallMethod.new.call(user_attributes) | |
# Yet another way of building your service, still respecting the #call method proc interface | |
class CreateUserServiceWithAttributesGivenToCallMethod | |
def self.call(user_attributes) | |
puts "Creating user with following attributes.." | |
p user_attributes | |
end | |
end | |
CreateUserServiceWithAttributesGivenToCallMethod.call(user_attributes) | |
# Going full circle to Pierre Gabriel examples, yet another way | |
# of *calling* your service 😁 | |
class CreateUserServiceWithAttributesGivenToCallMethod | |
def self.call(user_attributes) | |
puts "Creating user with following attributes.." | |
p user_attributes | |
end | |
end | |
CreateUserServiceWithAttributesGivenToCallMethod.(user_attributes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is what you get in terminal running the whole file at once