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_hash_errors = {}, nested_array_errors = {}) ⇒ 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_hash_errors = {}, nested_array_errors = {}) ⇒ 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_hash_errors = {}, nested_array_errors = {}) super(base) @base = base @nested_hash_errors = symbolize_nested(nested_hash_errors) @nested_array_errors = symbolize_nested(nested_array_errors) 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 21 22 |
# File 'lib/simple_params/errors.rb', line 14 def [](attribute) if is_a_nested_hash_error_attribute?(attribute) set(attribute.to_sym, @nested_hash_errors[attribute.to_sym]) elsif is_a_nested_array_error_attribute?(attribute) set(attribute.to_sym, @nested_array_errors[attribute.to_sym]) else get(attribute.to_sym) || set(attribute.to_sym, []) end end |
#[]=(attribute, error) ⇒ Object
24 25 26 |
# File 'lib/simple_params/errors.rb', line 24 def []=(attribute, error) add_error_to_attribute(attribute, error) end |
#add(attribute, message = :invalid, options = {}) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/simple_params/errors.rb', line 28 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
38 39 40 41 |
# File 'lib/simple_params/errors.rb', line 38 def clear super @nested_hash_errors.map { |attribute, errors| errors.clear } end |
#empty? ⇒ Boolean Also known as: blank?
43 44 45 46 |
# File 'lib/simple_params/errors.rb', line 43 def empty? super && @nested_hash_errors.all? { |attribute, errors| errors.empty? } end |
#full_messages ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/simple_params/errors.rb', line 66 def = map { |attribute, | (attribute, ) } = @nested_hash_errors.map do |attribute, errors| unless errors..nil? errors..map { || "#{attribute} " + } end end ( + ).flatten end |
#include?(attribute) ⇒ Boolean Also known as: has_key?, key?
49 50 51 52 53 54 55 |
# File 'lib/simple_params/errors.rb', line 49 def include?(attribute) if is_a_nested_hash_error_attribute?(attribute) !@nested_hash_errors[attribute.to_sym].empty? else [attribute].present? end end |
#to_hash(full_messages = false) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/simple_params/errors.rb', line 76 def to_hash( = false) = if msgs = {} self..each do |attribute, array| msgs[attribute] = array.map { || (attribute, ) } end msgs else self..dup end @nested_hash_errors.map do |attribute, errors| = (attribute, ) unless errors.empty? .merge!(attribute.to_sym => ) end end end |
#to_s(full_messages = false) ⇒ Object
96 97 98 99 |
# File 'lib/simple_params/errors.rb', line 96 def to_s( = false) array = to_a array.join(', ') end |
#values ⇒ Object
59 60 61 62 63 64 |
# File 'lib/simple_params/errors.rb', line 59 def values .values + @nested_hash_errors.map do |attribute, errors| errors.values end end |