Skip to content

Instantly share code, notes, and snippets.

@btaitelb
Created April 18, 2012 03:06
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 btaitelb/2410826 to your computer and use it in GitHub Desktop.
Save btaitelb/2410826 to your computer and use it in GitHub Desktop.
some old java code that happens to be in ruby
#
# strategies.rb
# reframeit-testing
#
# Created by Ben Taitelbaum on 2/25/08.
# Copyright (c) 2008 __MyCompanyName__. All rights reserved.
#
module ActiveThrift
class CompoundStrategy
def initialize(&block)
@strategy = block || Proc.new {|arg, opts| false }
end
def execute(*opts)
@strategy.call(*opts)
end
# chains this strategy with the next one so that the next
# one executes only if this one evaluates to true
def &(compound_strategy)
CompoundStrategy.new do |*opts|
@strategy.call(*opts) && compound_strategy.strategy.call(*opts)
end
end
# chains this strategy with the next one, where each will be executed
# and the result ignored
def +(compound_strategy)
CompoundStrategy.new do |*opts|
@strategy.call(*opts)
compound_strategy.strategy.call(*opts)
end
end
# chains this strategy with the next one, where the output of this
# is passed as the first argument to the next
#
# this requires that the strategy takes at least one argument, where the
# first is used as the piped arg
def |(compound_strategy)
CompoundStrategy.new do |arg, *opts|
ret = @strategy.call(arg, *opts)
compound_strategy.strategy.call(ret, *opts)
end
end
protected
attr_accessor :strategy
end
class SingletonStrategy < CompoundStrategy
def self.instance
@instance ||= self.send(:new)
return @instance
end
end
class TrueStrategy < SingletonStrategy
private
def initialize
super{true}
end
end
class FalseStrategy < SingletonStrategy
private
def initialize
super{false}
end
end
class IdentityStrategy < SingletonStrategy
private
def initialize
super{|arg, opts| arg}
end
end
class VoidStrategy < SingletonStrategy
private
def initialize
super{}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment