Skip to content

Instantly share code, notes, and snippets.

@banister
Created September 21, 2014 10:19
Show Gist options
  • Save banister/2c40d5a24c00fb298381 to your computer and use it in GitHub Desktop.
Save banister/2c40d5a24c00fb298381 to your computer and use it in GitHub Desktop.
class ServiceObject
NULL_LAMBDA = ->(*) { }
def self.callbacks(*args)
args.each do |v|
attr_writer *args.map { |v| "#{v}_callback"}
define_method("#{v}_callback") do
instance_variable_get("@#{v}_callback") || NULL_LAMBDA
end
define_method(v) do |&callback|
send("#{v}_callback=", callback)
end
end
end
def self.call(*args, &block)
new(*args).tap do |service|
yield(service) if block_given?
service.process if service.respond_to?(:process)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment