Skip to content

Instantly share code, notes, and snippets.

@andrelip
Last active June 24, 2016 22:28
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 andrelip/7eb985ea327a04bdf171 to your computer and use it in GitHub Desktop.
Save andrelip/7eb985ea327a04bdf171 to your computer and use it in GitHub Desktop.
# just use this pipe method at Kernel or whever you want it.
def pipe(*args, &block)
scope_object = args[0]
start_object = args[1]
pipe = Pipeline::Pipeline.new scope_object, &block
pipe.start(start_object)
pipe.run_list
end
module Pipeline
class Pipeline < ::BasicObject
attr_reader :last_value, :caller_object
def initialize(caller_object, &computation)
@computation = computation
@caller_object = caller_object
@last_value = nil
end
def __result__
@reference
end
def start(value)
@last_value = value
end
def inspect
last_value.present? ? last_value.inspect : @reference.inspect
end
def respond_to? (message)
message = message.to_sym
[:__result__, :__inspect__].include?(message) or __result__.respond_to? message
end
def raise(*args)
__result__.__send__(:raise, *args, &b)
end
def to_s(*args)
last_value.to_s(*args)
end
def run_list
self.instance_eval(&@computation)
end
def method_missing(*args, &b)
if last_value.nil?
@last_value = __result__.__send__(*args, &b)
else
new_args = args.insert(1, last_value)
@last_value = caller_object.__send__(*new_args, &b)
@reference = last_value
end
last_value
end
def external(*args, &block)
proc = args.shift
@last_value = proc.call(last_value)
@reference = last_value
end
private
def check_proc(value, *args, &b)
args.shift
if value.respond_to? :call
value = value.call(*args, &b)
else
value
end
value
end
end
end
#Pipeline::Pipe.new(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment