From 233dbb96da1969e513bd23affcb14f99101a67b0 Mon Sep 17 00:00:00 2001 From: Ryan Johnson Date: Sun, 6 May 2012 15:17:55 -0400 Subject: [PATCH] version 1.3.8 --- CHANGELOG.md | 7 ++ lib/ratatouille/ratifier.rb | 17 +++ lib/ratatouille/version.rb | 2 +- spec/lib/ratatouille/ratifier_spec.rb | 142 ++++++++++++++------------ 4 files changed, 100 insertions(+), 68 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d3d98e..a23ae10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## 1.3.8 + +### Updates + +* New is\_boolean method to test if ratifiable\_object value is of a TrueClass or FalseClass. +* All other classes fail validation. + ## 1.3.6 ### Updates diff --git a/lib/ratatouille/ratifier.rb b/lib/ratatouille/ratifier.rb index feda002..d154527 100644 --- a/lib/ratatouille/ratifier.rb +++ b/lib/ratatouille/ratifier.rb @@ -146,6 +146,23 @@ def is_a?(klass=nil, &block) end#is_a? + # Check if ratifiable object is a TrueClass or FalseClass. + # Any other class will result in a failed validation. + # + # @return [Boolean] + def is_boolean(&block) + case @ratifiable_object + when TrueClass, FalseClass + instance_eval(&block) if block_given? + else + validation_error("object is not a boolean") + return + end + rescue Exception => e + validation_error("#{e.message}", "/") + end#is_boolean? + + # Parse out common options into instance_variables for use within the # validation methods defined in various places. # diff --git a/lib/ratatouille/version.rb b/lib/ratatouille/version.rb index daa5e3d..fc99409 100644 --- a/lib/ratatouille/version.rb +++ b/lib/ratatouille/version.rb @@ -1,4 +1,4 @@ module Ratatouille # Gem Version - VERSION = "1.3.6" + VERSION = "1.3.8" end diff --git a/spec/lib/ratatouille/ratifier_spec.rb b/spec/lib/ratatouille/ratifier_spec.rb index cdee56e..1aa0042 100644 --- a/spec/lib/ratatouille/ratifier_spec.rb +++ b/spec/lib/ratatouille/ratifier_spec.rb @@ -3,32 +3,58 @@ class Something; end describe Ratatouille::Ratifier do + let(:valid_ratifier) { RatifierTest.new({}) } + let(:invalid_ratifier) { RatifierTest.new({}) { is_not_empty } } - it "should be valid on instantiation of new object" do - e = RatifierTest.new({}) - e.should be_valid - end + context ".new" do + it "should be valid" do + valid_ratifier.should be_valid + end + + context "errors_array" do + it "should be empty on new object" do + valid_ratifier.errors_array.should be_empty + end + end - it "should not progress into block if :is_a validation fails" do - f = false - RatifierTest.new({}, :is_a => String) { f = true } - f.should be_false + context "errors within validation block" do + before(:each) do + x = {} + RatifierTest.new({}){ x = @errors } + @errs = x + end - g = false - RatifierTest.new({}, :is_a => Hash) { g = true } - g.should be_true + it "errors should contain one key" do + @errs.keys.size.should == 1 + @errs.keys.should == ['/'] + end - h = false - RatifierTest.new({}) { h = true } - h.should be_true - end + it "errors['/'] should be empty" do + @errs['/'].should be_empty + end + end - it "errors should contain one key within block of new instance" do - x = {} - e = RatifierTest.new({}){ x = @errors } - x.keys.size.should == 1 - x.keys.should == ['/'] - x['/'].should be_empty + context "with :is_a" do + it "shouldn't enter validation block for Hash if expecting a String" do + block_entered = false + RatifierTest.new({}, :is_a => String) { block_entered = true } + block_entered.should be_false + end + + it "should enter validation block for Hash if expecting a Hash" do + block_entered = false + RatifierTest.new({}, :is_a => Hash) { block_entered = true } + block_entered.should be_true + end + end + + context "without :is_a" do + it "should enter validation block" do + block_entered = false + RatifierTest.new({}) { block_entered = true } + block_entered.should be_true + end + end end describe "when attempting to call an undefined method" do @@ -67,11 +93,6 @@ class Something; end end it "should add the error to the '/' context by default" do - test = RatifierTest.new({}) do - # NO ERRORS - end - test.errors['/'].should be_empty - test = RatifierTest.new({}) do validation_error("foo") end @@ -80,9 +101,7 @@ class Something; end it "should add an error to an explicit context (even if it doesn't exist)" do ctxt = "foo" - test = RatifierTest.new({}) do - # NO ERRORS - end + test = valid_ratifier test.errors[ctxt].should be_nil test = RatifierTest.new({}) do @@ -93,72 +112,56 @@ class Something; end end describe "valid?" do - it "should be true if errors is empty?" do - test = RatifierTest.new({}) do - # No Validation = Valid Object - end - test.valid?.should be_true - end end describe "instance variables" do - before(:each) do - @test = RatifierTest.new({}) - end - describe "ratifiable_object" do it "should raise error if modification is attempted" do - Proc.new { @test.ratifiable_object = {} }.should raise_error NoMethodError + Proc.new { valid_ratifier.ratifiable_object = {} }.should raise_error NoMethodError end end describe "errors" do it "should raise error if modification is attempted" do - Proc.new { @test.errors = {} }.should raise_error NoMethodError - Proc.new { @test.errors.delete("/") }.should raise_error TypeError + Proc.new { valid_ratifier.errors = {} }.should raise_error NoMethodError + Proc.new { valid_ratifier.errors.delete("/") }.should raise_error TypeError end it "should be empty on valid object" do - ratifier = RatifierTest.new({:foo => "bar"}) do - # No Validations = Valid Object - end - ratifier.errors.should be_empty + valid_ratifier.errors.should be_empty end it "should not be empty on invalid object" do - ratifier = RatifierTest.new({:foo => "bar"}) { is_empty } - ratifier.errors.should_not be_empty + invalid_ratifier.errors.should_not be_empty end end describe "errors_array" do - it "should be empty on new Ratifier" do - @test.errors_array.should be_empty + it "should have at least one String item for an invalid object" do + invalid_ratifier.errors_array.should have_at_least(1).String end + end + end - it "should be empty on valid object" do - ratifier = RatifierTest.new({}) do - # No Validations = Valid Object + describe "is_boolean" do + [true, false].each do |b| + it "should enter block for #{b} value" do + block_entered = false + RatifierTest.new(b) do + is_boolean { block_entered = true } end - ratifier.errors_array.should be_empty - end - - it "should have at least one String item for an invalid object" do - test = RatifierTest.new({:foo => "bar"}){ is_empty } - test.errors_array.should_not be_empty - test.errors_array.should have_at_least(1).String + block_entered.should be_true end end end describe "name" do - it "should return the same value if called twice in a row" do - r = RatifierTest.new({}) - r.name.should == r.name + it "should return same value if called twice in a row" do + valid_ratifier.name.should == valid_ratifier.name end it "should always return a String" do - RatifierTest.new({}).name.should be_a String + valid_ratifier.name.should be_a String end it "should return the class of the object if :name isn't passed into options" do @@ -175,7 +178,9 @@ class Something; end it "should not change the name if passed a non-string name" do r = RatifierTest.new({}) r.name = NilClass + r.name.should == 'Hash' r.name = Object.new + r.name.should == 'Hash' r.name = nil r.name.should == 'Hash' end @@ -202,22 +207,25 @@ class Something; end ].each do |obj, klass| it "#{obj.inspect} should be valid if matches #{klass}" do RatifierTest.new(obj) { is_a?(klass) }.should be_valid + end + + it "#{obj.inspect} should NOT be valid if expecting Something object" do RatifierTest.new(obj) { is_a?(Something) }.should_not be_valid end end end describe "method_missing" do - describe "for non-standard boolean methods" do + context "with non-standard boolean methods" do + let(:obj) { Object.new } + it "should render object invalid for given method" do - obj = Object.new obj.stub(:foo?).and_return(false) RatifierTest.new(obj) { is_foo }.should_not be_valid RatifierTest.new(obj) { is_not_foo }.should be_valid end it "should render object valid for given method" do - obj = Object.new obj.stub(:bar?).and_return(true) RatifierTest.new(obj) { is_bar }.should be_valid RatifierTest.new(obj) { is_not_bar }.should_not be_valid