Class: JSONAPIonify::Structure::Helpers::Errors

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/jsonapionify/structure/helpers/errors.rb

Instance Method Summary collapse

Constructor Details

#initializeErrors

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_messagesObject



37
38
39
40
41
42
43
# File 'lib/jsonapionify/structure/helpers/errors.rb', line 37

def all_messages
  @hash.each_with_object([]) do |(key, errors), messages|
    errors.each do |error|
      messages << [backtick_key(key), error].join(' ')
    end
  end
end

#as_collectionObject



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, messages), collection|
    *, key = pointer.to_s.split('/')
    entity = key.present? ? key : 'object'
    messages.each do |message|
      message = "#{entity} #{message}".chomp('.') << '.'
      object  = {
        source: {
          pointer: pointer
        },
        detail: message,
        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, messages)
  @hash[key] = Array.wrap(messages)
end