}
method _add_to_member_cache ($to_add) {
- return unless $self->_member_cache_built;
+ return $to_add unless $self->_member_cache_built;
push @{$self->_member_cache}, $to_add;
$to_add
}
+method _remove_from_member_cache ($to_remove) {
+ return $to_remove unless $self->_member_cache_built;
+ @{$self->_member_cache} = grep $_ ne $to_remove, @{$self->_member_cache};
+ $to_remove
+}
+
## key cache - by primary/unique key
has _key_cache => (is => 'ro', default => sub { {} });
$to_add
}
+method _remove_from_key_cache ($to_remove) {
+ # should return $to_remove
+ delete $self->_key_cache->{$self->_object_to_id($to_remove)}
+}
+
method _key_cache_has_raw ($raw) {
exists $self->_key_cache->{$self->_raw_to_id($raw)}
}
## loading data
method _new_raw_stream {
- $self->_store->new_select_command([])->execute;
+ $self->_store->new_select_command([])->execute
}
## thunking between the store representation and the set representation
method _inflate ($raw) {
bless($raw, $self->_class) if $self->_has_class;
- $raw;
+ $raw
}
method _deflate ($obj) {
method _merge ($obj, $raw) {
@{$obj}{keys %$raw} = values %$raw;
- $obj;
+ $obj
}
## methods to get ids
}
method _object_to_id ($obj) {
- $self->_raw_to_id($self->_deflate($obj));
+ $self->_raw_to_id($self->_deflate($obj))
}
+## array-ish operations - i.e. get all members
+
method flatten {
@{$self->_member_cache};
}
Data::Perl::Stream::Array->new(array => $self->_member_cache);
}
+## add to set
+
method add ($new) {
$self->_add_to_store($new);
$self->_add_to_caches($new);
- $new;
+ $new
}
method _add_to_store ($new) {
my $new_raw = $self->_deflate($new);
$self->_merge($new, $self->_store->new_insert_command($new_raw)->execute);
- $new;
+ $new
}
method _add_to_caches ($new) {
$new
}
+## remove from set
+
+method remove ($old) {
+ $self->_remove_from_store($old);
+ $self->_remove_from_caches($old);
+ $old
+}
+
+method _remove_from_store ($old) {
+ $self->_store->new_delete_command($self->_deflate($old))->execute;
+}
+
+method _remove_from_caches ($old) {
+ $self->_remove_from_member_cache($old);
+ $self->_remove_from_key_cache($old);
+ $old
+}
+
1;
raw_store => $self->raw_store,
insert_call_command => $self->raw_store->new_call_command(@_)
);
- }
+ },
+ delete_sql => q{DELETE FROM person WHERE id = ?},
+ delete_argument_order => [ 'id' ],
};
my $doug = $set->add({ name => 'Doug' });
cmp_ok(refaddr($doug), '==', refaddr($set_doug), 'Same hashref returned');
+$set->remove($doug);
+
+is_deeply([ sort_set $set->flatten ], \@expect, 'new row gone after remove');
+
+$set = make_set;
+
+is_deeply([ sort_set $set->flatten ], \@expect, 'new row still gone on reload');
+
done_testing;