Mixin-ExtraFields-Driver-DBIC
view release on metacpan - search on metacpan
view release on metacpan or search on metacpan
lib/Mixin/ExtraFields/Driver/DBIC.pm view on Meta::CPAN
#pod
#pod package My::Schema::Object;
#pod use parent 'DBIx::Class';
#pod ...
#pod use Mixin::ExtraFields -fields => {
#pod driver => { class => 'DBIC', rs_moniker => 'ObjectExtra' }
#pod };
#pod
#pod =head1 DRIVER ARGS
#pod
#pod The following arguments may be provided when defining the driver when setting
#pod up Mixin::ExtraFields:
#pod
#pod schema - the schema for the DBIx::Class storage (see below)
#pod rs_moniker - the moniker of the result source for extras
#pod id_column - the name of the column that stores object ids
#pod name_column - the name of the column that stores extra field names
#pod value_column - the name of the column that stores extra field values
#pod
#pod C<schema> may be an actual DBIx::Class::Schema object or a coderef which, when
#pod called on an object, returns a schema. The default value assumes that objects
#pod will be DBIx::Class::Row objects, and returns their schema.
#pod
#pod =head1 SETUP ARGS
#pod
#pod When using Mixin::ExtraFields::Driver::DBIC to set up a table result source,
#pod the following values may be in the argument to C<-setup> in the import call:
#pod
#pod table - (required) the name of the table in the storage
#pod id_column - the name of the column that stores object ids
#pod name_column - the name of the column that stores extra field names
#pod value_column - the name of the column that stores extra field values
#pod
#pod =begin Pod::Coverage
#pod
#pod id_column
#pod name_column
#pod value_column
#pod
#pod =end Pod::Coverage
#pod
#pod =cut
sub from_args {
my ($class, $arg) = @_;
# schema will be either a DBIx::Class::Schema or a methodref which, called on
# an object, returns a schema
my $schema = $arg->{schema} || sub { $_[1]->result_source->schema };
my $rs_moniker = $arg->{rs_moniker}
or Carp::croak "no rs_moniker provided to $class";
my $self = {
schema => $schema,
rs_moniker => $rs_moniker,
id_column => $arg->{id_column} || 'object_id',
name_column => $arg->{name_column} || 'extra_name',
value_column => $arg->{value_column} || 'extra_value',
};
bless $self => $class;
}
sub id_column { $_[0]->{id_column} }
sub name_column { $_[0]->{name_column} }
sub value_column { $_[0]->{value_column} }
sub _rs {
my ($self, $object) = @_;
if (eval { $self->{schema}->isa('DBIx::Class::Schema') }) {
return $self->{schema}->resultset( $self->{rs_moniker} );
} else {
my $method = $self->{schema};
return $self->$method($object)->resultset($self->{rs_moniker});
}
}
sub exists_extra {
my ($self, $object, $id, $name) = @_;
my $count = $self->_rs($object)->count({
$self->id_column => $id,
$self->name_column => $name,
});
return ! ! $count;
}
sub get_extra {
my ($self, $object, $id, $name) = @_;
my $row = $self->_rs($object)->find({
$self->id_column => $id,
$self->name_column => $name,
});
return undef unless $row;
my $value_column = $self->value_column;
return $row->$value_column;
}
sub get_all_extra {
Carp::confess 'get_all_extra is fatal outside of list context'
unless wantarray;
my ($self, $object, $id, $name) = @_;
my @rows = $self->_rs($object)->search({
$self->id_column => $id,
});
return unless @rows;
my $name_column = $self->name_column;
my $value_column = $self->value_column;
my @all = map { $_->$name_column, $_->$value_column } @rows;
return @all;
view all matches for this distributionview release on metacpan - search on metacpan
( run in 1.464 second using v1.00-cache-2.02-grep-82fe00e-cpan-1925d2aa809 )