Class: Validation::Validator

Inherits:
Object
  • Object
show all
Includes:
Rules
Defined in:
lib/validation/validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Rules

#validates, #validates_confirmation_of, #validates_format_of, #validates_greather_or_equal_then, #validates_greather_then, #validates_length_of_within, #validates_less_or_equal_then, #validates_less_then, #validates_numericality_of, #validates_option_in, #validates_presence_of, #validates_presence_of_field, #validates_url_format_of

Constructor Details

#initializeValidator

Returns a new instance of Validator.



7
8
9
10
# File 'lib/validation/validator.rb', line 7

def initialize
  @rules = []
  @errors = {}
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



5
6
7
# File 'lib/validation/validator.rb', line 5

def errors
  @errors
end

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/validation/validator.rb', line 21

def valid?
  @errors.empty?
end

#validate(data) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/validation/validator.rb', line 12

def validate data
  @errors = {}
  @rules.each do |rule|
    next if @errors[rule[:message].field]
    @errors[rule[:message].field] = rule[:message] unless rule[:callback].call(data)
  end
  valid?
end