repository = UsersRepository.new
Registration.call(repository, "[email protected]", "pass")
Last active
September 13, 2021 22:59
-
-
Save leandronsp/4af1f04720704bd6f2fe3c956bfd9aff to your computer and use it in GitHub Desktop.
Callable pattern in Ruby
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
module Callable | |
module ClassMethods | |
def call(*args) | |
new(*args).result | |
end | |
def receive(*arguments) | |
define_method(:initialize) do |*parameters| | |
parameters.zip(arguments).each do |parameter, argument| | |
instance_variable_set("@#{argument}", parameter) | |
end | |
end | |
end | |
end | |
def self.included(base) | |
base.extend(ClassMethods) | |
end | |
def result | |
fail NotImplementedError | |
end | |
end |
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 Registration | |
include Callable | |
receive :repository, :email, :password | |
def result | |
@repository.insert(@email, @password) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment