-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package DBIx::Class::Helper::Row::Types; | ||
|
||
# ABSTRACT: Use Types to define rows | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use Hash::Merge qw/ merge /; | ||
use Scalar::Util qw/ blessed /; | ||
use Types::SQL::Util qw/ column_info_from_type /; | ||
|
||
sub add_columns { | ||
my ( $self, @args ) = @_; | ||
|
||
my @cols = map { $self->_apply_types_to_column_defition($_) } @args; | ||
|
||
$self->next::method(@cols); | ||
} | ||
|
||
sub _apply_types_to_column_defition { | ||
my ( $self, $column_info ) = @_; | ||
|
||
return $column_info unless ref $column_info; | ||
|
||
my $type = $column_info->{isa} or return $column_info; | ||
|
||
my %info = column_info_from_type($type); | ||
|
||
return merge( $column_info, \%info ); | ||
} | ||
|
||
1; | ||
|
||
=pod | ||
=head1 SYNOPSIS | ||
In result class: | ||
use Types::SQL -types; | ||
use Types::Standard -types; | ||
__PACKAGE__->load_components('Helper::Row::Types'); | ||
__PACKAGE__->add_column( | ||
name => { | ||
isa => Maybe[ Varchar[64] ], | ||
}, | ||
); | ||
=head1 DESCRIPTION | ||
This helper allows you to specify column information by passing a | ||
L<Type::Tiny> object. | ||
Note that this I<does not> enforce that the data is of that type. It | ||
just allows you to use types as a shorthand for specifying the column | ||
info. | ||
A future version may add options to enforce types or coerce data. | ||
=head1 SEE ALSO | ||
L<Types::SQL::Util> | ||
L<Types::Standard> | ||
=cut |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!perl | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use lib 't/lib'; | ||
use Test::More; | ||
use Test::Deep; | ||
|
||
use TestSchema; | ||
my $schema = TestSchema->deploy_or_connect(); | ||
$schema->prepopulate; | ||
|
||
my $rs = $schema->resultset('Typed'); | ||
|
||
cmp_deeply $rs->result_source->column_info('id'), | ||
{ | ||
data_type => 'serial', | ||
is_auto_increment => 1, | ||
is_numeric => 1, | ||
isa => isa('Type::Tiny'), | ||
}, | ||
'id'; | ||
|
||
cmp_deeply $rs->result_source->column_info('serial_number'), { | ||
data_type => 'varchar', | ||
size => 32, | ||
is_numeric => 1, # overridden | ||
isa => isa('Type::Tiny'), | ||
}, | ||
'serial_number'; | ||
|
||
done_testing; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package TestSchema::Result::Typed; | ||
|
||
use DBIx::Class::Candy -components => [ | ||
qw( | ||
Helper::Row::Types | ||
) | ||
]; | ||
|
||
use Types::SQL -types; | ||
|
||
table 'Typed'; | ||
|
||
primary_column id => { isa => Serial }; | ||
|
||
column serial_number => { isa => Varchar [32], is_numeric => 1 }; | ||
|
||
1; |