Class: Valideizer::Core

Inherits:
Object
  • Object
show all
Includes:
Caster, ErrorPrinter, Rules, RulesChecker
Defined in:
lib/valideizer/core.rb

Constant Summary

Constants included from ErrorPrinter

ErrorPrinter::PREFIX

Constants included from RulesChecker

RulesChecker::RULES_EXCEPTIONS, RulesChecker::RULES_W_FRIENDLY, RulesChecker::VALID_TYPE_RULES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Caster

#cast_from_json, #cast_to_boolean, #cast_to_float, #cast_to_integer, #cast_to_time, #cast_to_time_with_format

Methods included from RulesChecker

#check_rule_set

Methods included from Rules

#validate

Constructor Details

#initialize(autocast: true) ⇒ Core

Returns a new instance of Core.



15
16
17
18
19
# File 'lib/valideizer/core.rb', line 15

def initialize(autocast: true)
  @autocast = autocast
  @rules = {}
  reinit_errrors
end

Instance Attribute Details

#rulesObject

Returns the value of attribute rules.



13
14
15
# File 'lib/valideizer/core.rb', line 13

def rules
  @rules
end

Instance Method Details

#add_rule(param, *rules) ⇒ Object Also known as: valideize

Adds new rule for validation



28
29
30
31
# File 'lib/valideizer/core.rb', line 28

def add_rule(param, *rules)
  check_rule_set(rules[0])
  @rules[param.to_s] = rules[0]
end

#clean!Object

Cleanes rules and errors



22
23
24
25
# File 'lib/valideizer/core.rb', line 22

def clean!
  @rules = {}
  reinit_errrors
end

#errorsObject

Prints error messages



59
60
61
62
# File 'lib/valideizer/core.rb', line 59

def errors
  build_error_messages
  @error_messages
end

#valideized?(params) ⇒ Boolean

Validates and recasts params

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/valideizer/core.rb', line 36

def valideized?(params)
  pre_process params

  params.each do |param, value|
    next unless null_check(param, value)
    @rules[param.to_s].each do |type, constraint|
      begin
        push_error(param, value, type, constraint) unless validate(value, type, constraint)
      rescue ArgumentError => ex
        puts ex
      end
    end if @rules.include? param.to_s
  end

  if @errors.empty?
    post_process params
    true
  else
    false
  end
end