Created
November 14, 2012 18:01
-
-
Save mbj/4073703 to your computer and use it in GitHub Desktop.
Service object simplifications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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