Class: PoroValidator::Validators::PresenceValidator

Inherits:
BaseClass
  • Object
show all
Defined in:
lib/poro_validator/validators/presence_validator.rb

Overview

Since:

  • 0.0.1

Constant Summary collapse

BLANK_STRING_MATCHER =

Since:

  • 0.0.1

/\A[[:space:]]*\z/.freeze

Instance Attribute Summary

Attributes inherited from BaseClass

#attribute

Instance Method Summary collapse

Methods inherited from BaseClass

#__validate__, #context, #errors, #initialize, #nested?, #options, #value

Constructor Details

This class inherits a constructor from PoroValidator::Validators::BaseClass

Instance Method Details

#validate(attribute, value, options) ⇒ Object

Since:

  • 0.0.1



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/poro_validator/validators/presence_validator.rb', line 6

def validate(attribute, value, options)
  allow_blank = options.fetch(:allow_blank, false)
  message = options.fetch(:message, :presence)

  if value.is_a?(::String)
    if value.gsub(/\s+/, '').match(BLANK_STRING_MATCHER)
      errors.add(attribute, message) unless allow_blank
      return
    end
  end

  if value.nil? || (value.respond_to?(:empty?) && value.empty?)
    errors.add(attribute, message)
  end
end