Skip to content

Commit

Permalink
first working version - we even get expected answers and have tests
Browse files Browse the repository at this point in the history
  • Loading branch information
norbu09 committed Apr 26, 2012
1 parent 99b4e26 commit 14eb65d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 8 deletions.
28 changes: 23 additions & 5 deletions lib/True/Truth.pm
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ has 'redis' => (
sub add_true_truth {
my ($self, $key, $truth) = @_;

return $self->_add($key, $truth);
return $self->_add($key, $truth) -1;
}

sub add_pending_truth {
Expand All @@ -41,11 +41,25 @@ sub add_pending_truth {
foreach my $ky (keys %$truth){
$truth->{$ky}->{_truth} = 'pending';
}
return $self->_add($key, $truth);
return $self->_add($key, $truth) -1;
}

sub update_pending_truth {
sub persist_pending_truth {
my ($self, $key, $index) = @_;

my $truth = $self->_get($key, $index);
foreach my $ky (keys %$truth){
delete $truth->{$ky}->{_truth};
}
$self->_add($key, $truth, $index);
return;
}

sub remove_pending_truth {
my ($self, $key, $index) = @_;

$self->_add($key, {}, $index);
return;
}

sub get_true_truth {
Expand Down Expand Up @@ -88,9 +102,13 @@ sub merge (@) {
#### internal stuff ####

sub _add {
my ($self, $key, $val) = @_;
my ($self, $key, $val, $index) = @_;
$self->redis->ping || $self->_connect_redis;
return $self->redis->rpush($key, encode_base64(nfreeze($val)));
if($index){
return $self->redis->lset($key, $index, encode_base64(nfreeze($val)));
} else {
return $self->redis->rpush($key, encode_base64(nfreeze($val)));
}
}

sub _get {
Expand Down
40 changes: 37 additions & 3 deletions t/truth.t
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ plan qw/no_plan/;

my $truth = True::Truth->new();

$truth->add_true_truth( $key, $a );
my $z = $truth->add_true_truth( $key, $a );
cmp_ok($z, '==', 0);
my $d = $truth->get_true_truth($key);
ok($d);
cmp_deeply(
Expand All @@ -32,7 +33,8 @@ plan qw/no_plan/;
}
);
print Dumper $d;
$truth->add_true_truth( $key, $b );
my $y = $truth->add_true_truth( $key, $b );
cmp_ok($y, '==', 1);
$d = $truth->get_true_truth($key);
ok($d);
cmp_deeply(
Expand All @@ -50,7 +52,8 @@ plan qw/no_plan/;
}
);
print Dumper $d;
$truth->add_pending_truth( $key, $c );
my $x = $truth->add_pending_truth( $key, $c );
cmp_ok($x, '==', 2);
$d = $truth->get_true_truth($key);
ok($d);
cmp_deeply(
Expand All @@ -66,4 +69,35 @@ plan qw/no_plan/;
}
);
print Dumper $d;
$truth->persist_pending_truth( $key, $x );
$d = $truth->get_true_truth($key);
ok($d);
cmp_deeply(
$d,
{
owner => 'lenz',
domain => 'norbu09.org',
dns => {
rr => { 'norbu09.org' => '1.2.3.5', type => 'A' },
},
status => 'active'
}
);
print Dumper $d;
$truth->remove_pending_truth( $key, $x );
$d = $truth->get_true_truth($key);
ok($d);
cmp_deeply(
$d,
{
owner => 'lenz',
domain => 'norbu09.org',
dns => {
rr => { 'norbu09.org' => '1.2.3.4', type => 'A' },
},
status => 'active'
}
);
print Dumper $d;

}

0 comments on commit 14eb65d

Please sign in to comment.