Class: Graphiti::Util::SimpleErrors

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(validation_target) ⇒ SimpleErrors

Returns a new instance of SimpleErrors.



11
12
13
14
15
16
# File 'lib/graphiti/util/simple_errors.rb', line 11

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.



9
10
11
# File 'lib/graphiti/util/simple_errors.rb', line 9

def details
  @details
end

#messagesObject (readonly)

Returns the value of attribute messages.



9
10
11
# File 'lib/graphiti/util/simple_errors.rb', line 9

def messages
  @messages
end

Instance Method Details

#[](attribute) ⇒ Object



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

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

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



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

def add(attribute, code, message: nil)
  message ||= "is #{code.to_s.humanize.downcase}"

  details[attribute.to_sym] << {error: code}
  messages[attribute.to_sym] << message
end

#added?(attribute, code) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/graphiti/util/simple_errors.rb', line 60

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

#clearObject



18
19
20
21
# File 'lib/graphiti/util/simple_errors.rb', line 18

def clear
  messages.clear
  details.clear
end

#eachObject



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

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

#empty?Boolean Also known as: blank?

Returns:

  • (Boolean)


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

def empty?
  size.zero?
end

#full_message(attribute, message) ⇒ Object



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

def full_message(attribute, message)
  return message if attribute == :base
  "#{attribute} #{message}"
end

#full_messagesObject Also known as: to_a



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

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

#full_messages_for(attribute) ⇒ Object



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

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

#keysObject



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

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

#sizeObject Also known as: count



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

def size
  values.flatten.size
end

#valuesObject



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

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