class Wardrobe::Plugins::Validation::Validator

Attributes

atr[R]
error_store[R]
validation[R]
value[R]

Public Class Methods

new(value, atr, error_store, validation = nil) click to toggle source
# File lib/wardrobe/plugins/validation/validator.rb, line 10
def initialize(value, atr, error_store, validation = nil)
  @value = value
  @atr = atr
  @error_store = error_store
  @validation = validation || atr.options[:validates]
end

Public Instance Methods

run(report = true) click to toggle source
# File lib/wardrobe/plugins/validation/validator.rb, line 17
def run(report = true)
  if validation
    validate(validation, report)
  elsif value.respond_to?(:_validate!)
    if value._validation_errors.any?
      error_store.store[atr.name] = value._validation_errors
    end
  else
    Wardrobe.logger.warn("Unable to validate #{value.class} class")
  end
end

Private Instance Methods

_and_(validations, report) click to toggle source
# File lib/wardrobe/plugins/validation/validator.rb, line 96
def _and_(validations, report)
  errors = validate_list(validations, false)
  report ? report(*errors) : errors
end
_optional_(validation, report) click to toggle source
# File lib/wardrobe/plugins/validation/validator.rb, line 111
def _optional_(validation, report)
  validate(validation, report)
rescue NoMethodError => e
  raise e unless value.nil?
end
_or_(validations, report) click to toggle source
# File lib/wardrobe/plugins/validation/validator.rb, line 89
def _or_(validations, report)
  errors = validate_list(validations, false)
  error_string = errors.join(' or ')
  report(error_string) if validations.size == errors.size && report
  error_string
end
_then_(validations, report) click to toggle source
# File lib/wardrobe/plugins/validation/validator.rb, line 101
def _then_(validations, report)
  error = validate(validations.first, false)
  if error
    report(error) if report
    error
  else
    validate_list(validations[1..-1], report)
  end
end
each?(validation, report) click to toggle source
# File lib/wardrobe/plugins/validation/validator.rb, line 50
def each?(validation, report)
  errors = {}
  value.each_with_index do |item, index|
    result = Validator.new(item, nil, nil, validation).run(false)
    result = [result] unless result.is_a?(Array)
    errors[index] = result if result.any?
  end
  if errors.any?
    report(errors) if report
    errors
  end
end
each_key?(validation, report) click to toggle source
# File lib/wardrobe/plugins/validation/validator.rb, line 63
def each_key?(validation, report)
  errors = {}
  value.each do |key, _|
    result = Validator.new(key, nil, nil, validation).run(false)
    result = [result] unless result.is_a?(Array)
    errors[key] = result.map { |error| 'key ' + error } if result.any?
  end
  if errors.any?
    report(errors) if report
    errors
  end
end
each_value?(validation, report) click to toggle source
# File lib/wardrobe/plugins/validation/validator.rb, line 76
def each_value?(validation, report)
  errors = {}
  value.each do |key, val|
    result = Validator.new(val, nil, nil, validation).run(false)
    result = [result] unless result.is_a?(Array)
    errors[key] = result.map { |error| 'value ' + error } if result.any?
  end
  if errors.any?
    report(errors) if report
    errors
  end
end
report(*errors) click to toggle source
# File lib/wardrobe/plugins/validation/validator.rb, line 31
def report(*errors)
  error_store.add(atr, *errors)
end
validate(validation, report) click to toggle source
# File lib/wardrobe/plugins/validation/validator.rb, line 35
def validate(validation, report)
  if validation.type == :special#method[/^_.+_$/]
    send(*validation.args, report)
  else
    error = value.send(*validation.args)
    report && error ? report(error) : error
  end
end
validate_list(validations, report) click to toggle source
# File lib/wardrobe/plugins/validation/validator.rb, line 44
def validate_list(validations, report)
  validations.map do |validation|
    validate(validation, report)
  end.compact
end