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_classes = {}, nested_classes_array = []) ⇒ 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_classes = {}, nested_classes_array = [])
  super(base)
  @base = base
  @nested_classes = symbolize_nested(nested_classes)
  @nested_classes_array = nested_classes_array
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
# File 'lib/simple_params/errors.rb', line 14

def [](attribute)
  if nested_attribute?(attribute)
    set_nested(attribute)
  else
    super(attribute)
  end
end

#[]=(attribute, error) ⇒ Object



22
23
24
# File 'lib/simple_params/errors.rb', line 22

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

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



26
27
28
29
30
31
32
33
34
35
# File 'lib/simple_params/errors.rb', line 26

def add(attribute, message = :invalid, options = {})
  message = message.call if message.respond_to?(:call)
  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



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

def clear
  super
  nested_instances.each { |i| i.errors.clear }
end

#empty?Boolean Also known as: blank?

Returns:

  • (Boolean)


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

def empty?
  super &&
  nested_instances.all? { |i| i.errors.empty? }
end

#full_messagesObject



63
64
65
66
67
68
69
70
# File 'lib/simple_params/errors.rb', line 63

def full_messages
  parent_messages = map { |attribute, message| full_message(attribute, message) }
  nested_messages = nested_instances.map do |i|
    i.errors.full_messages.map { |message| "#{i.parent_attribute_name} " + message }
  end

  (parent_messages + nested_messages).flatten
end

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

Returns:

  • (Boolean)


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

def include?(attribute)
  if nested_attribute?(attribute)
    !nested_class(attribute).errors.empty?
  else
    messages[attribute].present?
  end
end

#to_aObject



72
73
74
# File 'lib/simple_params/errors.rb', line 72

def to_a
  full_messages
end

#to_hash(full_messages = false) ⇒ Object



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

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

  @nested_classes.map do |attribute, klass|
    nested_msgs = run_or_mapped_run(klass) do |k|
      unless k.nil?
        get_messages(k.errors, full_messages)
      end
    end
    unless empty_messages?(nested_msgs)
      msgs.merge!(attribute.to_sym => nested_msgs)
    end
  end

  msgs
end

#to_s(full_messages = false) ⇒ Object



93
94
95
96
# File 'lib/simple_params/errors.rb', line 93

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

#valuesObject



58
59
60
61
# File 'lib/simple_params/errors.rb', line 58

def values
  messages.values +
  nested_instances.map { |i| i.errors.values }
end