Skip to content
Snippets Groups Projects
Commit 77f376b0 authored by Leo Gordon's avatar Leo Gordon
Browse files

allow anonymous hashrefs to be cached in Cacheable/NakedTable classes

parent 88bca4bd
No related branches found
No related tags found
No related merge requests found
......@@ -29,21 +29,26 @@ sub add_new_or_update {
my $self;
if( my $unikey_keys = $class->unikey() ) {
my %all_pairs = @_;
my %other_pairs = @_;
my %unikey_pairs;
@unikey_pairs{ @$unikey_keys} = @all_pairs{ @$unikey_keys };
@unikey_pairs{ @$unikey_keys} = delete @other_pairs{ @$unikey_keys };
use Data::Dumper;
local $Data::Dumper::Indent = 0; # we want everything on one line
local $Data::Dumper::Terse = 1; # and we want it without dummy variable names
local $Data::Dumper::Maxdepth = 1;
if( $self = $class->collection()->find_one_by( %unikey_pairs ) ) {
# update the rest of the fields
warn "Updating $class (".Dumper(\%unikey_pairs).")\n";
while( my ($method, $value) = each %all_pairs ) {
unless( exists( $unikey_pairs{$method} ) ) {
$self->$method($value);
if(keys %other_pairs) {
warn "Updating $class (".Dumper(\%unikey_pairs).") with (".Dumper(\%other_pairs).")\n";
if( ref($self) eq 'HASH' ) {
@$self{ keys %other_pairs } = values %other_pairs;
} else {
while( my ($key, $value) = each %other_pairs ) {
$self->$key($value);
}
}
} else {
warn "Found a matching $class (".Dumper(\%unikey_pairs).")\n";
}
} else {
warn "Creating a new $class (".Dumper(\%unikey_pairs).")\n";
......@@ -53,7 +58,12 @@ sub add_new_or_update {
}
unless( $self ) {
$self = $class->new( @_ );
if( $class->can('new') ) {
$self = $class->new( @_ );
} else {
$self = { @_ };
}
$class->collection()->add( $self );
}
......
......@@ -44,9 +44,10 @@ sub find_one_by {
ELEMENT: foreach my $element (@{ $self->listref }) {
keys %method_to_filter_value; # sic! This is to "rewind" the each% operator to the beginning each time
while(my ($method, $filter_value) = each %method_to_filter_value) {
next ELEMENT unless( defined($element->$method()) # either both defined and equal or neither defined
? defined($filter_value) && ($element->$method() eq $filter_value)
while(my ($filter_name, $filter_value) = each %method_to_filter_value) {
my $actual_value = (ref($element) eq 'HASH') ? $element->{$filter_name} : $element->$filter_name();
next ELEMENT unless( defined($actual_value) # either both defined and equal or neither defined
? defined($filter_value) && ($actual_value eq $filter_value)
: !defined($filter_value)
);
}
......@@ -62,8 +63,12 @@ sub find_all_by {
ELEMENT: foreach my $element (@{ $self->listref }) {
keys %method_to_filter_value; # sic! This is to "rewind" the each% operator to the beginning each time
while(my ($method, $filter_value) = each %method_to_filter_value) {
next ELEMENT unless($element->$method() eq $filter_value);
while(my ($filter_name, $filter_value) = each %method_to_filter_value) {
my $actual_value = (ref($element) eq 'HASH') ? $element->{$filter_name} : $element->$filter_name();
next ELEMENT unless( defined($actual_value) # either both defined and equal or neither defined
? defined($filter_value) && ($actual_value eq $filter_value)
: !defined($filter_value)
);
}
push @filtered_elements, $element;
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment