X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FTraitFor%2FModel%2FDBIC%2FSchema%2FSchemaProxy.pm;h=490671ac3fd2146e4906c6d208352bd5790fe55c;hb=f9fc703ef23fb411c1fa53bdf10c6534ba0e7d3b;hp=3fe2973cfe0c855c00cfa6686334d5859ddd9baa;hpb=ae3d05c2ca7d5dc27fe84ad7b4a3a64722748f51;p=catagits%2FCatalyst-Model-DBIC-Schema.git diff --git a/lib/Catalyst/TraitFor/Model/DBIC/Schema/SchemaProxy.pm b/lib/Catalyst/TraitFor/Model/DBIC/Schema/SchemaProxy.pm index 3fe2973..490671a 100644 --- a/lib/Catalyst/TraitFor/Model/DBIC/Schema/SchemaProxy.pm +++ b/lib/Catalyst/TraitFor/Model/DBIC/Schema/SchemaProxy.pm @@ -13,11 +13,80 @@ Options from Model =head1 DESCRIPTION Allows you to call your L methods directly on the Model -instance, and passes config options to the C attributes at C -time. +instance, and passes config options to your L and +L attributes at C time. -Methods and attributes local to your C take precedence over C -methods and attributes. +Methods and attributes local to your C take precedence over +L or L methods and attributes. + +=head1 CREATING SCHEMA CONFIG ATTRIBUTES + +To create attributes in your C, use either Moose or +L, which is inherited from by all L +classes automatically. E.g.: + + __PACKAGE__->mk_group_accessors(simple => qw/ + config_key1 + config_key2 + ... + /); + +Or with L: + + use Moose; + has config_key1 => (is => 'rw', default => 'default_value'); + +This code can be added after the md5sum on L +generated schemas. + +At app startup, any non-local options will be passed to these accessors, and can +be accessed as usual via C<< $schema->config_key1 >>. + +These config values go into your C block, along with normal config +values. + +=head1 CREATING RESULTSET CONFIG ATTRIBUTES + +You can create classdata on L classes to hold values +from L config. + +The code for this looks something like this: + + package MySchema::ResultSet::Foo; + + use base 'DBIx::Class::ResultSet'; + + __PACKAGE__->mk_group_accessors(inherited => qw/ + rs_config_key1 + rs_config_key2 + ... + /); + __PACKAGE__->rs_config_key1('default_value'); + +Or, if you prefer L: + + 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 => (is => 'rw', default => 'default_value'); + + ... + + __PACKAGE__->meta->make_immutable; + + 1; + +In your catalyst config, use the generated Model name as the config key, e.g.: + + + strict_passwords 1 + =cut @@ -43,6 +112,13 @@ after BUILD => sub { my ($self, $args) = @_; $self->_pass_options_to_schema($args); + + for my $source ($self->schema->sources) { + my $config_key = 'Model::' . $self->model_name . '::' . $source; + my $config = $self->app_class->config->{$config_key}; + next unless $config; + $self->_pass_options_to_resultset($source, $config); + } }; sub _delegates { @@ -89,11 +165,21 @@ sub _pass_options_to_schema { for my $opt (keys %$args) { if (not exists $attributes{$opt}) { next unless $self->schema->can($opt); - $self->schema->$opt($self->{$opt}); + $self->schema->$opt($args->{$opt}); } } } +sub _pass_options_to_resultset { + my ($self, $source, $args) = @_; + + for my $opt (keys %$args) { + my $rs_class = $self->schema->source($source)->resultset_class; + next unless $rs_class->can($opt); + $rs_class->$opt($args->{$opt}); + } +} + =head1 SEE ALSO L, L