Skip to content

Instantly share code, notes, and snippets.

@mbj
Created November 14, 2012 18:01
Show Gist options
  • Save mbj/4073703 to your computer and use it in GitHub Desktop.
Save mbj/4073703 to your computer and use it in GitHub Desktop.
Service object simplifications
class MethodObject < Module
def initialize(output_name)
@output_name = output_name
end
module InstanceMethods
attr_reader :input
def initialize(input)
@input = input
end
end
def included(descendant)
output_name = @output_name
descendant.send(:include, InstanceMethods)
descendant.define_singleton_method(:run) do |input|
new(input).public_send(output_name)
end
end
end
class Processor
include MethodObject.new(:result)
def first
split.first
end
def last
split.last
end
def split
input.split('.')
end
def result
[last, first, last].join(".")
end
end
p Processor.run("foo.bar") # => "bar.foo.bar"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment