Skip to content

Instantly share code, notes, and snippets.

@jumski
Last active February 18, 2016 11:34
Show Gist options
  • Save jumski/4a444ad8132838c6ac33 to your computer and use it in GitHub Desktop.
Save jumski/4a444ad8132838c6ac33 to your computer and use it in GitHub Desktop.
Alternative implementation of codequest-eu/codequest_pipes :-)
class Trough < Array
alias_method :|, :push
def call(context)
reduce(context) do |ctx, pig|
pig.new(ctx).call
ctx
end
end
end
class Pig
def initialize(context)
@context = context
end
def self.|(other)
Trough.new.push(self).push(other)
end
private
attr_reader :context
end
def Closure(&block)
Class.new(Pig) { define_method(:call, &block) }
end
require './piglet.rb'
class Chrum < Pig
def call
puts 'chrum chrum'
context[:chrumed] = true
end
end
class Oink < Pig
def call
puts 'oink oink'
context[:oinked] = true
end
end
TROUGH = Chrum |
Oink |
Closure {
puts 'snort snort'
context[:snorted] = true
}
input_context = {}
puts input_context.inspect
output_context = TROUGH.call(input_context)
puts output_context.inspect
@nathell
Copy link

nathell commented Feb 18, 2016

You're missing pre- and postconditions!

@jumski
Copy link
Author

jumski commented Feb 18, 2016

that's true and intentional, not worth wasting more time on this :) if you want to improve my pig farm, feel free to update Trough#call :)

@marcinwyszynski
Copy link

babies-dance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment