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 = {}) ⇒ Errors

Returns a new instance of Errors.



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

def initialize(base, nested_classes = {})
  super(base)
  @base = base
  @nested_classes = symbolize_nested(nested_classes)
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



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

def [](attribute)
  if nested_attribute?(attribute)
    set_nested(attribute)
  else
    get(attribute.to_sym) || set(attribute.to_sym, [])
  end
end

#[]=(attribute, error) ⇒ Object



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

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

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



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

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



35
36
37
38
# File 'lib/simple_params/errors.rb', line 35

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

#empty?Boolean Also known as: blank?

Returns:

  • (Boolean)


40
41
42
43
# File 'lib/simple_params/errors.rb', line 40

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

#full_messagesObject



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

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)


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

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

#to_hash(full_messages = false) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/simple_params/errors.rb', line 70

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



87
88
89
90
# File 'lib/simple_params/errors.rb', line 87

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

#valuesObject



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

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