1 package Catalyst::TraitFor::Model::DBIC::Schema::SchemaProxy;
3 use namespace::autoclean;
5 use Carp::Clan '^Catalyst::Model::DBIC::Schema';
6 use Catalyst::Model::DBIC::Schema::Types 'Schema';
10 Catalyst::TraitFor::Model::DBIC::Schema::SchemaProxy - Proxy Schema Methods and
15 Allows you to call your L<DBIx::Class::Schema> methods directly on the Model
16 instance, and passes config options to the C<Schema> attributes at C<BUILD>
19 Methods and attributes local to your C<Model> take precedence over C<Schema>
20 methods and attributes.
25 my ($self, $args) = @_;
27 my $schema = $self->schema;
29 my $was_mutable = $self->meta->is_mutable;
31 $self->meta->make_mutable;
32 $self->meta->add_attribute('schema',
35 handles => $self->_delegates # this removes the attribute too
37 $self->meta->make_immutable unless $was_mutable;
39 $self->schema($schema) if $schema;
43 my ($self, $args) = @_;
45 $self->_pass_options_to_schema($args);
51 my $schema_meta = Class::MOP::Class->initialize($self->schema_class);
52 my @schema_methods = $schema_meta->get_all_method_names;
54 # combine with any already added by other schemas
56 @{ $self->meta->find_attribute_by_name('schema')->handles }
59 # now kill the attribute, otherwise add_attribute in BUILD will not do the right
60 # thing (it clears the handles for some reason.) May be a Moose bug.
61 eval { $self->meta->remove_attribute('schema') };
64 @schema_methods{ @schema_methods, @handles } = ();
65 @schema_methods = keys %schema_methods;
67 my @my_methods = $self->meta->get_all_method_names;
69 @my_methods{@my_methods} = ();
72 for my $method (@schema_methods) {
73 push @delegates, $method unless exists $my_methods{$method};
79 sub _pass_options_to_schema {
80 my ($self, $args) = @_;
82 my @attributes = map {
84 } $self->meta->get_all_attributes;
87 @attributes{@attributes} = ();
89 for my $opt (keys %$args) {
90 if (not exists $attributes{$opt}) {
91 next unless $self->schema->can($opt);
92 $self->schema->$opt($self->{$opt});
99 L<Catalyst::Model::DBIC::Schema>, L<DBIx::Class::Schema>
103 See L<Catalyst::Model::DBIC::Schema/AUTHOR> and
104 L<Catalyst::Model::DBIC::Schema/CONTRIBUTORS>.
108 See L<Catalyst::Model::DBIC::Schema/COPYRIGHT>.
112 This program is free software, you can redistribute it and/or modify it
113 under the same terms as Perl itself.