Class: ReeValidator::ValidateLength

Inherits:
Object
  • Object
show all
Includes:
Ree::FnDSL
Defined in:
lib/ree_lib/packages/ree_validator/package/ree_validator/functions/validate_length.rb

Constant Summary collapse

LenthErr =
Class.new(StandardError)

Instance Method Summary collapse

Instance Method Details

#call(object, error = nil, **opts) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ree_lib/packages/ree_validator/package/ree_validator/functions/validate_length.rb', line 22

def call(object, error = nil, **opts)
  min, max, equal_to, not_equal_to = opts.values_at(:min, :max, :equal_to, :not_equal_to)

  if min && object.length < min
    error ||= LenthErr.new(
      t(
        'validator.length.can_not_be_less_than',
        {length: min},
        default_by_locale: :en
      )
    )

    raise error
  end

  if max && object.length > max
    error ||= LenthErr.new(
      t(
        'validator.length.can_not_be_more_than',
        {length: max},
        default_by_locale: :en
      )
    )

    raise error
  end

  if equal_to && object.length != equal_to
    error ||= LenthErr.new(
      t(
        'validator.length.should_be_equal_to',
        {length: equal_to},
        default_by_locale: :en
      )
    )

    raise error
  end

  if not_equal_to && object.length == not_equal_to
    error ||= LenthErr.new(
      t(
        'validator.length.should_not_be_equal_to',
        {length: not_equal_to},
        default_by_locale: :en
      )
    )

    raise error
  end

  true
end