Class: Mail::CommonAddressField

Inherits:
NamedStructuredField show all
Defined in:
lib/mail/fields/common_address_field.rb

Overview

:nodoc:

Instance Attribute Summary

Attributes inherited from CommonField

#charset, #errors, #name, #value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CommonField

#decoded, #encoded, #parse, parse, #responsible_for?, #singular?, #to_s

Constructor Details

#initialize(value = nil, charset = nil) ⇒ CommonAddressField

Returns a new instance of CommonAddressField.



21
22
23
# File 'lib/mail/fields/common_address_field.rb', line 21

def initialize(value = nil, charset = nil)
  super encode_if_needed(value, charset), charset
end

Class Method Details

.singular?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/mail/fields/common_address_field.rb', line 17

def self.singular?
  true
end

Instance Method Details

#<<(val) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/mail/fields/common_address_field.rb', line 93

def <<(val)
  case
  when val.nil?
    raise ArgumentError, "Need to pass an address to <<"
  when Utilities.blank?(val)
    self
  else
    self.value = [self.value, encode_if_needed(val)].reject { |a| Utilities.blank?(a) }.join(", ")
  end
end

#addressObject



40
41
42
# File 'lib/mail/fields/common_address_field.rb', line 40

def address
  addresses.first
end

#addressesObject

Returns the address string of all the addresses in the address list



45
46
47
48
# File 'lib/mail/fields/common_address_field.rb', line 45

def addresses
  list = element.addresses.map { |a| a.address }
  Mail::AddressContainer.new(self, list)
end

#addrsObject

Returns the actual address objects in the address list



63
64
65
66
# File 'lib/mail/fields/common_address_field.rb', line 63

def addrs
  list = element.addresses
  Mail::AddressContainer.new(self, list)
end

#decoded_group_addressesObject

Returns a list of decoded group addresses



79
80
81
# File 'lib/mail/fields/common_address_field.rb', line 79

def decoded_group_addresses
  groups.map { |k,v| v.map { |a| a.decoded } }.flatten
end

#defaultObject



36
37
38
# File 'lib/mail/fields/common_address_field.rb', line 36

def default
  addresses
end

#display_namesObject

Returns the display name of all the addresses in the address list



57
58
59
60
# File 'lib/mail/fields/common_address_field.rb', line 57

def display_names
  list = element.addresses.map { |a| a.display_name }
  Mail::AddressContainer.new(self, list)
end

#eachObject

Allows you to iterate through each address object in the address_list



30
31
32
33
34
# File 'lib/mail/fields/common_address_field.rb', line 30

def each
  element.addresses.each do |address|
    yield(address)
  end
end

#elementObject

:nodoc:



25
26
27
# File 'lib/mail/fields/common_address_field.rb', line 25

def element # :nodoc:
  @element ||= AddressList.new(value)
end

#encode_if_needed(val, val_charset = charset) ⇒ Object

:nodoc:



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/mail/fields/common_address_field.rb', line 104

def encode_if_needed(val, val_charset = charset) #:nodoc:
  case val
  when nil
    val

  # Need to join arrays of addresses into a single value
  when Array
    val.compact.map { |a| encode_if_needed a, val_charset }.join(', ')

  # Pass through UTF-8; encode non-UTF-8.
  else
    utf8_if_needed(val, val_charset) || Encodings.encode_non_usascii(val, val_charset)
  end
end

#encoded_group_addressesObject

Returns a list of encoded group addresses



84
85
86
# File 'lib/mail/fields/common_address_field.rb', line 84

def encoded_group_addresses
  groups.map { |k,v| v.map { |a| a.encoded } }.flatten
end

#formattedObject

Returns the formatted string of all the addresses in the address list



51
52
53
54
# File 'lib/mail/fields/common_address_field.rb', line 51

def formatted
  list = element.addresses.map { |a| a.format }
  Mail::AddressContainer.new(self, list)
end

#group_addressesObject

Returns the addresses that are part of groups



74
75
76
# File 'lib/mail/fields/common_address_field.rb', line 74

def group_addresses
  decoded_group_addresses
end

#group_namesObject

Returns the name of all the groups in a string



89
90
91
# File 'lib/mail/fields/common_address_field.rb', line 89

def group_names # :nodoc:
  element.group_names
end

#groupsObject

Returns a hash of group name => address strings for the address list



69
70
71
# File 'lib/mail/fields/common_address_field.rb', line 69

def groups
  element.addresses_grouped_by_group
end