diff --git a/lib/RevBank/Cart.pm b/lib/RevBank/Cart.pm index a154e46..fd1f19b 100644 --- a/lib/RevBank/Cart.pm +++ b/lib/RevBank/Cart.pm @@ -5,33 +5,11 @@ use List::Util (); use RevBank::Global; use RevBank::Cart::Entry; -# Some code is written with the assumption that the cart will only grow or -# be emptied. Changing existing stuff or removing items is probably not a -# good idea, and may lead to inconsistency. - sub new { my ($class) = @_; return bless { entries => [] }, $class; } -sub _call_old_hooks { - my ($self, $hook, $entry) = @_; - - my $data = $entry->{attributes}; - - for (1 .. $entry->quantity) { - for ($entry, $entry->contras) { - my $item = { - %$data, - amount => $_->{amount}, - description => $_->{description}, - }; - - RevBank::Plugins::call_hooks($hook, $self, $_->{user}, $item); - } - } -} - sub add_entry { my ($self, $entry) = @_; @@ -47,29 +25,18 @@ sub add_entry { } sub add { + # Deprecated interface: ->add($user, ...) if (defined $_[3] and not ref $_[3]) { - my ($self, $user, $amount, $description, $data) = @_; - - Carp::carp("Plugin uses deprecated old-style call to \$cart->add"); - - $data->{COMPATIBILITY} = 1; - - my $entry = RevBank::Cart::Entry->new( - defined $user ? 0 : $amount, - $description, - $data - ); - $entry->add_contra($user, $amount, $description) if defined $user; - $entry->{FORCE} = 1; - - return $self->add_entry($entry); + return shift->old_add(@_); } + # ->add($entry) if (@_ == 2) { my ($self, $entry) = @_; return $self->add_entry($entry); } + # ->add($amount, ...) my ($self, $amount, $description, $data) = @_; return $self->add_entry(RevBank::Cart::Entry->new($amount, $description, $data)); } @@ -100,13 +67,6 @@ sub display { say "$prefix$_" for map $_->as_printable, @{ $self->{entries} }; } -sub as_strings { - my ($self) = @_; - Carp::carp("Plugin uses deprecated \$cart->as_strings"); - - return map $_->as_loggable, @{ $self->{entries} }; -} - sub size { my ($self) = @_; return scalar @{ $self->{entries} }; @@ -140,25 +100,6 @@ sub checkout { sleep 1; # Ensure new timestamp/id for new transaction } -sub select_items { - my ($self, $key) = @_; - Carp::carp("Plugin uses deprecated \$cart->select_items"); - - my @matches; - for my $entry (@{ $self->{entries} }) { - my %attributes = %{ $entry->{attributes} }; - for (1 .. $entry->quantity) { - for my $item ($entry, $entry->contras) { - push @matches, { %attributes, %$item } - if @_ == 1 # No key or match given: match everything - or @_ == 2 and $entry->has_attribute($key) # Just a key - } - } - } - - return @matches; -} - sub entries { my ($self, $attribute) = @_; @@ -167,10 +108,6 @@ sub entries { return @entries; } -sub is_multi_user { - Carp::carp("\$cart->is_multi_user is no longer supported, ignoring"); -} - sub changed { my ($self) = @_; @@ -188,4 +125,73 @@ sub sum { return List::Util::sum(map $_->{amount} * $_->quantity, @{ $self->{entries} }); } + +### Old stuff, to be deleted in a future version: + +sub _call_old_hooks { + my ($self, $hook, $entry) = @_; + + my $data = $entry->{attributes}; + + for (1 .. $entry->quantity) { + for ($entry, $entry->contras) { + my $item = { + %$data, + amount => $_->{amount}, + description => $_->{description}, + }; + + RevBank::Plugins::call_hooks($hook, $self, $_->{user}, $item); + } + } +} + +sub as_strings { + my ($self) = @_; + Carp::carp("Plugin uses deprecated \$cart->as_strings"); + + return map $_->as_loggable, @{ $self->{entries} }; +} + +sub is_multi_user { + Carp::carp("\$cart->is_multi_user is no longer supported, ignoring"); +} + +sub select_items { + my ($self, $key) = @_; + Carp::carp("Plugin uses deprecated \$cart->select_items"); + + my @matches; + for my $entry (@{ $self->{entries} }) { + my %attributes = %{ $entry->{attributes} }; + for (1 .. $entry->quantity) { + for my $item ($entry, $entry->contras) { + push @matches, { %attributes, %$item } + if @_ == 1 # No key or match given: match everything + or @_ == 2 and $entry->has_attribute($key) # Just a key + } + } + } + + return @matches; +} + +sub old_add { + my ($self, $user, $amount, $description, $data) = @_; + + Carp::carp("Plugin uses deprecated old-style call to \$cart->add"); + + $data->{COMPATIBILITY} = 1; + + my $entry = RevBank::Cart::Entry->new( + defined $user ? 0 : $amount, + $description, + $data + ); + $entry->add_contra($user, $amount, $description) if defined $user; + $entry->{FORCE} = 1; + + return $self->add_entry($entry); +} + 1;