Class: Valideez::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/valideez/base.rb

Direct Known Subclasses

Iban, Nip, Pesel, Phone, Regon, Siret

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(val) ⇒ Base

Returns a new instance of Base.



6
7
8
9
# File 'lib/valideez/base.rb', line 6

def initialize(val)
  @val = val.to_s.gsub(/-/, '').gsub(/\s+/, '')
  @validable = []
end

Instance Attribute Details

#valObject (readonly)

Returns the value of attribute val.



3
4
5
# File 'lib/valideez/base.rb', line 3

def val
  @val
end

#validableObject

Returns the value of attribute validable.



4
5
6
# File 'lib/valideez/base.rb', line 4

def validable
  @validable
end

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
# File 'lib/valideez/base.rb', line 11

def valid?
  # p - Array of methods (Strings) which can be used for validation
  p = validable.select{ |var| respond_to?("validate_#{var.to_s}", true) }
  # execute validations, stop and return false if current validations return false
  p.take_while { |var| send("validate_#{var.to_s}") }.size == p.size
end