Skip to content

Commit

Permalink
Working, Tests pass, magic
Browse files Browse the repository at this point in the history
  • Loading branch information
kentfredric committed Aug 24, 2009
1 parent b31aace commit 25aed56
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 23 deletions.
1 change: 1 addition & 0 deletions dist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ repository = http://github.com/kentfredric/MooseX-AttributeIndexes

[Prereq]
Moose = 0.89
MooseX::Types::Moose = 0
namespace::autoclean = 0.08

Test::More = 0.92
Expand Down
14 changes: 12 additions & 2 deletions lib/Moose/Meta/Attribute/Custom/Trait/Indexed.pm
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
use strict;
use warnings;
package Moose::Meta::Attribute::Custom::Trait::Indexed;

# ABSTRACT: Registration Node for the Indexed Trait.
#
# $Id:$
use strict;
use warnings;

=head1 METHODS
=head2 register_implementation
Associates the Indexed trait with MX::AI
=cut

sub register_implementation {
'MooseX::AttributeIndexes::Meta::Attribute::Trait::Indexed';
Expand Down
35 changes: 21 additions & 14 deletions lib/MooseX/AttributeIndexes.pm
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
use strict;
use warnings;

package AttributeIndexes;
package MooseX::AttributeIndexes;

# ABSTRACT: Advertise metadata about your Model-Representing Classes to Any Database tool.
use Moose ();

use Moose::Exporter;
use Moose::Util::MetaRole;
use MooseX::AttributeIndexes::Provider;
use MooseX::AttributeIndexes::Provider::FromAttributes;
use MooseX::AttributeIndexes::Meta::Attribute::Trait::Indexed;

use namespace::autoclean;

=head1 SYNOPSIS
=head2 Implementing Indexes
Expand Down Expand Up @@ -67,23 +66,31 @@ use namespace::autoclean;
=cut

Moose::Exporter->setup_import_methods;
Moose::Exporter->setup_import_methods();

=head1 METHODS
=head2 init_meta
Injects the traits for Indexed as default traits on all new attributes,
and glues the 2 magical roles into your package.
=cut

sub init_meta {
my ( $class, %options ) = @_;
Moose::Util::MetaRole::apply_metaclass_roles (
for_class => $options{'for_class'},
attribute_metaclass_roles => [qw( Indexed )],
Moose->init_meta( for_class => $options{'for_class'} )
unless $options{'for_class'}->can('meta');

Moose::Util::MetaRole::apply_metaclass_roles(
for_class => $options{'for_class'},
attribute_metaclass_roles => [ 'MooseX::AttributeIndexes::Meta::Attribute::Trait::Indexed' ],
);
Moose::Util::MetaRole::apply_base_class_roles (
Moose::Util::MetaRole::apply_base_class_roles(
for_class => $options{'for_class'},
roles => [
'MooseX::AttributeIndexes::Provider',
'MooseX::AttributeIndexes::Provider::FromAttributes',
],
)
roles => [ 'MooseX::AttributeIndexes::Provider', 'MooseX::AttributeIndexes::Provider::FromAttributes', ],
);
}


1;

20 changes: 18 additions & 2 deletions lib/MooseX/AttributeIndexes/Meta/Attribute/Trait/Indexed.pm
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
use strict;
use warnings;
package MooseX::AttributeIndexes::Meta::Attribute::Trait::Indexed;

# ABSTRACT: A Trait for attributes which permits various indexing tunables

# $Id:$
use strict;
use warnings;
use Moose::Role;
use Moose::Meta::Attribute::Custom::Trait::Indexed;
use MooseX::Types::Moose qw(:all);
use namespace::autoclean;

=head1 ATTRIBUTES
=head2 indexed
Bool. 0 = This attribute is not/cannot indexed, 1 = This Attribute is/can-be indexed.
=cut

has 'indexed' => (
is => 'ro',
isa => Bool,
required => 1,
default => 0,
);

=head2 primary_index
Bool. 0 = This attribute is not a primary index, 1 = This Attribute is a primary index.
=cut

has 'primary_index' => (
is => 'ro',
isa => Bool,
Expand Down
4 changes: 2 additions & 2 deletions lib/MooseX/AttributeIndexes/Provider.pm
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use strict;
use warnings;
package MooseX::AttributeIndexes::Provider;

# ABSTRACT: A role that advertises an object is capable of providing metadata.

# $Id:$
use strict;
use warnings;
use Moose::Role;
use namespace::autoclean;

Expand Down
39 changes: 36 additions & 3 deletions lib/MooseX/AttributeIndexes/Provider/FromAttributes.pm
Original file line number Diff line number Diff line change
@@ -1,10 +1,43 @@
use strict;
use warnings;
package MooseX::AttributeIndexes::Provider::FromAttributes;

# ABSTRACT: A Glue-on-role that provides attribute_indexes data to a class via harvesting attribute traits

# $Id:$
use strict;
use warnings;
use Moose;
use Moose::Role;
use namespace::autoclean;

=head1 METHODS
=head2 attribute_indexes
A very trivial scanner, which looks for the
C<indexed> and C<primary_index> keys and returns a hashref of
key->value pairs ( circumventing the getter )
=cut

sub attribute_indexes {

my $self = shift;
my $meta = $self->meta();

my $k = {};

my $map = $meta->get_attribute_map;
for my $attr_name ( keys %{ $map } ){
my $attr = $map->{$attr_name};

if( $attr->does( 'MooseX::AttributeIndexes::Meta::Attribute::Trait::Indexed' ) ) {
if( $attr->indexed || $attr->primary_index ){
$k->{$attr_name} = $attr->get_value($self);
}
}
}
return $k;
}
1;

2 changes: 2 additions & 0 deletions perlcriticrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[Perl::Critic::Policy::BuiltinFunctions::ProhibitStringyEval]
allow_includes = 1
22 changes: 22 additions & 0 deletions t/02-basic.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use strict;
use warnings;

use Test::More tests => 5;
use Test::Moose;

use lib 't/lib';
use Example;

meta_ok('Example');
does_ok( 'Example', 'MooseX::AttributeIndexes::Provider' );
does_ok( 'Example', 'MooseX::AttributeIndexes::Provider::FromAttributes' );

my $i = new_ok('Example',[
foo_indexed => "hello",
foo_nothing => "world",
foo_primary => "bar",
]);

use Data::Dump qw( dump );
is_deeply( $i->attribute_indexes, { 'foo_indexed' => 'hello', 'foo_primary' => 'bar' } );

29 changes: 29 additions & 0 deletions t/lib/Example.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package Example;

# $Id:$
use Moose;

use MooseX::AttributeIndexes;
use namespace::autoclean;
has 'foo_indexed' => (
isa => 'Str',
required => 1,
is => 'rw',
indexed => 1,
);

has 'foo_primary' => (
isa => 'Str',
required => 1,
is => 'rw',
primary_index => 1,
);

has 'foo_nothing' => (
isa => 'Str',
required => 1,
is => 'rw',
);

1;

5 changes: 5 additions & 0 deletions xt/author/critic.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use strict;
use warnings;

use Test::Perl::Critic( -profile => 'perlcriticrc' );
all_critic_ok();
9 changes: 9 additions & 0 deletions xt/author/kwalitee.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

use strict;
use warnings;

use Test::More;
use Test::Kwalitee;



0 comments on commit 25aed56

Please sign in to comment.