<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Catalyst::TraitFor::Model::DBIC::Schema::SchemaProxy;

use namespace::autoclean;
use Moose::Role;
use Carp::Clan '^Catalyst::Model::DBIC::Schema';
use Catalyst::Model::DBIC::Schema::Types 'Schema';

=head1 NAME

Catalyst::TraitFor::Model::DBIC::Schema::SchemaProxy - Proxy Schema Methods and
Options from Model

=head1 DESCRIPTION

Allows you to call your L&lt;DBIx::Class::Schema&gt; methods directly on the Model
instance, and passes config options to your L&lt;DBIx::Class::Schema&gt; and
L&lt;DBIx::Class::ResultSet&gt; attributes at C&lt;BUILD&gt; time.

Methods and attributes local to your C&lt;Model&gt; take precedence over
L&lt;DBIx::Class::Schema&gt; or L&lt;DBIx::Class::ResultSet&gt; methods and attributes.

=head1 CREATING SCHEMA CONFIG ATTRIBUTES

To create attributes in your C&lt;Schema.pm&gt;, use either Moose or
L&lt;Class::Accessor::Grouped&gt;, which is inherited from by all L&lt;DBIx::Class&gt;
classes automatically. E.g.:

    __PACKAGE__-&gt;mk_group_accessors(simple =&gt; qw/
        config_key1
        config_key2
        ...
    /);

Or with L&lt;Moose&gt;:

    use Moose;
    has config_key1 =&gt; (is =&gt; 'rw', default =&gt; 'default_value');

This code can be added after the md5sum on L&lt;DBIx::Class::Schema::Loader&gt;
generated schemas.

At app startup, any non-local options will be passed to these accessors, and can
be accessed as usual via C&lt;&lt; $schema-&gt;config_key1 &gt;&gt;.

These config values go into your C&lt;Model::DB&gt; block, along with normal config
values.

=head1 CREATING RESULTSET CONFIG ATTRIBUTES

You can create classdata on L&lt;DBIx::Class::ResultSet&gt; classes to hold values
from L&lt;Catalyst&gt; config.

The code for this looks something like this:

    package MySchema::ResultSet::Foo;

    use base 'DBIx::Class::ResultSet';

    __PACKAGE__-&gt;mk_group_accessors(inherited =&gt; qw/
        rs_config_key1
        rs_config_key2
        ...
    /);
    __PACKAGE__-&gt;rs_config_key1('default_value');

Or, if you prefer L&lt;Moose&gt;:

    package MySchema::ResultSet::Foo;

    use Moose;
    use MooseX::NonMoose;
    use MooseX::ClassAttribute;
    extends 'DBIx::Class::ResultSet';

    sub BUILDARGS { $_[2] } # important

    class_has rs_config_key1 =&gt; (is =&gt; 'rw', default =&gt; 'default_value');

    ...

    __PACKAGE__-&gt;meta-&gt;make_immutable;

    1;

In your catalyst config, use the generated Model name as the config key, e.g.:

    &lt;Model::DB::Users&gt;
        strict_passwords 1
    &lt;/Model::DB::Users&gt;

=cut

after setup =&gt; sub {
    my ($self, $args) = @_;

    my $schema = $self-&gt;schema;

    my $was_mutable = $self-&gt;meta-&gt;is_mutable;

    $self-&gt;meta-&gt;make_mutable;
    $self-&gt;meta-&gt;add_attribute('schema',
        is =&gt; 'rw',
        isa =&gt; Schema,
        handles =&gt; $self-&gt;_delegates # this removes the attribute too
    );
    $self-&gt;meta-&gt;make_immutable unless $was_mutable;

    $self-&gt;schema($schema) if $schema;
};

after BUILD =&gt; sub {
    my ($self, $args) = @_;

    $self-&gt;_pass_options_to_schema($args);

    for my $source ($self-&gt;schema-&gt;sources) {
        my $config_key = 'Model::' . $self-&gt;model_name . '::' . $source;
        my $config = $self-&gt;app_class-&gt;config-&gt;{$config_key};
        next unless $config;
        $self-&gt;_pass_options_to_resultset($source, $config);
    }
};

sub _delegates {
    my $self = shift;

    my $schema_meta = Class::MOP::Class-&gt;initialize($self-&gt;schema_class);
    my @schema_methods = $schema_meta-&gt;get_all_method_names;

# combine with any already added by other schemas
    my @handles = eval {
        @{ $self-&gt;meta-&gt;find_attribute_by_name('schema')-&gt;handles }
    };

# now kill the attribute, otherwise add_attribute in BUILD will not do the right
# thing (it clears the handles for some reason.) May be a Moose bug.
    eval { $self-&gt;meta-&gt;remove_attribute('schema') };

    my %schema_methods;
    @schema_methods{ @schema_methods, @handles } = ();
    @schema_methods = keys %schema_methods;

    my @my_methods = $self-&gt;meta-&gt;get_all_method_names;
    my %my_methods;
    @my_methods{@my_methods} = ();

    my @delegates;
    for my $method (@schema_methods) {
        push @delegates, $method unless exists $my_methods{$method};
    }

    return \@delegates;
}

sub _pass_options_to_schema {
    my ($self, $args) = @_;

    my @attributes = map {
        $_-&gt;init_arg || ()
    } $self-&gt;meta-&gt;get_all_attributes;

    my %attributes;
    @attributes{@attributes} = ();

    for my $opt (keys %$args) {
        if (not exists $attributes{$opt}) {
            next unless $self-&gt;schema-&gt;can($opt);
            $self-&gt;schema-&gt;$opt($args-&gt;{$opt});
        }
    }
}

sub _pass_options_to_resultset {
    my ($self, $source, $args) = @_;

    for my $opt (keys %$args) {
        my $rs_class = $self-&gt;schema-&gt;source($source)-&gt;resultset_class;
        next unless $rs_class-&gt;can($opt);
        $rs_class-&gt;$opt($args-&gt;{$opt});
    }
}

=head1 SEE ALSO

L&lt;Catalyst::Model::DBIC::Schema&gt;, L&lt;DBIx::Class::Schema&gt;

=head1 AUTHOR

See L&lt;Catalyst::Model::DBIC::Schema/AUTHOR&gt; and
L&lt;Catalyst::Model::DBIC::Schema/CONTRIBUTORS&gt;.

=head1 COPYRIGHT

See L&lt;Catalyst::Model::DBIC::Schema/COPYRIGHT&gt;.

=head1 LICENSE

This program is free software, you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut

1;
</pre></body></html>