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 = {}, nested_classes_array = []) ⇒ Errors
constructor
A new instance of Errors.
- #to_a ⇒ Object
- #to_hash(full_messages = false) ⇒ Object
- #to_s(full_messages = false) ⇒ Object
- #values ⇒ Object
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
#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
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, = :invalid, = {}) = .call if .respond_to?(:call) = (attribute, , ) if exception = [:strict] exception = ActiveModel::StrictValidationFailed if exception == true raise exception, (attribute, ) end add_error_to_attribute(attribute, ) end |
#clear ⇒ Object
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?
42 43 44 45 |
# File 'lib/simple_params/errors.rb', line 42 def empty? super && nested_instances.all? { |i| i.errors.empty? } end |
#full_messages ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/simple_params/errors.rb', line 63 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?
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 [attribute].present? end end |
#to_a ⇒ Object
72 73 74 |
# File 'lib/simple_params/errors.rb', line 72 def to_a 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( = 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
93 94 95 96 |
# File 'lib/simple_params/errors.rb', line 93 def to_s( = false) array = to_a.compact array.join(', ') end |
#values ⇒ Object
58 59 60 61 |
# File 'lib/simple_params/errors.rb', line 58 def values .values + nested_instances.map { |i| i.errors.values } end |