lib/dao/validations.rb in dao-2.1.0 vs lib/dao/validations.rb in dao-2.2.3
- old
+ new
@@ -26,10 +26,12 @@
end
end
end
+ # class methods
+ #
class << Validations
def for(*args, &block)
new(*args, &block)
end
@@ -41,19 +43,20 @@
self.for(*args)
end
end
end
+ # instance methods
+ #
attr_accessor :result
attr_accessor :ran
def initialize(*args, &block)
@result = args.shift if args.first.is_a?(Result)
@ran = false
super
end
-
alias_method('ran?', 'ran')
def params
result.params
end
@@ -73,26 +76,24 @@
def size
size = 0
depth_first_each{ size += 1 }
size
end
-
alias_method('count', 'size')
alias_method('length', 'size')
- Cleared = '___CLEARED___'.freeze unless defined?(Cleared)
+ Cleared = 'Cleared'.freeze unless defined?(Cleared)
def run
previous_errors = []
new_errors = []
errors.each_message do |keys, message|
previous_errors.push([keys, message])
end
+ errors.clear!
- errors.clear
-
depth_first_each do |keys, chain|
chain.each do |callback|
next unless callback and callback.respond_to?(:to_proc)
number_of_errors = errors.size
@@ -156,16 +157,14 @@
def add(*args, &block)
options = Dao.map_for(args.last.is_a?(Hash) ? args.pop : {})
block = args.pop if args.last.respond_to?(:call)
block ||= NotNil
- callback = Validations::Callback.new(options, &block)
+ callback = Callback.new(options, &block)
set(args => Callback::Chain.new) unless has?(args)
get(args).add(callback)
callback
- #args.push(callback)
- #set(*args)
end
end
module Validations::Common
def validates_length_of(*args)
@@ -426,9 +425,145 @@
map
end
validates(*args, &block)
+ end
+
+ def validates_any_of(*args)
+ options = Dao.options_for!(args)
+ list = args
+
+ list.each do |args|
+ candidates = list.dup
+ candidates.delete(args)
+
+ message = options[:message] || "(or #{ candidates.map{|candidate| Array(candidate).join('.')}.join(', ') } ) is blank or missing"
+ allow_nil = options[:allow_nil]
+ allow_blank = options[:allow_blank]
+
+ result = self.result
+
+ block =
+ lambda do |value|
+ map = Dao.map(:valid => true)
+ values = list.map{|key| result.get(key)}
+ valid = false
+ values.each do |val|
+ if val
+ valid = true
+ break
+ end
+
+ if val.nil?
+ if allow_nil
+ valid = true
+ break
+ end
+ end
+
+ val = val.to_s.strip
+
+ if val.empty?
+ if allow_blank
+ valid = true
+ break
+ end
+ end
+ end
+
+ unless valid
+ if value.nil?
+ unless allow_nil
+ map[:message] = message
+ map[:valid] = false
+ throw(:valid, map)
+ end
+ end
+
+ value = value.to_s.strip
+
+ if value.empty?
+ unless allow_blank
+ map[:message] = message
+ map[:valid] = false
+ throw(:valid, map)
+ end
+ end
+ end
+
+ map
+ end
+ validates(*args, &block)
+ end
+ end
+
+ def validates_all_of(*args)
+ options = Dao.options_for!(args)
+ list = args
+
+ list.each do |args|
+ candidates = list.dup
+ candidates.delete(args)
+
+ message = options[:message] || "(and #{ candidates.map{|candidate| Array(candidate).join('.')}.join(', ') } ) is blank or missing"
+ allow_nil = options[:allow_nil]
+ allow_blank = options[:allow_blank]
+
+ result = self.result
+
+ block =
+ lambda do |value|
+ map = Dao.map(:valid => true)
+
+ values = list.map{|key| result.get(key)}
+ valid = true
+ values.each do |val|
+ if val
+ break
+ end
+
+ if val.nil?
+ unless allow_nil
+ valid = false
+ break
+ end
+ end
+
+ val = val.to_s.strip
+
+ if val.empty?
+ unless allow_blank
+ valid = false
+ break
+ end
+ end
+ end
+
+ unless valid
+ if value.nil?
+ unless allow_nil
+ map[:message] = message
+ map[:valid] = false
+ throw(:valid, map)
+ end
+ end
+
+ value = value.to_s.strip
+
+ if value.empty?
+ unless allow_blank
+ map[:message] = message
+ map[:valid] = false
+ throw(:valid, map)
+ end
+ end
+ end
+
+ map
+ end
+ validates(*args, &block)
+ end
end
end
def Validations.add(method_name, &block)
::Dao::Validations::Common.module_eval do