Skip to content

Commit

Permalink
minor improvements, especially fixing default hashguts key generation
Browse files Browse the repository at this point in the history
  • Loading branch information
rjbs committed Dec 11, 2006
1 parent cfc790d commit 9c6ba7d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 20 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,5 +1,8 @@
Changelog for Mixin-ExtraFields

0.003 2006-12-11
improve uniqueness of default HashGuts key

0.002 2006-12-10
some documentation improvements

Expand Down
8 changes: 2 additions & 6 deletions lib/Mixin/ExtraFields.pm
Expand Up @@ -11,13 +11,13 @@ Mixin::ExtraFields - add extra stashes of data to your objects
=head1 VERSION
version 0.002
version 0.003
$Id$
=cut

our $VERSION = '0.002';
our $VERSION = '0.003';

=head1 SYNOPSIS
Expand Down Expand Up @@ -417,10 +417,6 @@ available under the same terms as perl itself.
=item * handle invocants without ids (classes) and drivers that don't need ids
=item * a CGI->param-like subclass, to replace Mixin::Params, never released
=item * a Data::Hive-like subclass -- implemented, but too Listbox-specific
=back
=cut
Expand Down
39 changes: 27 additions & 12 deletions lib/Mixin/ExtraFields/Driver/HashGuts.pm
Expand Up @@ -11,13 +11,13 @@ Mixin::ExtraFields::Driver::HashGuts - store extras in a hashy object's guts
=head1 VERSION
version 0.001
version 0.003
$Id$
=cut

our $VERSION = '0.001';
our $VERSION = '0.003';

=head1 SYNOPSIS
Expand All @@ -29,8 +29,9 @@ our $VERSION = '0.001';
This driver class implements an extremely simple storage mechanism: extras are
stored on the object on which the mixed-in methods are called. By default,
they are stored in the key C<__extras>, but this can be changed by providing a
C<hash_key> argument to the driver configuration, like so:
they are stored under the key returned by the C<L</default_has_key>> method,
but this can be changed by providing a C<hash_key> argument to the driver
configuration, like so:
use Mixin::ExtraFields -fields => {
driver => { class => 'HashGuts', hash_key => "\0Something\0Wicked\0" }
Expand All @@ -49,19 +50,33 @@ This method returns the key where the driver will store its extras.
=cut

sub hash_key {
my ($self) = @_;
return $self->{hash_key};
}

=head2 default_hash_key
If no C<hash_key> argument is given for the driver, this method is called
during driver initialization. It will return a unique string to be used as the
hash key.
=cut

my $i = 0;
sub default_hash_key {
my ($self) = @_;
return "$self" . '@' . $i++;
}

sub from_args {
my ($class, $arg) = @_;

my $self = {
hash_key => $arg->{hash_key} || '__extras',
};
my $self = bless {} => $class;

bless $self => $class;
}
$self->{hash_key} = $arg->{hash_key} || $self->default_hash_key;

sub hash_key {
my ($self) = @_;
return $self->{hash_key};
return $self;
}

sub exists_extra {
Expand Down
5 changes: 4 additions & 1 deletion t/basic.t
@@ -1,7 +1,7 @@
use strict;
use warnings;

use Test::More tests => 19;
use Test::More tests => 20;

BEGIN { require_ok('Mixin::ExtraFields'); }

Expand Down Expand Up @@ -64,3 +64,6 @@ is_deeply(
[ datum => { value => 20 } ],
"get_all_detailed_misc gets the one pair that it should",
);

eval { (ref $object)->get_all_misc };
like($@, qr/couldn't determine id/, "exception thrown if called on class");
1 change: 0 additions & 1 deletion t/lib/Object/HasExtraFields.pm
Expand Up @@ -18,7 +18,6 @@ sub new {
}

sub id {
Carp::croak "not given an object" unless ref $_[0];
0 + $_[0];
}

Expand Down

0 comments on commit 9c6ba7d

Please sign in to comment.