Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support add/add_item method chaining #141

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions lib/HTTP/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,11 @@ sub empty {
}

sub add {
if (@_ == 2) {
my $self = shift;
push(@$self, shift);
return;
}
my($self, %spec) = @_;
push(@$self, \%spec);
return;
my $self = shift;

push @$self, @_ == 1 ? @_ : { @_ };

return $self;
}

sub find2 {
Expand Down Expand Up @@ -288,6 +285,14 @@ This is just a shorthand for C<< not $conf->entries >>.
Adds a new entry to the configuration.
You can either pass separate key/value pairs or a hash reference.

Method supports chaining:

my $conf = HTTP::Config->new
->add (... spec 1 ...)
->add (... spec 2 ...)
->add (... spec 3 ...)
;

=item $conf->remove( %spec )

Removes (and returns) the entries that have matches for all the key/value pairs in %spec.
Expand Down
22 changes: 11 additions & 11 deletions t/http-config.t
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ SKIP: {
ok(!defined $res);
}

$conf = HTTP::Config->new;

$conf->add_item("always");
$conf->add_item("GET", m_method => ["GET", "HEAD"]);
$conf->add_item("POST", m_method => "POST");
$conf->add_item(".com", m_domain => ".com");
$conf->add_item("secure", m_secure => 1);
$conf->add_item("not secure", m_secure => 0);
$conf->add_item("slash", m_host_port => "www.example.com:80", m_path_prefix => "/");
$conf->add_item("u:p", m_host_port => "www.example.com:80", m_path_prefix => "/foo");
$conf->add_item("success", m_code => "2xx");
$conf = HTTP::Config->new
->add_item("always")
->add_item("GET", m_method => ["GET", "HEAD"])
->add_item("POST", m_method => "POST")
->add_item(".com", m_domain => ".com")
->add_item("secure", m_secure => 1)
->add_item("not secure", m_secure => 0)
->add_item("slash", m_host_port => "www.example.com:80", m_path_prefix => "/")
->add_item("u:p", m_host_port => "www.example.com:80", m_path_prefix => "/foo")
->add_item("success", m_code => "2xx")
;
is($conf->find(m_domain => ".com")->{item}, '.com');
my @found = $conf->find(m_domain => ".com");
is($#found, 0);
Expand Down