Last active
December 14, 2015 15:18
-
-
Save shelling/5106268 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| class Foo | |
| # has_many :bar | |
| attr_accessor :bar | |
| def method_missing(method, *args, &block) | |
| if method.to_s =~ /(\w+)_on/ | |
| raise Exception, "no condition given to filter `#{$1}'" unless args[0] | |
| result = self.send($1.to_sym) | |
| args[0].each { |field, value| | |
| result = result.select { |item| | |
| item.send(field) == value | |
| } | |
| } | |
| result | |
| end | |
| end | |
| end | |
| class Bar | |
| # belongs_to :foo | |
| attr_accessor :number, :texture | |
| def initialize(params={}) | |
| @number = params[:number] | |
| @texture = params[:texture] | |
| end | |
| end | |
| describe Foo do | |
| before(:each) do | |
| @foo = Foo.new | |
| @foo.bar = (1..9).map { |i| Bar.new(:number => i, :texture => (i>5 ? "wood": "iron")) } | |
| end | |
| it "has a bar number is 8" do | |
| @foo.should have(1).bar_on(:texture => "wood", :number => 8) | |
| @foo.bar_on(:texture => "wood", :number => 8).length.should == 1 | |
| end | |
| it "has no wood bar number is 4" do | |
| @foo.should have(0).bar_on(:texture => "wood", :number => 4) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment