Class: JSONAPIonify::Structure::Helpers::Errors
- Inherits:
-
Object
- Object
- JSONAPIonify::Structure::Helpers::Errors
- Includes:
- Enumerable
- Defined in:
- lib/jsonapionify/structure/helpers/errors.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #add(key, value) ⇒ Object (also: #[]=)
- #all_messages ⇒ Object
- #as_collection ⇒ Object
-
#initialize ⇒ Errors
constructor
A new instance of Errors.
- #initialize_copy(new_copy) ⇒ Object
- #replace(key, messages) ⇒ Object
Constructor Details
#initialize ⇒ Errors
Returns a new instance of Errors.
10 11 12 |
# File 'lib/jsonapionify/structure/helpers/errors.rb', line 10 def initialize @hash = {} end |
Instance Method Details
#[](key) ⇒ Object
29 30 31 |
# File 'lib/jsonapionify/structure/helpers/errors.rb', line 29 def [](key) @hash[key] end |
#add(key, value) ⇒ Object Also known as: []=
23 24 25 |
# File 'lib/jsonapionify/structure/helpers/errors.rb', line 23 def add(key, value) (@hash[key] ||= []) << value end |
#all_messages ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/jsonapionify/structure/helpers/errors.rb', line 37 def @hash.each_with_object([]) do |(key, errors), | errors.each do |error| << [backtick_key(key), error].join(' ') end end end |
#as_collection ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/jsonapionify/structure/helpers/errors.rb', line 45 def as_collection each_with_object(Collections::Errors.new) do |(pointer, ), collection| *, key = pointer.to_s.split('/') entity = key.present? ? key : 'object' .each do || = "#{entity} #{}".chomp('.') << '.' object = { source: { pointer: pointer }, detail: , status: "422" } collection << object end end end |
#initialize_copy(new_copy) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/jsonapionify/structure/helpers/errors.rb', line 14 def initialize_copy(new_copy) super instance_variables.each do |ivar| new_copy.instance_variable_set( ivar, instance_variable_get(ivar).dup ) rescue nil end end |
#replace(key, messages) ⇒ Object
33 34 35 |
# File 'lib/jsonapionify/structure/helpers/errors.rb', line 33 def replace(key, ) @hash[key] = Array.wrap() end |