Skip to content

Instantly share code, notes, and snippets.

@nfilzi
Created February 16, 2021 11:53
Show Gist options
  • Save nfilzi/c97b56829f305bce22b51f47c3d0744b to your computer and use it in GitHub Desktop.
Save nfilzi/c97b56829f305bce22b51f47c3d0744b to your computer and use it in GitHub Desktop.
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: 'pg@gmail.com',
password: 'secret'
}
CreateUserService.new(user_attributes).call
# OUTPUT in terminal
# Creating user with following attributes..
# {:email=>"pg@gmail.com", :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=>"pg@gmail.com", :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)
@nfilzi
Copy link
Author

nfilzi commented Feb 16, 2021

This is what you get in terminal running the whole file at once

Screenshot 2021-02-16 at 12 54 04

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment