File-DataClass

 view release on metacpan or  search on metacpan

lib/File/DataClass/ResultSource.pm  view on Meta::CPAN

package File::DataClass::ResultSource;

use namespace::autoclean;

use File::DataClass::Constants qw( FALSE NUL TRUE );
use File::DataClass::ResultSet;
use File::DataClass::Types     qw( ArrayRef ClassName HashRef
                                   Object SimpleStr Str );
use Moo;

# Private functions
my $_build_attributes = sub {
   my $self = shift; my $attr = {};

   $attr->{ $_ } = TRUE for (@{ $self->attributes });

   return $attr;
};

# Public attributes
has 'attributes'           => is => 'ro', isa => ArrayRef[Str],
   required                => TRUE;

has 'defaults'             => is => 'ro', isa => HashRef, builder => sub { {} };

has 'name'                 => is => 'ro', isa => SimpleStr, required => TRUE;

has 'label_attr'           => is => 'ro', isa => SimpleStr, default => NUL;

has 'resultset_attributes' => is => 'ro', isa => HashRef, builder => sub { {} };

has 'resultset_class'      => is => 'ro', isa => ClassName,
   default                 => 'File::DataClass::ResultSet';

has 'schema'               => is => 'ro', isa => Object,
   handles                 => [ 'path', 'storage' ],
   required                => TRUE, weak_ref => TRUE;

has 'types'                => is => 'ro', isa => HashRef, builder => sub { {} };

has '_attributes' => is => 'lazy', isa => HashRef,
   builder        => $_build_attributes, init_arg => undef;

# Public methods
sub columns {
   return @{ $_[ 0 ]->attributes };
}

sub has_column {
   my $key = $_[ 1 ] // '_invalid_key_';

   return exists $_[ 0 ]->_attributes->{ $key } ? TRUE : FALSE;
}

sub resultset {
   my $self = shift;

   my $attrs = { %{ $self->resultset_attributes }, result_source => $self };

   return $self->resultset_class->new( $attrs );
}

1;

__END__

=pod

=head1 Name

File::DataClass::ResultSource - A source of result sets for a given schema

=head1 Synopsis

   use File::DataClass::Schema;

   $schema = File::DataClass::Schema->new
      ( path    => [ qw(path to a file) ],
        result_source_attributes => { source_name => {}, },
        tempdir => [ qw(path to a directory) ] );

   $schema->source( q(source_name) )->attributes( [ qw(list of attr names) ] );
   $rs = $schema->resultset( q(source_name) );
   $result = $rs->find( { name => q(id of field element to find) } );
   $result->$attr_name( $some_new_value );
   $result->update;
   @result = $rs->search( { 'attr name' => q(some value) } );

=head1 Description

Provides new result sources for a given schema

Each element in a data file requires a schema definition to define it's
attributes

=head1 Configuration and Environment

Defines the following attributes



( run in 2.039 seconds using v1.01-cache-2.11-cpan-d8267643d1d )