Module: Aba::Validations

Included in:
Aba, Transaction
Defined in:
lib/aba/validations.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



3
4
5
# File 'lib/aba/validations.rb', line 3

def errors
  @errors
end

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/aba/validations.rb', line 5

def self.included(base)
  base.instance_eval do
    @_validations = {}
  end

  base.send :extend, ClassMethods
end

Instance Method Details

#valid?Boolean

Run all validations

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/aba/validations.rb', line 14

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