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.



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

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

Class Method Details

.singular?Boolean

Returns:

  • (Boolean)


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

def self.singular?
  true
end

Instance Method Details

#<<(val) ⇒ Object



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

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



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

def address
  addresses.first
end

#addressesObject

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



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

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



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

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

#decoded_group_addressesObject

Returns a list of decoded group addresses



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

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

#defaultObject



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

def default
  addresses
end

#display_namesObject

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



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

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



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

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

#elementObject

:nodoc:



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

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

#encode_if_needed(val, val_charset = charset) ⇒ Object

:nodoc:



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

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



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

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



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

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



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

def group_addresses
  decoded_group_addresses
end

#group_namesObject

Returns the name of all the groups in a string



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

def group_names # :nodoc:
  element.group_names
end

#groupsObject

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



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

def groups
  element.addresses_grouped_by_group
end