Skip to content

Instantly share code, notes, and snippets.

@ccyrille
Created January 28, 2022 22:00
Show Gist options
  • Select an option

  • Save ccyrille/a93fd4eda542662e2d7192616b3ffbc3 to your computer and use it in GitHub Desktop.

Select an option

Save ccyrille/a93fd4eda542662e2d7192616b3ffbc3 to your computer and use it in GitHub Desktop.
# Service module
#
# Example usage :
# ```
# class SayHello
# include Service
#
# attr_accessor :name
#
# def call
# puts "Hello #{name} !"
# end
# end
#
# SayHello.call(name: "toto")
# ```
module Service
def self.included(base)
base.class_eval do
# Proxy to #call instance method
def self.call(*args)
new(*args).call
end
# Service initializer
def initialize(args={})
unless args.is_a?(Hash)
raise ArgumentError, "provided 'args' should be a hash."
end
args.each { |k,v| send("#{k.to_s}=", v) }
end
end
end
end
@bobmaerten
Copy link

Hon! Clever :)
Merci pour le détail du pourquoi.

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