Module: Magpie::Goose::ClassMethods
- Defined in:
- lib/magpie/goose.rb
Instance Method Summary (collapse)
-
- (Object) goose_validate(*ms, &p)
用户定义验证规则.
- - (Object) goose_validate_format_of(*attrs)
- - (Object) goose_validate_length_of(*attrs)
- - (Object) goose_validate_presence_of(*attrs)
- - (Object) parse_options(attrs)
- - (Object) validations
Instance Method Details
- (Object) goose_validate(*ms, &p)
用户定义验证规则
goose_validate :be_number, :not_blank goose_validate do |item| item.errors[:name] << "名字的长度不能超过4" if item.name.length > 4 end
param [Array, Proc], 将验证规则添加到@validations中
24 25 26 27 28 29 30 |
# File 'lib/magpie/goose.rb', line 24 def goose_validate(*ms, &p) @validations ||= [] unless ms.to_s.length == 0 ms.each { |m| @validations << m.to_s unless @validations.member?(m.to_s)} end @validations << p unless p.to_s.length == 0 end |
- (Object) goose_validate_format_of(*attrs)
44 45 46 47 48 49 50 51 52 |
# File 'lib/magpie/goose.rb', line 44 def goose_validate_format_of(*attrs) attrs, = (attrs) attrs.each do |attr| goose_validate { |am| am.errors[attr] << ([:msg] || "格式错误") unless am.send(attr) =~ [:with] or ([:allow_blank] and am.send(attr).blank?) } end end |
- (Object) goose_validate_length_of(*attrs)
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/magpie/goose.rb', line 54 def goose_validate_length_of(*attrs) attrs, = attrs min_length = [:min_length] || 0 max_length = [:max_length] attrs.each do |attr| goose_validate { |am| attr_length = am.send(attr).to_s.length am.errors[attr] << ([:msg] || "长度错误") unless max_length.blank? or (attr_length >= min_length and attr_length <= max_length) or ([:allow_blank] and am.send(attr).blank?) } end end |
- (Object) goose_validate_presence_of(*attrs)
36 37 38 39 40 41 42 |
# File 'lib/magpie/goose.rb', line 36 def goose_validate_presence_of(*attrs) attrs.each { |attr| goose_validate do |am| am.errors[attr] << "can't be blank" if am.send(attr).blank? end } end |
- (Object) parse_options(attrs)
66 67 68 69 |
# File 'lib/magpie/goose.rb', line 66 def (attrs) = attrs.select{ |attr| attr.is_a? Hash} return attrs - , .first end |
- (Object) validations
32 33 34 |
# File 'lib/magpie/goose.rb', line 32 def validations (@validations ||=[]).dup end |