-
-
Save rkh/82440 to your computer and use it in GitHub Desktop.
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
module Enumerable | |
class Evaluator | |
instance_methods.each { |s| undef_method(s) unless s.to_s =~ /__/ } | |
def initialize(enum, invoke=nil, *args) | |
@enum = enum | |
@invoke = invoke | |
@args = args | |
end | |
def not | |
@negate = true | |
self | |
end | |
def are_a? klass | |
is_a? klass | |
end | |
alias are_an? are_a? | |
alias is_an? are_a? | |
def method_missing(sym, *args, &blk) | |
if @invoke | |
res = @enum.send(@invoke, *@args) { |i| i.send(sym, *args, &blk) } | |
@negate ? !res : res | |
else | |
@invoke = sym.to_s + '?' | |
@args = args | |
self | |
end | |
end | |
end | |
def not | |
Evaluator.new(self).not | |
end | |
def any | |
Evaluator.new(self, :any?) | |
end | |
def all | |
Evaluator.new(self, :all?) | |
end | |
def every | |
Evaluator.new(self, :each) | |
end | |
def at_least number | |
Evaluator.new(self, :at_least?, number) | |
end | |
def no_more_than number | |
Evaluator.new(self, :no_more_than?, number) | |
end | |
def at_least? number | |
found = 0 | |
each do |i| | |
found += 1 if yield(i) | |
return true if found >= number | |
end | |
false | |
end | |
def no_more_than? number | |
found = 0 | |
each do |i| | |
found += 1 if yield(i) | |
return false if found > number | |
end | |
true | |
end | |
end |
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
require 'bacon' | |
describe "any" do | |
it "provides proxy" do | |
[nil, :foo].any.nil?.should.be.true | |
[:foo, :bar].any.nil?.should.be.false | |
end | |
it "takes args" do | |
[nil, :bar].any.is_a?(Symbol).should.be.true | |
[nil, 'bar'].any.is_a?(Symbol).should.be.false | |
end | |
it "can be negated" do | |
[nil, :bar].not.any.kind_of?(Symbol).should.be.false | |
[nil, 'foo'].not.any.kind_of?(Symbol).should.be.true | |
end | |
end | |
describe "all" do | |
it "provides proxy" do | |
[nil, nil].all.nil?.should.be.true | |
[nil, :bar].all.nil?.should.be.false | |
end | |
it "takes args" do | |
[:foo, :bar].all.is_a?(Symbol).should.be.true | |
[nil, :foo].all.is_a?(Symbol).should.be.false | |
end | |
it "can be negated" do | |
[:foo, :bar].not.all.kind_of?(Symbol).should.be.false | |
[nil, :foo].not.all.kind_of?(Symbol).should.be.true | |
end | |
end | |
describe "every" do | |
it "provides proxy" do | |
a = ['foo', 'bar'] | |
a.every.upcase! | |
a.should == ['FOO', 'BAR'] | |
end | |
it "takes args and block" do | |
a = ['foo', 'bar'] | |
a.every.gsub!(/^./) { |m| m.upcase } | |
a.should == ['Foo', 'Bar'] | |
end | |
end | |
describe "at_least" do | |
it "provides proxy" do | |
[nil, nil, nil, :foo].at_least(2).nil?.should.be.true | |
[nil, nil, :foo, :bar].at_least(3).nil?.should.be.false | |
end | |
it "takes args" do | |
[:foo, :bar].at_least(1).is_a?(Symbol).should.be.true | |
[nil, :foo].at_least(2).are_a?(Symbol).should.be.false | |
end | |
it "can be negated" do | |
[nil, nil, nil, :foo].not.at_least(2).nil?.should.be.false | |
[nil, nil, :foo, :bar].not.at_least(3).nil?.should.be.true | |
end | |
end | |
describe "no more than" do | |
it "provides proxy" do | |
[nil, nil, :foo, :bar].no_more_than(3).nil?.should.be.true | |
[nil, nil, nil, :foo].no_more_than(2).nil?.should.be.false | |
end | |
it "takes args" do | |
[:foo, :bar, nil].no_more_than(2).are_a?(Symbol).should.be.true | |
[nil, :foo, :bar].no_more_than(1).is_a?(Symbol).should.be.false | |
end | |
it "can be negated" do | |
[nil, nil, nil, :foo].not.no_more_than(2).nil?.should.be.true | |
[nil, nil, :foo, :bar].not.no_more_than(3).nil?.should.be.false | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment