Dancer2-Plugin-DBIx-Class

 view release on metacpan or  search on metacpan

lib/Dancer2/Plugin/DBIx/Class.pm  view on Meta::CPAN

package Dancer2::Plugin::DBIx::Class;
use Modern::Perl;
our $VERSION = '1.1001'; # VERSION
our $AUTHORITY = 'cpan:GEEKRUTH'; # AUTHORITY
# ABSTRACT: syntactic sugar for DBIx::Class in Dancer2, optionally with DBIx::Class::Schema::ResultSetNames
use Carp;
use Class::C3::Componentised;
use Dancer2::Plugin::DBIx::Class::ExportBuilder;
use Dancer2::Plugin 0.154000;

my $_schemas = {};

sub BUILD {
   my ($self) = @_;
   my $config = $self->config;
   my $call_rs = sub { shift->schema->resultset(@_) };
   @{ $self->keywords }{'rs'}        = $call_rs;
   @{ $self->keywords }{'rset'}      = $call_rs;
   @{ $self->keywords }{'resultset'} = $call_rs;
   @{ $self->keywords }{'schema'}    = sub { shift->schema(@_) };
   if ( defined $config->{default} ) {
      if ( !$config->{default}->{alias} ) {
         my $export_builder = Dancer2::Plugin::DBIx::Class::ExportBuilder->new(
            map { $_ => $config->{default}->{$_} }
            qw(schema_class dsn user password export_prefix) );
         my %new_keywords = $export_builder->exports;
         foreach my $dsl_keyword ( keys %{ Dancer2::Core::DSL->dsl_keywords } ) {
            delete $new_keywords{$dsl_keyword};
         }
         @{ $self->keywords }{ keys %new_keywords } = values %new_keywords;
      }
   }
   foreach my $schema ( keys %$config ) {
      next if $schema eq 'default';
      next if $config->{$schema}->{alias};
      my $export_builder = Dancer2::Plugin::DBIx::Class::ExportBuilder->new(
         map { $_ => $config->{$schema}->{$_} }
         qw(schema_class dsn user password export_prefix) );
      my %new_keywords = $export_builder->exports;
      foreach my $dsl_keyword ( keys %{ Dancer2::Core::DSL->dsl_keywords } ) {
         delete $new_keywords{$dsl_keyword};
      }
      foreach my $new_keyword ( keys %new_keywords ) {
         next if defined $self->keywords->{$new_keyword};
         $self->keywords->{$new_keyword} = $new_keywords{$new_keyword};
      }
   }
}

sub schema {
   my ( $self, $name, $schema_cfg ) = @_;

   my $cfg = $self->config;

   if ( not defined $name ) {
      my @names = keys %{$cfg}
          or croak('No schemas are configured');

      # Either pick the only one in the config or the default
      $name = @names == 1 ? $names[0] : 'default';
   }

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.552 second using v1.00-cache-2.02-grep-82fe00e-cpan-48ebf85a1963 )