Class: Rapporteur::MessageList

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/rapporteur/message_list.rb

Overview

A container of keys and one or more messages per key. This acts similarly to an ActiveModel::Errors collection from Ruby on Rails.

Instance Method Summary collapse

Constructor Details

#initialize(list_type) ⇒ MessageList

Public: Initialize a new MessageList instance.

list_type - A Symbol representing the type of this list. This Symbol is

used to determine the structure of the I18n key used when
translating the given message Symbols.
Allowed values: :messages or :errors.

Returns a MessageList instance.



25
26
27
28
# File 'lib/rapporteur/message_list.rb', line 25

def initialize(list_type)
  @list_type = list_type
  @messages = Hash.new { |hash, key| hash[key] = Set.new }
end

Instance Method Details

#add(attribute, message, i18n_options = {}) ⇒ Object

Public: Adds a new message to the list for the given attribute.

attribute - A Symbol describing the category, attribute, or name to which

the message pertains.

message - A Symbol, String, or Proc which contains the message for the

attribute.

If a String is given, it is directly used for the message.

If a Symbol is given, it is used to lookup the full message
from I18n, under the "rapporteur.<list_type>.<message Symbol>"
key. For instance, if this is a :messages list, and the
attribute is :time, then I18n would be queried for
"rapporteur.messages.time".

If a Proc is given, it is `.call`'d with no parameters. The
return value from the Proc is directly used for the message.

i18n_options - A Hash of optional key/value pairs to pass to the I18n

translator.

Examples

list.add(:time, '2013-08-23T12:34:00Z')
list.add(:time, :too_late)
list.add(:time, :too_late, :time => 'midnight')
list.add(:time, lambda { Time.now.iso8601 })

Returns the MessageList instance.



60
61
62
63
# File 'lib/rapporteur/message_list.rb', line 60

def add(attribute, message, i18n_options = {})
  @messages[attribute.to_sym].add(normalize_message(attribute, message, i18n_options))
  self
end

#full_messagesObject

Public: Generates an Array containing the combination of all of the added attributes and their messages.

Examples

list.add(:time, 'is now')
list.add(:time, 'is valuable')
list.add(:day, 'is today')
list.full_messages
# => ["time is now", "time is valuable", "day is today"]

Returns an Array containing Strings of messages.



78
79
80
81
82
# File 'lib/rapporteur/message_list.rb', line 78

def full_messages
  @messages.map { |attribute, attribute_messages|
    attribute_messages.collect { |message| "#{attribute} #{message}" }
  }.flatten
end

#to_hashObject

Public: Returns the added attributes and their messages as a Hash, keyed by the attribute, with either an Array containing all of the added messages or just the single attribute message.

Examples

list.add(:time, 'is now')
list.add(:time, 'is valuable')
list.add(:day, 'is today')
list.full_messages
# => {:time => ["is now", "is valuable"], :day => "is today"}

Returns a Hash instance.



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rapporteur/message_list.rb', line 98

def to_hash
  hash = Hash.new
  @messages.each_pair do |key, value|
    if value.size == 1
      hash[key] = value.first
    else
      hash[key] = value.to_a
    end
  end
  hash
end