Class: Objective::Errors::ErrorMessageCreator

Inherits:
Object
  • Object
show all
Defined in:
lib/objective/errors/error_message_creator.rb

Constant Summary collapse

MESSAGES =
Hash.new('is invalid').tap do |h|
  h.merge!(
    nils: 'cannot be nil',
    required: 'is required',

    string: 'must be a string',
    integer: 'must be an integer',
    decimal: 'must be a number',
    boolean: 'must be a boolean',
    hash: 'must be a hash',
    array: 'must be an array',
    model: 'must be the right class',
    date: 'date does non exist',

    empty: 'cannot be empty',
    matches: 'has an incorrect format',
    in: 'is not an available option',
    min: 'is too small',
    max: 'is too big',
    new_records: 'model must be saved'
  )
end

Instance Method Summary collapse

Instance Method Details

#index_ordinal(index) ⇒ Object



39
40
41
42
# File 'lib/objective/errors/error_message_creator.rb', line 39

def index_ordinal(index)
  return if index.nil?
  (index + 1).ordinalize
end

#message(atom, parent_key, index) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/objective/errors/error_message_creator.rb', line 29

def message(atom, parent_key, index)
  [
    index_ordinal(index),
    (atom.key || parent_key || 'item').to_s.titleize,
    MESSAGES[atom.codes]
  ]
    .compact
    .join(' ')
end