class LHS::Problems::Base

Attributes

codes[R]
messages[R]
raw[R]
record[R]

Public Instance Methods

[](attribute) click to toggle source
# File lib/lhs/problems/base.rb, line 35
def [](attribute)
  get(attribute.to_sym) || messages[attribute] = []
end
[]=(attribute, message) click to toggle source
# File lib/lhs/problems/base.rb, line 39
def []=(attribute, message)
  self[attribute] << generate_message(attribute, message)
end
add(attribute, message = :invalid, options = {}) click to toggle source
# File lib/lhs/problems/base.rb, line 19
def add(attribute, message = :invalid, options = {})
  self[attribute]
  messages[attribute] << generate_message(attribute, message, options)
end
clear() click to toggle source
# File lib/lhs/problems/base.rb, line 57
def clear
  @raw = nil
  @messages.clear
  @codes.clear
end
count() click to toggle source
# File lib/lhs/problems/base.rb, line 67
def count
  to_a.size
end
each() { |attribute, message| ... } click to toggle source
# File lib/lhs/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?() click to toggle source
# File lib/lhs/problems/base.rb, line 71
def empty?
  all? { |_k, v| v&.empty? && !v.is_a?(String) }
end
get(key) click to toggle source
# File lib/lhs/problems/base.rb, line 24
def get(key)
  messages[key]
end
has_key?(attribute)
Alias for: include?
include?(attribute) click to toggle source
# File lib/lhs/problems/base.rb, line 13
def include?(attribute)
  messages[attribute].present?
end
Also aliased as: has_key?, key?
key?(attribute)
Alias for: include?
set(key, message) click to toggle source
# File lib/lhs/problems/base.rb, line 28
def set(key, message)
  return if message.blank?
  messages[key] = [generate_message(key, message)]
end
size() click to toggle source
# File lib/lhs/problems/base.rb, line 53
def size
  values.flatten.size
end

Private Instance Methods

add_error(messages, key, value) click to toggle source
# File lib/lhs/problems/base.rb, line 77
def add_error(messages, key, value)
  key = key.to_sym
  codes[key] ||= []
  codes[key].push(value)
  messages[key] ||= []
  messages[key].push(generate_message(key, value))
end
find_translated_message(attribute, message, problem_type) click to toggle source
# File lib/lhs/problems/base.rb, line 90
def find_translated_message(attribute, message, problem_type)
  normalized_attribute = attribute.to_s.underscore.gsub(/\.[\d+\.]/, '')
  normalized_message = message.to_s.underscore
  messages = []
  messages = messages_for_record(normalized_attribute, normalized_message, problem_type) if record
  messages.concat([
    ['lhs', problem_type, 'messages', normalized_message],
    ['lhs', problem_type, 'attributes', normalized_attribute, normalized_message],
    ['lhs', problem_type, 'fallback_message']
  ]).detect do |path|
    key = path.join('.')
    return I18n.translate(key) if I18n.exists?(key)
  end
end
generate_message(attribute, message, _options = {}) click to toggle source
# File lib/lhs/problems/base.rb, line 85
def generate_message(attribute, message, _options = {})
  problem_type = self.class.name.demodulize.downcase
  find_translated_message(attribute, message, problem_type) || message
end
messages_for_record(normalized_attribute, normalized_message, problem_type) click to toggle source
# File lib/lhs/problems/base.rb, line 105
def messages_for_record(normalized_attribute, normalized_message, problem_type)
  record_name = record.model_name.name.underscore
  [
    ['lhs', problem_type, 'records', record_name, 'attributes', normalized_attribute, normalized_message],
    ['lhs', problem_type, 'records', record_name, normalized_message]
  ]
end