Class: SimpleParams::Errors

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, nested_hash_errors = {}, nested_array_errors = {}) ⇒ Errors

Returns a new instance of Errors.



7
8
9
10
11
12
# File 'lib/simple_params/errors.rb', line 7

def initialize(base, nested_hash_errors = {}, nested_array_errors = {})
  super(base)
  @base = base
  @nested_hash_errors = symbolize_nested(nested_hash_errors)
  @nested_array_errors = symbolize_nested(nested_array_errors)
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



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

def base
  @base
end

Instance Method Details

#[](attribute) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/simple_params/errors.rb', line 14

def [](attribute)
  if is_a_nested_hash_error_attribute?(attribute)
    set(attribute.to_sym, @nested_hash_errors[attribute.to_sym])
  elsif is_a_nested_array_error_attribute?(attribute)
    set(attribute.to_sym, @nested_array_errors[attribute.to_sym])
  else
    get(attribute.to_sym) || set(attribute.to_sym, [])
  end
end

#[]=(attribute, error) ⇒ Object



24
25
26
# File 'lib/simple_params/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/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

#clearObject



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

def clear
  super
  @nested_hash_errors.map { |attribute, errors| errors.clear }
end

#empty?Boolean Also known as: blank?

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/simple_params/errors.rb', line 43

def empty?
  super &&
  @nested_hash_errors.all? { |attribute, errors| errors.empty? }
end

#full_messagesObject



66
67
68
69
70
71
72
73
74
# File 'lib/simple_params/errors.rb', line 66

def full_messages
  parent_messages = map { |attribute, message| full_message(attribute, message) }
  nested_messages = @nested_hash_errors.map do |attribute, errors|
    unless errors.full_messages.nil?
      errors.full_messages.map { |message| "#{attribute} " + message }
    end
  end
  (parent_messages + nested_messages).flatten
end

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

Returns:

  • (Boolean)


49
50
51
52
53
54
55
# File 'lib/simple_params/errors.rb', line 49

def include?(attribute)
  if is_a_nested_hash_error_attribute?(attribute)
    !@nested_hash_errors[attribute.to_sym].empty?
  else
    messages[attribute].present?
  end
end

#to_hash(full_messages = false) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/simple_params/errors.rb', line 76

def to_hash(full_messages = false)
  messages = if full_messages
    msgs = {}
    self.messages.each do |attribute, array|
      msgs[attribute] = array.map { |message| full_message(attribute, message) }
    end
    msgs
  else
    self.messages.dup
  end

  @nested_hash_errors.map do |attribute, errors|
    error_messages = nested_error_messages(attribute, full_messages)
    unless errors.empty?
      messages.merge!(attribute.to_sym => error_messages)
    end
  end
  messages
end

#to_s(full_messages = false) ⇒ Object



96
97
98
99
# File 'lib/simple_params/errors.rb', line 96

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

#valuesObject



59
60
61
62
63
64
# File 'lib/simple_params/errors.rb', line 59

def values
  messages.values +
  @nested_hash_errors.map do |attribute, errors|
    errors.values
  end
end