Class: Graphiti::Util::SimpleErrors
- 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
-
#details ⇒ Object
readonly
Returns the value of attribute details.
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
Instance Method Summary collapse
- #[](attribute) ⇒ Object
- #add(attribute, code, message: nil, **interpolations) ⇒ Object
- #added?(attribute, code) ⇒ Boolean
- #clear ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean (also: #blank?)
- #full_message(attribute, message) ⇒ Object
- #full_messages ⇒ Object (also: #to_a)
- #full_messages_for(attribute) ⇒ Object
-
#initialize(validation_target) ⇒ SimpleErrors
constructor
A new instance of SimpleErrors.
- #keys ⇒ Object
- #size ⇒ Object (also: #count)
- #values ⇒ Object
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
#details ⇒ Object (readonly)
Returns the value of attribute details.
24 25 26 |
# File 'lib/graphiti/util/simple_errors.rb', line 24 def details @details end |
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
24 25 26 |
# File 'lib/graphiti/util/simple_errors.rb', line 24 def @messages end |
Instance Method Details
#[](attribute) ⇒ Object
38 39 40 |
# File 'lib/graphiti/util/simple_errors.rb', line 38 def [](attribute) [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} [attribute.to_sym] << translate(code, , **interpolations, attribute: attribute) end |
#added?(attribute, code) ⇒ 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 |
#clear ⇒ Object
33 34 35 36 |
# File 'lib/graphiti/util/simple_errors.rb', line 33 def clear .clear details.clear end |
#each ⇒ Object
42 43 44 45 46 |
# File 'lib/graphiti/util/simple_errors.rb', line 42 def each .each_key do |attribute| [attribute].each { |error| yield attribute, error } end end |
#empty? ⇒ Boolean Also known as: blank?
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 (attribute, ) return if attribute == :base translate(:format, DEFAULT_FORMAT, [:graphiti, :errors], attribute: attribute, message: ) end |
#full_messages ⇒ Object Also known as: to_a
77 78 79 |
# File 'lib/graphiti/util/simple_errors.rb', line 77 def map { |attribute, | (attribute, ) } end |
#full_messages_for(attribute) ⇒ Object
82 83 84 85 |
# File 'lib/graphiti/util/simple_errors.rb', line 82 def (attribute) attribute = attribute.to_sym [attribute].map { || (attribute, ) } end |
#keys ⇒ Object
57 58 59 60 61 |
# File 'lib/graphiti/util/simple_errors.rb', line 57 def keys .select { |key, value| !value.empty? }.keys end |
#size ⇒ Object Also known as: count
48 49 50 |
# File 'lib/graphiti/util/simple_errors.rb', line 48 def size values.flatten.size end |
#values ⇒ Object
53 54 55 |
# File 'lib/graphiti/util/simple_errors.rb', line 53 def values .values.reject(&:empty?) end |