Class: Gecko::Errors

Inherits:
Object
  • Object
show all
Defined in:
lib/gecko/helpers/validation_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ Errors

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set up the error object



36
37
38
39
# File 'lib/gecko/helpers/validation_helper.rb', line 36

def initialize(base)
  @base     = base
  @messages = {}
end

Instance Attribute Details

#messagesHash (readonly)

The hash of errors for this record

Returns:

  • (Hash)


31
32
33
# File 'lib/gecko/helpers/validation_helper.rb', line 31

def messages
  @messages
end

Instance Method Details

#[](attribute) ⇒ Array

Fetch the errors for a specific attribute

Examples:

product.errors[:name]
  #=> ["can't be blank"]

Returns:

  • (Array)


52
53
54
# File 'lib/gecko/helpers/validation_helper.rb', line 52

def [](attribute)
  messages[attribute.to_sym]
end

#clearundefined

Clear the validation errors

Examples:

product.errors.clear

Returns:

  • (undefined)


64
65
66
# File 'lib/gecko/helpers/validation_helper.rb', line 64

def clear
  @messages.clear
end

#empty?Boolean

Whether there are any errors

Returns:

  • (Boolean)


73
74
75
# File 'lib/gecko/helpers/validation_helper.rb', line 73

def empty?
  messages.all? { |k, v| v && v.empty? && !v.is_a?(String) }
end

#from_response(error_hash) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse JSON errors response into the error object

Returns:

  • (undefined)


85
86
87
88
89
# File 'lib/gecko/helpers/validation_helper.rb', line 85

def from_response(error_hash)
  error_hash.each do |attr, errors|
    @messages[attr.to_sym] = errors
  end
end