mirror of
https://github.com/zhigang1992/rmq.git
synced 2026-01-12 22:51:53 +08:00
Merge branch 'invalid_valid_selection'
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
module RubyMotionQuery
|
||||
class RMQ
|
||||
|
||||
# @return [Validation]
|
||||
def self.validation
|
||||
Validation
|
||||
@@ -26,6 +27,9 @@ module RubyMotionQuery
|
||||
self
|
||||
end
|
||||
|
||||
# This method validates all the selected and is responsible
|
||||
# for calling invalid/valid events
|
||||
#
|
||||
# @return [Boolean] false if any validations fail
|
||||
def valid?
|
||||
result = true
|
||||
@@ -49,19 +53,44 @@ module RubyMotionQuery
|
||||
end
|
||||
return result
|
||||
end
|
||||
end
|
||||
|
||||
# @return [Array] of validations that have failed
|
||||
def invalid
|
||||
invalid = []
|
||||
selected.each do |view|
|
||||
view.rmq_data.validations.each do |validation|
|
||||
invalid.push(view) unless validation.valid_status
|
||||
end
|
||||
end
|
||||
return invalid
|
||||
end
|
||||
|
||||
# @return [Array] of validations that have failed
|
||||
def valid
|
||||
invalid = []
|
||||
selected.each do |view|
|
||||
view.rmq_data.validations.each do |validation|
|
||||
invalid.push(view) if validation.valid_status
|
||||
end
|
||||
end
|
||||
return invalid
|
||||
end
|
||||
|
||||
end # End RMQ
|
||||
|
||||
class Validation
|
||||
attr_reader :valid_status
|
||||
|
||||
def initialize(rule, options={})
|
||||
@rule = @@validation_methods[rule]
|
||||
@options = options
|
||||
@valid_status = true
|
||||
raise "RMQ validation error: :#{rule} is not one of the supported validation methods." unless @rule
|
||||
end
|
||||
|
||||
def valid?(data, options={})
|
||||
@options = options.merge(@options)
|
||||
@rule.call(data, @options)
|
||||
@valid_status = @rule.call(data, @options)
|
||||
end
|
||||
|
||||
class << self
|
||||
|
||||
@@ -107,7 +107,7 @@ describe 'validation' do
|
||||
@rmq.validation.valid?('test', :length, min_length: 8, max_length: 16).should == false
|
||||
@rmq.validation.valid?('test', :length, exact_length: 2..7).should == true
|
||||
@rmq.validation.valid?('test', :length, exact_length: 8..16).should == false
|
||||
# ignore pre/post whitespace in length
|
||||
# Next 2 lines testing ignore pre/post whitespace in length
|
||||
@rmq.validation.valid?(' test ', :length, min_length: 5).should == true
|
||||
@rmq.validation.valid?(' test ', :length, min_length: 5, strip: true).should == false
|
||||
end
|
||||
@@ -149,6 +149,30 @@ describe 'validation' do
|
||||
|
||||
end
|
||||
|
||||
it 'maintains what selected items are invalid' do
|
||||
vc = UIViewController.alloc.init
|
||||
|
||||
vc.rmq.append(UITextField).validates(:digits).data('taco loco').tag(:one)
|
||||
vc.rmq.append(UITextField).validates(:digits).data('123455').tag(:two)
|
||||
|
||||
#everything is valid by default
|
||||
vc.rmq.all.invalid.should == []
|
||||
vc.rmq.all.valid? # this flags the items
|
||||
vc.rmq.all.invalid.should == [vc.rmq(:one).get]
|
||||
end
|
||||
|
||||
it 'maintains what selected items are valid' do
|
||||
vc = UIViewController.alloc.init
|
||||
|
||||
vc.rmq.append(UITextField).validates(:digits).data('taco loco').tag(:one)
|
||||
vc.rmq.append(UITextField).validates(:digits).data('123455').tag(:two)
|
||||
|
||||
#everything is valid by default
|
||||
vc.rmq.all.valid.size.should == 2
|
||||
vc.rmq.all.valid? # this flags the items
|
||||
vc.rmq.all.valid.should == [vc.rmq(:two).get]
|
||||
end
|
||||
|
||||
it 'can clear all validations' do
|
||||
vc = UIViewController.new
|
||||
|
||||
|
||||
Reference in New Issue
Block a user