Skip to content

Commit

Permalink
Added ability to use << on the results returned by the various addres…
Browse files Browse the repository at this point in the history
…s fields, ie, mail.to << 'new@address' now works
  • Loading branch information
mikel committed Jan 23, 2010
1 parent 3030c3b commit 89b9672
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/mail.rb
Expand Up @@ -63,6 +63,7 @@ module Mail # :doc:
require 'mail/field_list'

# Load in all common header fields modules
require 'mail/fields/common/address_container'
require 'mail/fields/common/common_address'
require 'mail/fields/common/common_date'
require 'mail/fields/common/common_field'
Expand Down
16 changes: 16 additions & 0 deletions lib/mail/fields/common/address_container.rb
@@ -0,0 +1,16 @@
module Mail

class AddressContainer < Array

def initialize(field, list = [])
@field = field
super(list)
end

def << (address)
@field << address
end

end

end
25 changes: 20 additions & 5 deletions lib/mail/fields/common/common_address.rb
Expand Up @@ -10,7 +10,7 @@ module InstanceMethods # :doc:

def parse(val = value)
unless val.blank?
@tree ||= AddressList.new(value)
@tree = AddressList.new(val)
else
nil
end
Expand All @@ -25,22 +25,26 @@ def each

# Returns the address string of all the addresses in the address list
def addresses
tree.addresses.map { |a| a.address }
list = tree.addresses.map { |a| a.address }
Mail::AddressContainer.new(self, list)
end

# Returns the formatted string of all the addresses in the address list
def formatted
tree.addresses.map { |a| a.format }
list = tree.addresses.map { |a| a.format }
Mail::AddressContainer.new(self, list)
end

# Returns the display name of all the addresses in the address list
def display_names
tree.addresses.map { |a| a.display_name }
list = tree.addresses.map { |a| a.display_name }
Mail::AddressContainer.new(self, list)
end

# Returns the actual address objects in the address list
def addrs
tree.addresses
list = tree.addresses
Mail::AddressContainer.new(self, list)
end

# Returns a hash of group name => address strings for the address list
Expand All @@ -65,6 +69,17 @@ def group_names # :nodoc:
def default
addresses
end

def <<(val)
case
when val.nil?
raise ArgumentError, "Need to pass an address to <<"
when val.blank?
parse(encoded)
else
parse((formatted + [val]).join(", "))
end
end

private

Expand Down
18 changes: 18 additions & 0 deletions spec/mail/fields/common/address_container_spec.rb
@@ -0,0 +1,18 @@
# encoding: utf-8
require File.join(File.dirname(File.expand_path(__FILE__)), '..', '..', '..', 'spec_helper')

describe 'AddressContainer' do
it "should allow you to append an address to an address field result" do
m = Mail.new("To: mikel@test.lindsaar.net")
m.to.should == ['mikel@test.lindsaar.net']
m.to << 'bob@test.lindsaar.net'
m.to.should == ['mikel@test.lindsaar.net', 'bob@test.lindsaar.net']
end

it "should handle complex addresses correctly" do
m = Mail.new("From: mikel@test.lindsaar.net")
m.from.should == ['mikel@test.lindsaar.net']
m.from << '"Ada Lindsaar" <ada@test.lindsaar.net>, bob@test.lindsaar.net'
m.from.should == ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net', 'bob@test.lindsaar.net']
end
end
6 changes: 6 additions & 0 deletions spec/mail/fields/common/common_address_spec.rb
Expand Up @@ -68,6 +68,12 @@
field.encoded.should == "To: mikel@test.lindsaar.net\r\n"
end

it "should allow you to append an address" do
field = Mail::ToField.new("To", "")
field << 'mikel@test.lindsaar.net'
field.addresses.should == ["mikel@test.lindsaar.net"]
end

end

describe "encoding and decoding fields" do
Expand Down

0 comments on commit 89b9672

Please sign in to comment.