class SimpleParams::Errors

Attributes

base[R]

Public Class Methods

new(base, nested_classes = {}, nested_classes_array = []) click to toggle source
Calls superclass method
# File lib/simple_params/errors.rb, line 7
def initialize(base, nested_classes = {}, nested_classes_array = [])
  super(base)
  @base = base
  @nested_classes = symbolize_nested(nested_classes)
  @nested_classes_array = nested_classes_array
end

Public Instance Methods

[](attribute) click to toggle source
Calls superclass method
# File lib/simple_params/errors.rb, line 14
def [](attribute)
  if nested_attribute?(attribute)
    set_nested(attribute)
  else
    super(attribute)
  end
end
[]=(attribute, error) click to toggle source
# File lib/simple_params/errors.rb, line 22
def []=(attribute, error)
  add_error_to_attribute(attribute, error)
end
add(attribute, message = :invalid, options = {}) click to toggle source
# File lib/simple_params/errors.rb, line 26
def add(attribute, message = :invalid, options = {})
  message = message.call if message.respond_to?(:call)
  message = normalize_message(attribute, message, options)
  if exception = options[:strict]
    exception = ActiveModel::StrictValidationFailed if exception == true
    raise exception, full_message(attribute, message)
  end

  add_error_to_attribute(attribute, message)
end
blank?()
Alias for: empty?
clear() click to toggle source
Calls superclass method
# File lib/simple_params/errors.rb, line 37
def clear
  super
  nested_instances.each { |i| i.errors.clear }
end
empty?() click to toggle source
Calls superclass method
# File lib/simple_params/errors.rb, line 42
def empty?
  super &&
  nested_instances.all? { |i| i.errors.empty? }
end
Also aliased as: blank?
full_messages() click to toggle source
# File lib/simple_params/errors.rb, line 63
def full_messages
  parent_messages = map { |attribute, message| full_message(attribute, message) }
  nested_messages = nested_instances.map do |i|
    i.errors.full_messages.map { |message| "#{i.parent_attribute_name} " + message }
  end

  (parent_messages + nested_messages).flatten
end
has_key?(attribute)
Alias for: include?
include?(attribute) click to toggle source
# File lib/simple_params/errors.rb, line 48
def include?(attribute)
  if nested_attribute?(attribute)
    !nested_class(attribute).errors.empty?
  else
    messages[attribute].present?
  end
end
Also aliased as: has_key?, key?
key?(attribute)
Alias for: include?
to_a() click to toggle source
# File lib/simple_params/errors.rb, line 72
def to_a
  full_messages
end
to_hash(full_messages = false) click to toggle source
# File lib/simple_params/errors.rb, line 76
def to_hash(full_messages = false)
  msgs = get_messages(self, full_messages)

  @nested_classes.map do |attribute, klass|
    nested_msgs = run_or_mapped_run(klass) do |k|
      unless k.nil?
        get_messages(k.errors, full_messages)
      end
    end
    unless empty_messages?(nested_msgs)
      msgs.merge!(attribute.to_sym => nested_msgs)
    end
  end

  msgs
end
to_s(full_messages = false) click to toggle source
# File lib/simple_params/errors.rb, line 93
def to_s(full_messages = false)
  array = to_a.compact
  array.join(', ')
end
values() click to toggle source
# File lib/simple_params/errors.rb, line 58
def values
  messages.values +
  nested_instances.map { |i| i.errors.values }
end

Private Instance Methods

add_error_to_attribute(attribute, message) click to toggle source
# File lib/simple_params/errors.rb, line 107
def add_error_to_attribute(attribute, message)
  if nested_attribute?(attribute)
    @nested_classes[attribute].errors.add(:base, message)
  else
    self[attribute] << message
  end
end
empty_messages?(msgs) click to toggle source
# File lib/simple_params/errors.rb, line 126
def empty_messages?(msgs)
  msgs.nil? || msgs.empty? || (msgs.is_a?(Array) && msgs.all? { |m| m.nil? || m.empty? })
end
get_messages(object, full_messages = false) click to toggle source
# File lib/simple_params/errors.rb, line 115
def get_messages(object, full_messages = false)
  valid_messages = object.messages.reject { |key, val| empty_messages?(val) }
  if full_messages
    valid_messages.each_with_object({}) do |(attribute, array), messages|
      messages[attribute] = array.map { |message| object.full_message(attribute, message) }
    end
  else
    valid_messages
  end
end
nested_attribute?(attribute) click to toggle source
# File lib/simple_params/errors.rb, line 103
def nested_attribute?(attribute)
  @nested_classes.keys.include?(attribute.to_sym)
end
nested_class(key) click to toggle source
# File lib/simple_params/errors.rb, line 99
def nested_class(key)
  @nested_classes[key.to_sym]
end
nested_instances() click to toggle source
# File lib/simple_params/errors.rb, line 152
def nested_instances
  array =[]
  @nested_classes.each do |key, instance|
    if instance.is_a?(Array)
      array << instance
      array = array.flatten.compact
    else
      unless instance.nil?
        array << instance
      end
    end
  end
  array
end
run_or_mapped_run(object) { |obj| ... } click to toggle source
# File lib/simple_params/errors.rb, line 130
def run_or_mapped_run(object, &block)
  if object.is_a?(Array)
    object.map { |obj| yield obj }
  else
    yield object
  end
end
set_nested(attribute) click to toggle source
# File lib/simple_params/errors.rb, line 142
def set_nested(attribute)
  klass = nested_class(attribute)
  errors = run_or_mapped_run(klass) { |k| k.errors if k.present? }
  if respond_to?(:normalize_detail)
    detail  = normalize_detail(errors, {})
    details[attribute.to_sym] = detail
  end
  messages[attribute.to_sym] = errors
end
symbolize_nested(nested) click to toggle source
# File lib/simple_params/errors.rb, line 138
def symbolize_nested(nested)
  nested.inject({}) { |memo,(k,v) | memo[k.to_sym] = v; memo }
end