Skip to content

Instantly share code, notes, and snippets.

@leandronsp
Last active September 13, 2021 22:59
Show Gist options
  • Save leandronsp/4af1f04720704bd6f2fe3c956bfd9aff to your computer and use it in GitHub Desktop.
Save leandronsp/4af1f04720704bd6f2fe3c956bfd9aff to your computer and use it in GitHub Desktop.
Callable pattern in Ruby

Usage

repository = UsersRepository.new

Registration.call(repository, "[email protected]", "pass")
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
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