Class: SimpleParams::Errors
- Inherits:
-
ActiveModel::Errors
- Object
- ActiveModel::Errors
- SimpleParams::Errors
- Defined in:
- lib/simple_params/errors.rb
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
Instance Method Summary collapse
- #[](attribute) ⇒ Object
- #[]=(attribute, error) ⇒ Object
- #add(attribute, message = :invalid, options = {}) ⇒ Object
- #clear ⇒ Object
- #empty? ⇒ Boolean (also: #blank?)
- #full_messages ⇒ Object
- #include?(attribute) ⇒ Boolean (also: #has_key?, #key?)
-
#initialize(base, nested_classes = {}) ⇒ Errors
constructor
A new instance of Errors.
- #to_hash(full_messages = false) ⇒ Object
- #to_s(full_messages = false) ⇒ Object
- #values ⇒ Object
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
#base ⇒ Object (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, = :invalid, = {}) = (attribute, , ) if exception = [:strict] exception = ActiveModel::StrictValidationFailed if exception == true raise exception, (attribute, ) end add_error_to_attribute(attribute, ) end |
#clear ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/simple_params/errors.rb', line 35 def clear super @nested_classes.map do |attribute, klass| run_or_mapped_run(klass) do |k| k.errors.clear end end end |
#empty? ⇒ Boolean Also known as: blank?
44 45 46 47 48 49 50 51 |
# File 'lib/simple_params/errors.rb', line 44 def empty? super && @nested_classes.all? do |attribute, klass| run_or_mapped_run(klass) do |k| k.errors.empty? end end end |
#full_messages ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/simple_params/errors.rb', line 71 def = map { |attribute, | (attribute, ) } = @nested_classes.map do |attribute, klass| run_or_mapped_run(klass) do |k| unless k.errors..nil? k.errors..map { || "#{attribute} " + } end end end ( + ).flatten end |
#include?(attribute) ⇒ Boolean Also known as: has_key?, key?
54 55 56 57 58 59 60 |
# File 'lib/simple_params/errors.rb', line 54 def include?(attribute) if nested_attribute?(attribute) !nested_class(attribute).errors.empty? else [attribute].present? end end |
#to_hash(full_messages = false) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/simple_params/errors.rb', line 83 def to_hash( = false) msgs = (self, ) @nested_classes.map do |attribute, klass| nested_msgs = run_or_mapped_run(klass) do |k| unless k.nil? (k.errors, ) end end unless (nested_msgs) msgs.merge!(attribute.to_sym => nested_msgs) end end msgs end |
#to_s(full_messages = false) ⇒ Object
99 100 101 102 |
# File 'lib/simple_params/errors.rb', line 99 def to_s( = false) array = to_a array.join(', ') end |
#values ⇒ Object
64 65 66 67 68 69 |
# File 'lib/simple_params/errors.rb', line 64 def values .values + @nested_classes.map do |key, klass| run_or_mapped_run(klass) { |k| k.errors.values } end end |