Move old stuff to bottom of source file
This commit is contained in:
parent
89885a29c1
commit
f9554ec463
1 changed files with 73 additions and 67 deletions
|
@ -5,33 +5,11 @@ use List::Util ();
|
||||||
use RevBank::Global;
|
use RevBank::Global;
|
||||||
use RevBank::Cart::Entry;
|
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 {
|
sub new {
|
||||||
my ($class) = @_;
|
my ($class) = @_;
|
||||||
return bless { entries => [] }, $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 {
|
sub add_entry {
|
||||||
my ($self, $entry) = @_;
|
my ($self, $entry) = @_;
|
||||||
|
|
||||||
|
@ -47,29 +25,18 @@ sub add_entry {
|
||||||
}
|
}
|
||||||
|
|
||||||
sub add {
|
sub add {
|
||||||
|
# Deprecated interface: ->add($user, ...)
|
||||||
if (defined $_[3] and not ref $_[3]) {
|
if (defined $_[3] and not ref $_[3]) {
|
||||||
my ($self, $user, $amount, $description, $data) = @_;
|
return shift->old_add(@_);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# ->add($entry)
|
||||||
if (@_ == 2) {
|
if (@_ == 2) {
|
||||||
my ($self, $entry) = @_;
|
my ($self, $entry) = @_;
|
||||||
return $self->add_entry($entry);
|
return $self->add_entry($entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# ->add($amount, ...)
|
||||||
my ($self, $amount, $description, $data) = @_;
|
my ($self, $amount, $description, $data) = @_;
|
||||||
return $self->add_entry(RevBank::Cart::Entry->new($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} };
|
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 {
|
sub size {
|
||||||
my ($self) = @_;
|
my ($self) = @_;
|
||||||
return scalar @{ $self->{entries} };
|
return scalar @{ $self->{entries} };
|
||||||
|
@ -140,25 +100,6 @@ sub checkout {
|
||||||
sleep 1; # Ensure new timestamp/id for new transaction
|
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 {
|
sub entries {
|
||||||
my ($self, $attribute) = @_;
|
my ($self, $attribute) = @_;
|
||||||
|
|
||||||
|
@ -167,10 +108,6 @@ sub entries {
|
||||||
return @entries;
|
return @entries;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub is_multi_user {
|
|
||||||
Carp::carp("\$cart->is_multi_user is no longer supported, ignoring");
|
|
||||||
}
|
|
||||||
|
|
||||||
sub changed {
|
sub changed {
|
||||||
my ($self) = @_;
|
my ($self) = @_;
|
||||||
|
|
||||||
|
@ -188,4 +125,73 @@ sub sum {
|
||||||
return List::Util::sum(map $_->{amount} * $_->quantity, @{ $self->{entries} });
|
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;
|
1;
|
||||||
|
|
Loading…
Add table
Reference in a new issue