Class: SimpleParams::NestedErrors

Inherits:
ActiveModel::Errors
  • Object
show all
Defined in:
lib/simple_params/nested_errors.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ NestedErrors

Returns a new instance of NestedErrors.



7
8
9
10
# File 'lib/simple_params/nested_errors.rb', line 7

def initialize(base)
  super(base)
  @base = base
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



5
6
7
# File 'lib/simple_params/nested_errors.rb', line 5

def base
  @base
end

Instance Method Details

#[](attribute) ⇒ Object



20
21
22
# File 'lib/simple_params/nested_errors.rb', line 20

def [](attribute)
  super(attribute)
end

#[]=(attribute, error) ⇒ Object



24
25
26
# File 'lib/simple_params/nested_errors.rb', line 24

def []=(attribute, error)
  add_error_to_attribute(attribute, error)
end

#add(attribute, message = :invalid, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/simple_params/nested_errors.rb', line 28

def add(attribute, message = :invalid, options = {})
  message = normalize_message(attribute, message, options)
  if exception = options[:strict]
    exception = ActiveModel::StrictValidationFailed if exception == true
    raise exception, full_message(attribute, message)
  end

  add_error_to_attribute(attribute, message)
end

#array?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/simple_params/nested_errors.rb', line 12

def array?
  @base.array?
end

#clearObject



38
39
40
# File 'lib/simple_params/nested_errors.rb', line 38

def clear
  super
end

#empty?Boolean Also known as: blank?

Returns:

  • (Boolean)


42
43
44
# File 'lib/simple_params/nested_errors.rb', line 42

def empty?
  super
end

#full_messagesObject



57
58
59
# File 'lib/simple_params/nested_errors.rb', line 57

def full_messages
  map { |attribute, message| full_message(attribute, message) }
end

#hash?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/simple_params/nested_errors.rb', line 16

def hash?
  @base.hash?
end

#include?(attribute) ⇒ Boolean Also known as: has_key?, key?

Returns:

  • (Boolean)


47
48
49
# File 'lib/simple_params/nested_errors.rb', line 47

def include?(attribute)
  messages[attribute].present?
end

#to_aObject



61
62
63
# File 'lib/simple_params/nested_errors.rb', line 61

def to_a
  full_messages
end

#to_hash(full_messages = false) ⇒ Object



65
66
67
# File 'lib/simple_params/nested_errors.rb', line 65

def to_hash(full_messages = false)
  get_messages(self, full_messages)
end

#to_s(full_messages = false) ⇒ Object



69
70
71
72
# File 'lib/simple_params/nested_errors.rb', line 69

def to_s(full_messages = false)
  array = to_a.compact
  array.join(', ')
end

#valuesObject



53
54
55
# File 'lib/simple_params/nested_errors.rb', line 53

def values
  messages.values
end