Class: Graphiti::Util::SimpleErrors

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/graphiti/util/simple_errors.rb

Constant Summary collapse

DEFAULT_MESSAGES =

Overridable under graphiti.errors.messages.

{
  missing: "is missing",
  invalid: "must be an object",
  invalid_relationship: "is not a valid relationship",
  unwritable_relationship: "cannot be written",
  unknown_attribute: "is an unknown attribute",
  unwritable_attribute: "cannot be written",
  type_error: "should be type %{type}",
  attribute_mismatch: "does not match the server endpoint"
}.freeze
DEFAULT_FORMAT =

Joins an attribute to its message, like Rails' own errors.format.

"%{attribute} %{message}"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(validation_target) ⇒ SimpleErrors

Returns a new instance of SimpleErrors.



26
27
28
29
30
31
# File 'lib/graphiti/util/simple_errors.rb', line 26

def initialize(validation_target)
  @target = validation_target
  @messages = apply_default_array({})
  @details = apply_default_array({})
  @errors = apply_default_array({})
end

Instance Attribute Details

#detailsObject (readonly)

Returns the value of attribute details.



24
25
26
# File 'lib/graphiti/util/simple_errors.rb', line 24

def details
  @details
end

#messagesObject (readonly)

Returns the value of attribute messages.



24
25
26
# File 'lib/graphiti/util/simple_errors.rb', line 24

def messages
  @messages
end

Instance Method Details

#[](attribute) ⇒ Object



38
39
40
# File 'lib/graphiti/util/simple_errors.rb', line 38

def [](attribute)
  messages[attribute.to_sym]
end

#add(attribute, code, message: nil, **interpolations) ⇒ Object



68
69
70
71
# File 'lib/graphiti/util/simple_errors.rb', line 68

def add(attribute, code, message: nil, **interpolations)
  details[attribute.to_sym] << {error: code}
  messages[attribute.to_sym] << translate(code, message, **interpolations, attribute: attribute)
end

#added?(attribute, code) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/graphiti/util/simple_errors.rb', line 73

def added?(attribute, code)
  details[attribute.to_sym].include?({error: code})
end

#clearObject



33
34
35
36
# File 'lib/graphiti/util/simple_errors.rb', line 33

def clear
  messages.clear
  details.clear
end

#eachObject



42
43
44
45
46
# File 'lib/graphiti/util/simple_errors.rb', line 42

def each
  messages.each_key do |attribute|
    messages[attribute].each { |error| yield attribute, error }
  end
end

#empty?Boolean Also known as: blank?

Returns:

  • (Boolean)


63
64
65
# File 'lib/graphiti/util/simple_errors.rb', line 63

def empty?
  size.zero?
end

#full_message(attribute, message) ⇒ Object



87
88
89
90
91
# File 'lib/graphiti/util/simple_errors.rb', line 87

def full_message(attribute, message)
  return message if attribute == :base

  translate(:format, DEFAULT_FORMAT, [:graphiti, :errors], attribute: attribute, message: message)
end

#full_messagesObject Also known as: to_a



77
78
79
# File 'lib/graphiti/util/simple_errors.rb', line 77

def full_messages
  map { |attribute, message| full_message(attribute, message) }
end

#full_messages_for(attribute) ⇒ Object



82
83
84
85
# File 'lib/graphiti/util/simple_errors.rb', line 82

def full_messages_for(attribute)
  attribute = attribute.to_sym
  messages[attribute].map { |message| full_message(attribute, message) }
end

#keysObject



57
58
59
60
61
# File 'lib/graphiti/util/simple_errors.rb', line 57

def keys
  messages.select { |key, value|
    !value.empty?
  }.keys
end

#sizeObject Also known as: count



48
49
50
# File 'lib/graphiti/util/simple_errors.rb', line 48

def size
  values.flatten.size
end

#valuesObject



53
54
55
# File 'lib/graphiti/util/simple_errors.rb', line 53

def values
  messages.values.reject(&:empty?)
end