Skip to content

Instantly share code, notes, and snippets.

@virtualstaticvoid
Created December 17, 2014 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save virtualstaticvoid/ff1a2ee338c5d1bde434 to your computer and use it in GitHub Desktop.
Save virtualstaticvoid/ff1a2ee338c5d1bde434 to your computer and use it in GitHub Desktop.
Operation[params] vs Operation.(params) vs Operation(params)
# gem code
module Trailblazer
class Operation
def self.inherited(klass)
qualified_name = klass.name
name_parts = qualified_name.split('::')
method_name = name_parts.last
container_name = name_parts[0...-1].join('::')
container = const_get(container_name)
container.instance_eval <<-RUBY, __FILE__, __LINE__ + 1
def self.#{method_name}(*args)
#{qualified_name}.new().process(*args)
end
RUBY
end
end
end
# user code
class SomeModel
class CreateUser < Trailblazer::Operation
def process(params)
# blah...
puts "Processing with #{params.inspect}"
end
end
end
params = {:foo => :bar}
create_user = SomeModel.CreateUser(params)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment