Sha256: 7d72f15a9fc051fc5380671c5c3643933c228d1f74c1159e712b507e714af055

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

module ActiveModel
  class Errors
    attr_reader :messages_by_type

    def initialize(base)
      @base = base
      @messages = {}
      @messages_by_type = {}
    end

    def add(attribute, message = :invalid, options = {})
      normalized_message = normalize_message(attribute, message, options)
      if exception = options[:strict]
        exception = ActiveModel::StrictValidationFailed if exception == true
        raise exception, full_message(attribute, message)
      end

      if message.is_a?(Symbol)
        message_by_type = {}
        message_by_type[message] = normalized_message
        self.messages_by_type[attribute] = (self.messages_by_type[attribute] || {}).merge(message_by_type)
      end
      
      self[attribute] = normalized_message
    end

    def by_type(full_messages: false)
      return messages_by_type.dup unless full_messages
      messages_by_type.each do |attribute, messages|
        messages.each do |type, message|
          messages_by_type[attribute][type] = full_message(attribute, message)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ames_by_type-0.1.0 lib/ames_by_type.rb