From 788dc3dc124a953fb73fe3c01714c78374298177 Mon Sep 17 00:00:00 2001 From: Juerd Waalboer Date: Thu, 28 Feb 2013 01:16:37 +0100 Subject: [PATCH] Internal metadata in cart items + smart matching for items. --- RevBank/Cart.pm | 13 +++++++++---- plugins/deposit | 6 +++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/RevBank/Cart.pm b/RevBank/Cart.pm index fa18fad..0ec6bd0 100644 --- a/RevBank/Cart.pm +++ b/RevBank/Cart.pm @@ -14,9 +14,11 @@ sub new { } sub add { - my ($self, $user, $amount, $description) = @_; + my ($self, $user, $amount, $description, $data) = @_; $user ||= '$you'; + $data ||= {}; push @{ $self->{ $user } }, { + %$data, # Internal stuff, not logged or printed. amount => $amount, description => $description, }; @@ -104,14 +106,17 @@ sub checkout { } sub select_items { - my ($self, $regex) = @_; - $regex ||= qr/(?:)/; # Match everything if no regex is given + my ($self, $key, $smartmatch) = @_; + + use v5.12; # New smartmatch semantics + + my $match_all = (@_ == 1); # Match everything if no key/match is given. my @matches; for my $user (keys %$self) { for my $item (@{ $self->{$user} }) { push @matches, { user => $user, %$item } - if $item->{description} =~ /$regex/; + if $match_all or $item->{ $key } ~~ $smartmatch; } } diff --git a/plugins/deposit b/plugins/deposit index 5e08846..ef28bca 100755 --- a/plugins/deposit +++ b/plugins/deposit @@ -11,7 +11,7 @@ sub command :Tab(deposit) { return "Amount to deposit into your account", \&amount; } - if ($cart->select_items(qr/^Deposit$/)) { + if ($cart->select_items(is_deposit => 1)) { # No other plugin recognised the input, so it must be a new user. $self->{new_user} = $command; return "Add new account for user '$command'?", \&create; @@ -26,7 +26,7 @@ sub amount { $amount = parse_amount($amount) or return REJECT, "Invalid amount"; - $cart->add(undef, +$amount, "Deposit"); + $cart->add(undef, +$amount, "Deposit", { is_deposit => 1 }); return ACCEPT; } @@ -46,7 +46,7 @@ sub create { sub hook_checkout { my ($class, $cart, $user, $transaction_id) = @_; my $sum; - $sum += $_->{amount} for $cart->select_items(qr/^Deposit$/); + $sum += $_->{amount} for $cart->select_items(is_deposit => 1); say sprintf "Don't forget to add EUR %.2f to the cash box!", $sum if $sum; }