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',

    before: 'must be before given date',
    after: 'must be after given date',
    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



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

def index_ordinal(index)
  index&.+(1)&.ordinalize
end

#message(atom, parent_key, index) ⇒ Object



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

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