diff --git a/.travis.yml b/.travis.yml index 1d8c67f..a5df186 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,9 @@ language: objective-c before_install: - ruby --version - sudo chown -R travis ~/Library/RubyMotion -rvm: - - "1.9.3" -script: bundle exec rake spec + - mkdir -p ~/Library/RubyMotion/build + - sudo motion update +gemfile: + - Gemfile +script: + - bundle exec rake spec diff --git a/Gemfile b/Gemfile index f486d81..af881e0 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,5 @@ source "https://rubygems.org" gem "rake" -gem "bubble-wrap" gem "motion-stump", '~>0.2' +gemspec diff --git a/Gemfile.lock b/Gemfile.lock index 15b3e33..ea60af2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,14 +1,26 @@ +PATH + remote: . + specs: + motion_model (0.6.0) + bubble-wrap (>= 1.3.0) + motion-support (>= 0.1.0) + GEM remote: https://rubygems.org/ specs: - bubble-wrap (1.4.0) + bubble-wrap (1.7.1) + bubble-wrap-http (= 1.7.1) + bubble-wrap-http (1.7.1) + motion-require (0.2.0) motion-stump (0.3.0) - rake (10.1.0) + motion-support (0.2.6) + motion-require (>= 0.0.6) + rake (10.3.2) PLATFORMS ruby DEPENDENCIES - bubble-wrap motion-stump (~> 0.2) + motion_model! rake diff --git a/README.md b/README.md index 14c2920..062f349 100644 --- a/README.md +++ b/README.md @@ -362,6 +362,19 @@ Here are some sample validations: validate :field_name, :email => true validate :field_name, :format => /\A\d?\d-\d?\d-\d\d\Z/ # expected string format would be like '12-12-12' +You can also define conditions to execute validations just in some contexts: +```ruby +# using a method passed as symbol +validate :field_name, :presence => true, :if => :some_context? + +# or directly with proc/lambda +validate :filed_name, :presence => true, :if => -> { # stuff here } + +def some_context? + # Your conditional stuff here +end +``` + The framework is sufficiently flexible that you can add in custom validators like so: ```ruby diff --git a/motion/validatable.rb b/motion/validatable.rb index c545a06..d43556b 100644 --- a/motion/validatable.rb +++ b/motion/validatable.rb @@ -130,7 +130,22 @@ def validate_for(field, value) result end + def must_validate?(validation) + stuff = validation[:if] + return true unless stuff + + case stuff + when Symbol then send(stuff) + when Proc then instance_exec(&stuff) + else + fail ArgumentError, + ':if requires a Symbol denoting a method or a Proc/lambda' + end + end + def validate_one(field, validation) #nodoc + return true unless must_validate?(validation) + result = true validation.each_pair do |validation_type, setting| if self.respond_to? validation_method(validation_type) diff --git a/spec/validation_spec.rb b/spec/validation_spec.rb index c9994d4..b0c0933 100644 --- a/spec/validation_spec.rb +++ b/spec/validation_spec.rb @@ -6,7 +6,9 @@ class ValidatableTask :email => :string, :some_day => :string, :some_float => :float, - :some_int => :int + :some_int => :int, + :if_with_proc => :string, + :if_with_sym => :string validate :name, :presence => true validate :name, :length => 2..10 @@ -15,6 +17,12 @@ class ValidatableTask validate :some_day, :length => 8..10 validate :some_float, :presence => true validate :some_int, :presence => true + validate :if_with_proc, :presence => true, :if => -> { if_with_proc == 'unexpected' } + validate :if_with_sym, :presence => true, :if => :false_condition_method + + def false_condition_method + false + end end describe "validations" do @@ -24,10 +32,28 @@ class ValidatableTask :email => 'bob@domain.com', :some_day => '12-12-12', :some_float => 1.080, - :some_int => 99 + :some_int => 99, + :if_with_proc => 'Never validated column', + :if_with_sym => 'Never validated column' } end + describe 'conditional validation' do + context 'with conditional by method' do + it 'does not validate if condition returns false' do + task = ValidatableTask.new(@valid_tasks.except(:if_with_sym)) + task.valid?.should == true + end + end + + context 'with conditional by proc' do + it 'does not validate if condition returns false' do + task = ValidatableTask.new(@valid_tasks.except(:if_with_proc)) + task.valid?.should == true + end + end + end + describe "presence" do it "is initially false if name is blank" do task = ValidatableTask.new(@valid_tasks.except(:name))