module WageSlave::Validate::InstanceMethods

Attributes

errors[R]
object[R]

Public Class Methods

new(object) click to toggle source
# File lib/wage_slave/validation.rb, line 47
def initialize(object)
  @object = object
  @errors = ValidationErrors.new
end

Public Instance Methods

valid?() click to toggle source
# File lib/wage_slave/validation.rb, line 52
def valid?
  self.class.validators.each { |args| validate(args) }
  @errors.all.empty?
end

Private Instance Methods

type_validator(name, options) click to toggle source
# File lib/wage_slave/validation.rb, line 74
def type_validator(name, options)
  return if @object.send(name).is_a?(options[:type])
  @errors.add(name, "must be a #{options[:type].name}")
end
validate(args) click to toggle source
# File lib/wage_slave/validation.rb, line 59
def validate(args)
  case
  when args[1].key?(:with)
    with_validator(*args)
  when args[1].key?(:type)
    type_validator(*args)
  end
end
with_validator(name, options) click to toggle source
# File lib/wage_slave/validation.rb, line 68
def with_validator(name, options)
  fail unless options[:with].call(@object)
rescue
  @errors.add(name, options[:msg])
end