Created
April 18, 2012 03:06
-
-
Save btaitelb/2410826 to your computer and use it in GitHub Desktop.
some old java code that happens to be in ruby
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
# | |
# 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