Sha256: 47ad44b3e2683dae47aad5a583044154bd300463c40590a1d62343c23d4171ec
Contents?: true
Size: 1.51 KB
Versions: 2
Compression:
Stored size: 1.51 KB
Contents
class Aba module Validations attr_accessor :errors def self.included(base) base.instance_eval do @_validations = {} end base.send :extend, ClassMethods end # Run all validations def valid? self.errors = [] self.class.instance_variable_get(:@_validations).each do |attribute, validations| value = send(attribute) validations.each do |type, param| case type when :presence self.errors << "#{attribute} is empty" if value.nil? || value.to_s.empty? when :bsb self.errors << "#{attribute} format is incorrect" unless value =~ /^\d{3}-\d{3}$/ when :max_length self.errors << "#{attribute} length must not exceed #{param} characters" if value.to_s.length > param end end end self.errors.empty? end module ClassMethods def validates_presence_of(*attributes) attributes.each do |a| add_validation_attribute(a, :presence) end end def validates_bsb(*attributes) attributes.each do |a| add_validation_attribute(a, :bsb) end end def validates_max_length(attribute, length) add_validation_attribute(attribute, :max_length, length) end private def add_validation_attribute(attribute, type, param = true) @_validations[attribute] = {} unless @_validations[attribute] @_validations[attribute][type] = param end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
aba-0.1.0 | lib/aba/validations.rb |
aba-0.0.1 | lib/aba/validations.rb |