Class: DHS::Problems::Base

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/dhs/problems/base.rb

Direct Known Subclasses

Errors, Warnings

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#codesObject (readonly)

Returns the value of attribute codes.



11
12
13
# File 'lib/dhs/problems/base.rb', line 11

def codes
  @codes
end

#messagesObject (readonly)

Returns the value of attribute messages.



11
12
13
# File 'lib/dhs/problems/base.rb', line 11

def messages
  @messages
end

#rawObject (readonly)

Returns the value of attribute raw.



11
12
13
# File 'lib/dhs/problems/base.rb', line 11

def raw
  @raw
end

#recordObject (readonly)

Returns the value of attribute record.



11
12
13
# File 'lib/dhs/problems/base.rb', line 11

def record
  @record
end

Instance Method Details

#[](attribute) ⇒ Object



35
36
37
# File 'lib/dhs/problems/base.rb', line 35

def [](attribute)
  get(attribute.to_sym) || messages[attribute] = []
end

#[]=(attribute, message) ⇒ Object



39
40
41
# File 'lib/dhs/problems/base.rb', line 39

def []=(attribute, message)
  self[attribute] << generate_message(attribute, message)
end

#add(attribute, message = :invalid, options = {}) ⇒ Object



19
20
21
22
# File 'lib/dhs/problems/base.rb', line 19

def add(attribute, message = :invalid, options = {})
  self[attribute]
  messages[attribute] << generate_message(attribute, message, options)
end

#clearObject



57
58
59
60
61
# File 'lib/dhs/problems/base.rb', line 57

def clear
  @raw = nil
  @messages.clear
  @codes.clear
end

#countObject



67
68
69
# File 'lib/dhs/problems/base.rb', line 67

def count
  to_a.size
end

#eachObject



43
44
45
46
47
48
49
50
51
# File 'lib/dhs/problems/base.rb', line 43

def each
  if messages.is_a?(Hash)
    messages.each_key do |attribute|
      self[attribute].each { |message| yield attribute, message }
    end
  elsif messages.is_a?(Array)
    messages.each { |message| yield message }
  end
end

#empty?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/dhs/problems/base.rb', line 71

def empty?
  all? { |_k, v| v&.empty? && !v.is_a?(String) }
end

#get(key) ⇒ Object



24
25
26
# File 'lib/dhs/problems/base.rb', line 24

def get(key)
  messages[key]
end

#include?(attribute) ⇒ Boolean Also known as: has_key?, key?

Returns:

  • (Boolean)


13
14
15
# File 'lib/dhs/problems/base.rb', line 13

def include?(attribute)
  messages[attribute].present?
end

#set(key, message) ⇒ Object



28
29
30
31
# File 'lib/dhs/problems/base.rb', line 28

def set(key, message)
  return if message.blank?
  messages[key] = [generate_message(key, message)]
end

#sizeObject



53
54
55
# File 'lib/dhs/problems/base.rb', line 53

def size
  values.flatten.size
end