Browse files

Implement remaining functions in ::Header, write a couple more tests.

  • Loading branch information...
1 parent 5c6ebc3 commit 758139e29daa409840d0fffb2939cc86aae13109 @retupmoca retupmoca committed Feb 13, 2012
Showing with 43 additions and 8 deletions.
  1. +0 −3 README
  2. +1 −1 lib/Email/Simple.pm6
  3. +38 −3 lib/Email/Simple/Header.pm6
  4. +4 −1 t/basic.t
View
3 README
@@ -13,7 +13,4 @@ TODO
* implement the following functions:
** Email::Simple
*** create
-** Email::Simple::Header
-*** header
-*** header-set
* Allow for user to specify their own header class
View
2 lib/Email/Simple.pm6
@@ -44,7 +44,7 @@ method header-obj-set ($obj) {
}
method header ($name) { $!header.header($name); }
-method header-set ($name, @lines) { $!header.header-set($name, |@lines); }
+method header-set ($name, *@lines) { $!header.header-set($name, |@lines); }
method header-names { $!header.header-names; }
method headers { self.header-names; }
method header-pairs { $!header.header-pairs; }
View
41 lib/Email/Simple/Header.pm6
@@ -65,19 +65,54 @@ method header-pairs {
return @!headers;
}
-method header ($name) {
+method header (Str $name) {
+ my @values = gather {
+ for @!headers {
+ if lc($_[0]) eq lc($name) {
+ take $_[1];
+ }
+ }
+ }
+ return @values but @values[0];
}
-method header-set (:$field) {
+method header-set ($field, *@values) {
+ my @indices;
+ my $x = 0;
+ for @!headers {
+ if lc($_[0]) eq lc($field) {
+ push(@indices, $x);
+ }
+ $x++;
+ }
+
+ if +@indices > +@values {
+ my $overage = +@indices - +@values;
+ for 1..$overage {
+ @!headers.splice(@indices[*-1],1);
+ @indices.pop();
+ }
+ } elsif +@values > +@indices {
+ my $underage = +@values - +@indices;
+ for 1..$underage {
+ @!headers.push([$field, '']);
+ @indices.push(+@!headers-1);
+ }
+ }
+
+ for 0..(+@indices - 1) {
+ @!headers[@indices[$_]] = [$field, @values[$_]];
+ }
+ return @values but @values[0];
}
method crlf {
return $!crlf;
}
-method !fold ($line) {
+method !fold (Str $line) {
my $limit = self!default-fold-at - 1;
if $line.chars <= $limit {
View
5 t/basic.t
@@ -9,7 +9,10 @@ my $mail-text = slurp './t/test-mails/josey-nofold';
my $mail = Email::Simple.new($mail-text);
-plan 1;
+plan 3;
my $old-from;
is $old-from = $mail.header('From'), 'Andrew Josey <ajosey@rdg.opengroup.org>', "We can get a header";
+my $sc = 'Simon Cozens <simon@cpan.org>';
+is $mail.header-set("From", $sc), $sc, "Setting returns new value";
+is $mail.header("From"), $sc, "Which is consistently returned";

0 comments on commit 758139e

Please sign in to comment.