Created
June 13, 2012 18:53
-
-
Save rainkinz/2925769 to your computer and use it in GitHub Desktop.
Ruby Pipes
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
#/usr/bin/env ruby | |
# encoding: utf-8 | |
APP_ROOT=File.expand_path('../../', __FILE__) | |
class Chain | |
def initialize(*procs) | |
@chain = procs | |
end | |
def |(proc) | |
@chain << proc | |
self | |
end | |
def call(args) | |
@chain.inject(args) {|x, p| p.call(x) } | |
end | |
alias [] call | |
end | |
class Proc | |
def |(proc) | |
Chain.new(self, proc) | |
end | |
end | |
# f = lambda {|x| x * 7} | lambda {|x| x + 10 } | lambda {|x| -x } | |
# puts f.call(5) | |
# | |
# puts f[0] | |
# class Method | |
# def |(meth) | |
# Chain.new(self, meth.to_proc) | |
# end | |
# end | |
class BaseStep | |
def |(proc) | |
Chain.new(self, proc) | |
end | |
end | |
class ReadArticles < BaseStep | |
def call(args) | |
puts "Reading articles from #{args}" | |
return "loaded #{args}" | |
end | |
end | |
extract_body = lambda do |args| | |
puts "extracting body" | |
return "extracted body" | |
end | |
def write_html(html) | |
puts "writing html" | |
return "done" | |
end | |
class Symbol | |
# TOTAL HACK | |
def call(args) | |
Object.send(self, args) | |
end | |
end | |
transform = ReadArticles.new | extract_body | :write_html | |
puts transform.call("#{APP_ROOT}/examples/articles") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment