Class: DataValidator::BaseValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/validations/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value, options, errors) ⇒ BaseValidator

Returns a new instance of BaseValidator.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/validations/base.rb', line 9

def initialize(name, value, options, errors)
  raise ArgumentError, 'Options must define' if options.blank?

  case options
  when Hash
    @options = options
  else
    @options = {}
  end

  @name   = name
  @value  = value
  @errors = errors
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



7
8
9
# File 'lib/validations/base.rb', line 7

def errors
  @errors
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/validations/base.rb', line 7

def name
  @name
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/validations/base.rb', line 7

def options
  @options
end

#valueObject

Returns the value of attribute value.



7
8
9
# File 'lib/validations/base.rb', line 7

def value
  @value
end

Instance Method Details

#add_error(error_message_key, message_args = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/validations/base.rb', line 31

def add_error(error_message_key, message_args = {})
  error_subject_key = "datavalidator.errors.subjects.#{name}"
  error_subject = ''
  begin
    error_subject = I18n.t! "datavalidator.errors.subjects.#{name}"
  rescue
    error_subject = name.to_s
  end

  if errors.key? name
    errors[name] << "#{error_subject} #{I18n.t("datavalidator.errors.messages.#{error_message_key.to_s}", message_args)}"
  else
    errors[name] = ["#{error_subject} #{I18n.t("datavalidator.errors.messages.#{error_message_key.to_s}", message_args)}"]
  end
end

#check_validity!Object



24
25
# File 'lib/validations/base.rb', line 24

def check_validity!
end

#validateObject

Raises:

  • (ArgumentError)


27
28
29
# File 'lib/validations/base.rb', line 27

def validate
  raise ArgumentError, 'Validate method is necessary'
end