Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/monken/p5-moosex-dbic
Moose result class for DBIx::Class
https://github.com/monken/p5-moosex-dbic
Last synced: 3 months ago
JSON representation
Moose result class for DBIx::Class
- Host: GitHub
- URL: https://github.com/monken/p5-moosex-dbic
- Owner: monken
- Created: 2010-03-30T16:00:43.000Z (almost 15 years ago)
- Default Branch: master
- Last Pushed: 2013-03-03T12:05:49.000Z (almost 12 years ago)
- Last Synced: 2024-05-01T23:41:39.137Z (8 months ago)
- Language: Perl
- Homepage:
- Size: 1.43 MB
- Stars: 7
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.pod
Awesome Lists containing this project
README
package MooseX::DBIC;
# ABSTRACT: DBIC result class based on Moose
use Moose ();
use MooseX::DBIC::Meta::Role::Class;
use MooseX::DBIC::Types qw(ResultSet);
use Moose::Exporter;my ( undef, undef, $init ) = Moose::Exporter->build_import_methods(
with_meta => [
qw(has_column has_many belongs_to has_one might_have table remove with class_has)
],
as_is => [qw(ResultSet)],
install => [qw(import unimport)]
);sub init_meta {
my $class = shift;
my (%opts) = @_;
Moose::Util::MetaRole::apply_metaroles(
for => $opts{for_class},
class_metaroles => {
class => [ qw(MooseX::DBIC::Meta::Role::Class) ],
instance => [qw(MooseX::DBIC::Meta::Role::Instance)],
},
role_metaroles => {
role => [ qw(MooseX::DBIC::Meta::Role::Class) ],
application_to_class =>
['MooseX::DBIC::Meta::Role::Application::ToClass']
},
);
my $meta = Class::MOP::class_of( $opts{for_class} );
Moose::Util::ensure_all_roles( $meta->name, 'MooseX::DBIC::Role::Result' )
unless ( $meta->isa('Moose::Meta::Role') );
return $meta;
}sub with {
my ( $meta, $role ) = @_;
eval { Moose::with( $meta, 'MooseX::DBIC::Role::' . $role ); } or do {
die $@ if ( $@ !~ /^Can't locate/ );
Moose::with( $meta, $role );
}
}sub class_has {
my $meta = shift;
my $name = shift;
my %options = @_;my $attrs = ref $name eq 'ARRAY' ? $name : [$name];
$meta->add_class_attribute( $_, %options ) for @{$attrs};
}sub table {
shift->set_class_attribute_value( 'table_name', shift );
}sub has_column {
shift->add_column(@_);
}sub remove {
shift->remove_column(shift);
}sub has_many {
shift->add_relationship( @_, type => 'HasMany' );
}sub belongs_to {
shift->add_relationship( @_, type => 'BelongsTo' );
}sub might_have {
shift->add_relationship( @_, type => 'MightHave' );
}sub has_one {
shift->add_relationship( @_, type => 'HasOne' );
}1;
__END__
=head1 SYNOPSIS
package MySchema::CD;
use Moose;
use MooseX::DBIC;
has_column title => ( is => 'ro', isa => 'Str' );
belongs_to artist => ( is => 'ro', isa => 'MySchema::Artist' );
package MySchema::Artist;
use Moose;
use MooseX::DBIC;
has_column name => ( is => 'ro', isa => 'Str' );;
has_many cds => ( is => 'ro', isa => 'MySchema::CD' );
package MySchema;
use Moose;
extends 'MooseX::DBIC::Schema';
__PACKAGE__->load_namespaces();
package main;
my $schema = MySchema->connect( 'dbi:SQLite::memory:' );
$schema->deploy;
my $artist = $schema->resultset('Artist')->create(
{
name => 'Mo',
cds => [ { title => 'Sound of Moose' } ],
}
);
my @artists = $schema->resultset('Artist')
->order_by('name')
->prefetch('cds')
->all;=head1 PRINCIPLES
=over 4
=item B
=item B
By default, all result classes have a primary key attribute, named C. For maximum portability,
a random string is genereated instead of using an incrementing integer.=item B
Primary keys consisting of more than one column are not (yet) supported.
=back
=head1 RESULT DEFINITION
package MySchema::Artist;
use MooseX::DBIC;
# column and relationship definition
__PACKAGE__->meta->make_immutable; # speed=over 4
=item B<< table >>
table 'mytable';
B By default MooseX::DBIC will use the package name as
table name. Given the name of the schema is C, a result class C
will lead to a table named C.=item B<< has_column >>
has_column 'name';
use MooseX::Types::Email qw(EmailAddress);
has_column email => ( is => 'ro', isa => EmailAddress );Add a column to the result class. See L for
further details.
=item B<< remove >>remove 'id';
has_column mypk => ( primary_key => 1, auto_increment => 1, isa => 'Int' );Remove a previously added column. Can be used to remove the default primary key column C.
=item B<< has_many >>
has_many cds => ( is => 'ro', isa => 'MyApp::CD' );
See L.
=item B<< belongs_to >>
belongs_to producer => ( is => 'ro', isa => 'MyApp::Producer' );
See L.
=item B<< might_have >>
might_have artwork => ( is => 'ro', isa => 'MyApp::Artwork' );
See L.
=item B<< has_one >>
has_one mandatory_artwork => ( is => 'ro', isa => 'MyApp::Artwork' );
See L.
=item B<< with >>
with 'AutoUpdate';
with 'MooseX::DBIC::Role::AutoUpdate'; # same as aboveThis is the preferred way to apply roles to the class. L
has been overridden to allow for a shorter syntax.See the C namespace for more roles.
=back
=head1 INTROSPECTION
One of the big advantages that come with Moose is the ability to introspect
classes, attributes and pretty much everything. MooseX::DBIC adds methods to
the meta class to get easy access to columns, relationships and more.my $meta = MyApp::Artist->meta;
Check out L to get started.