Module: Magpie::Goose::ClassMethods
- Defined in:
- lib/magpie/goose.rb
Instance Method Summary collapse
-
#goose_validate(*ms, &p) ⇒ Object
用户定义验证规则 goose_validate :be_number, :not_blank goose_validate do |item| item.errors << “名字的长度不能超过4” if item.name.length > 4 end param [Array, Proc], 将验证规则添加到@validations中.
- #goose_validate_format_of(*attrs) ⇒ Object
- #goose_validate_length_of(*attrs) ⇒ Object
- #goose_validate_presence_of(*attrs) ⇒ Object
- #parse_options(attrs) ⇒ Object
- #validations ⇒ Object
Instance Method Details
#goose_validate(*ms, &p) ⇒ Object
用户定义验证规则
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 |
#goose_validate_format_of(*attrs) ⇒ Object
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 |
#goose_validate_length_of(*attrs) ⇒ Object
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 |
#goose_validate_presence_of(*attrs) ⇒ Object
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 |
#parse_options(attrs) ⇒ Object
66 67 68 69 |
# File 'lib/magpie/goose.rb', line 66 def (attrs) = attrs.select{ |attr| attr.is_a? Hash} return attrs - , .first end |
#validations ⇒ Object
32 33 34 |
# File 'lib/magpie/goose.rb', line 32 def validations (@validations ||=[]).dup end |