Skip to content

Commit

Permalink
Ensure different values of prefix don't trash each other
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelhorne committed Nov 15, 2016
1 parent db32c32 commit db40c82
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Revision history for Data::Fetch

0.04
Improved handling of routines that return undef
Ensure different values of prefix don't trash each other

0.03 Tue Nov 15 10:17:45 EST 2016
Fix values call in DESTROY
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Takes two mandatory parameters:
object - the object you'll be sending the message to
message - the message you'll be sending

Takes one mandatory parameter:
Takes one optional parameter:
arg - passes this argument to the message

## get
Expand All @@ -47,13 +47,18 @@ Retrieve get a value you've primed. Takes two mandatory parameters:
object - the object you'll be sending the message to
message - the message you'll be sending

Takes one optional parameter:
arg - passes this argument to the message

# AUTHOR

Nigel Horne, `<njh at bandsman.co.uk>`

# BUGS

Can't pass more than one argument to the message
Can't pass more than one argument to the message.

I would not advise using this to call messages that change values in the object.

Changing a value between prime and get will not necessarily get you the data you want. That's the way it works
and isn't going to change.
Expand Down
20 changes: 16 additions & 4 deletions lib/Data/Fetch.pm
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Takes two mandatory parameters:
object - the object you'll be sending the message to
message - the message you'll be sending
Takes one mandatory parameter:
Takes one optional parameter:
arg - passes this argument to the message
=cut
Expand All @@ -69,8 +69,11 @@ sub prime {
return unless($args{'object'} && $args{'message'});

my $object = $args{'object'} . '->' . $args{'message'};
if($args{arg}) {
$object .= "($args{arg})"
}

if($self->{values}->{$object}) {
if($self->{values} && $self->{values}->{$object} && $self->{values}->{$object}->{status}) {
return $self;
}
# $self->{values}->{$object}->{thread} = threads->create(sub {
Expand Down Expand Up @@ -102,6 +105,9 @@ Retrieve get a value you've primed. Takes two mandatory parameters:
object - the object you'll be sending the message to
message - the message you'll be sending
Takes one optional parameter:
arg - passes this argument to the message
=cut

sub get {
Expand All @@ -111,9 +117,13 @@ sub get {
return unless($args{'object'} && $args{'message'});

my $object = $args{'object'} . '->' . $args{'message'};
if($args{arg}) {
$object .= "($args{arg})"
}

if(!defined($self->{values}->{$object}->{status})) {
die "Need to prime before getting";
my @call_details = caller(0);
die "Need to prime before getting at line ", $call_details[2], ' of ', $call_details[1];
}
if($self->{values}->{$object}->{status} eq 'complete') {
return $self->{values}->{$object}->{value};
Expand Down Expand Up @@ -151,7 +161,9 @@ Nigel Horne, C<< <njh at bandsman.co.uk> >>
=head1 BUGS
Can't pass more than one argument to the message
Can't pass more than one argument to the message.
I would not advise using this to call messages that change values in the object.
Changing a value between prime and get will not necessarily get you the data you want. That's the way it works
and isn't going to change.
Expand Down
2 changes: 1 addition & 1 deletion t/fetch.t
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ FETCH: {
$simple = Data::Value->new(3);
$fetch->prime(object => $simple, message => 'get', arg => 'prefix');
ok($fetch->get(object => $simple, message => 'get', arg => 'prefix') eq 'prefix: 3');
ok($fetch->get(object => $simple, message => 'get') eq 'prefix: 3');
ok($fetch->get(object => $simple, message => 'get', arg => 'prefix') eq 'prefix: 3');

$simple = Data::Value->new(4);
$fetch->prime(object => $simple, message => 'get', arg => 'prefix');
Expand Down

0 comments on commit db40c82

Please sign in to comment.