Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
source "https://rubygems.org"

gem "rake"
gem "bubble-wrap"
gem "motion-stump", '~>0.2'
gemspec
18 changes: 15 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions motion/validatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 28 additions & 2 deletions spec/validation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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))
Expand Down