Skip to content

Commit

Permalink
Added =~ and .match to Body. Matches against the decoded text
Browse files Browse the repository at this point in the history
  • Loading branch information
mikel committed Nov 23, 2009
1 parent 85d4afc commit 79933ca
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
31 changes: 30 additions & 1 deletion lib/mail/body.rb
Expand Up @@ -54,10 +54,39 @@ def ==(other)
if other.class == String
self.decoded == other
else
self.eql?(other)
super
end
end

# Accepts a string and performs a regular expression against the decoded text
#
# Examples:
#
# body = Mail::Body.new('The body')
# body =~ /The/ #=> 0
#
# body = Mail::Body.new("VGhlIGJvZHk=\n")
# body.encoding = 'base64'
# body =~ /The/ #=> 0
def =~(regexp)
self.decoded =~ regexp
end

# Accepts a string and performs a regular expression against the decoded text
#
# Examples:
#
# body = Mail::Body.new('The body')
# body.match(/The/) #=> #<MatchData "The">
#
# body = Mail::Body.new("VGhlIGJvZHk=\n")
# body.encoding = 'base64'
# body.match(/The/) #=> #<MatchData "The">
def match(regexp)
self.decoded.match(regexp)
end


# Returns the raw source that the body was initialized with, without
# any tampering
def raw_source
Expand Down
23 changes: 23 additions & 0 deletions spec/mail/body_spec.rb
Expand Up @@ -215,6 +215,7 @@
end

describe "matching" do

it "should still equal itself" do
body = Mail::Body.new('The body')
body.should == body
Expand All @@ -231,6 +232,28 @@
(body == "The body").should be_true
end

it "should match on the body part decoded if given a string to =~" do
body = Mail::Body.new('The body')
(body =~ /The/).should == 0
end

it "should match on the body part decoded if given a string to ==" do
body = Mail::Body.new("VGhlIGJvZHk=\n")
body.encoding = 'base64'
(body =~ /The/).should == 0
end

it "should match on the body part decoded if given a string to match" do
body = Mail::Body.new('The body')
(body.match(/The/))[0].should == 'The'
end

it "should match on the body part decoded if given a string to match" do
body = Mail::Body.new("VGhlIGJvZHk=\n")
body.encoding = 'base64'
(body.match(/The/))[0].should == 'The'
end

end

end

0 comments on commit 79933ca

Please sign in to comment.