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 |
# 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?
40 41 42 43 |
# File 'lib/simple_params/errors.rb', line 40 def empty? super && nested_instances.all? { |i| i.errors.empty? } end |
#full_messages ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/simple_params/errors.rb', line 61 def = map { |attribute, | (attribute, ) } = nested_instances.map do |i| i.errors..map { || "#{i.parent_attribute_name} " + } end ( + ).flatten end |
#include?(attribute) ⇒ Boolean Also known as: has_key?, key?
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 [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( = 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
87 88 89 90 |
# File 'lib/simple_params/errors.rb', line 87 def to_s( = false) array = to_a.compact array.join(', ') end |
#values ⇒ Object
56 57 58 59 |
# File 'lib/simple_params/errors.rb', line 56 def values .values + nested_instances.map { |i| i.errors.values } end |