| 1 | package CatalystX::Imports::Context::Config; | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | CatalystX::Imports::Context::Config - Import Configuration Constants | ||||||||||
| 6 | |||||||||||
| 7 | =cut | ||||||||||
| 8 | |||||||||||
| 9 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | =head1 BASE CLASSES | ||||||||||
| 13 | |||||||||||
| 14 | L<CatalystX::Imports::Context> | ||||||||||
| 15 | |||||||||||
| 16 | =cut | ||||||||||
| 17 | |||||||||||
| 18 | 4 | ▶ | use base 'CatalystX::Imports::Context'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | 4 | ▶ | use Class::C3; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 4 | ▶ | use Carp::Clan qw{ ^CatalystX::Imports(?:::|$) }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | |||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||
| 24 | |||||||||||
| 25 | package MyApp::Controller::Foo; | ||||||||||
| 26 | use base 'Catalyst::Controller'; | ||||||||||
| 27 | |||||||||||
| 28 | use CatalystX::Imports Config => [qw(bar baz)]; | ||||||||||
| 29 | |||||||||||
| 30 | __PACKAGE__->config( | ||||||||||
| 31 | bar => 23, | ||||||||||
| 32 | baz => 17, | ||||||||||
| 33 | ); | ||||||||||
| 34 | |||||||||||
| 35 | sub bar_and_baz: Local { | ||||||||||
| 36 | my ($self, $c) = @_; | ||||||||||
| 37 | $c->response->body( bar + baz ); # now 40 | ||||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 1; | ||||||||||
| 41 | |||||||||||
| 42 | =head1 DESCRIPTION | ||||||||||
| 43 | |||||||||||
| 44 | This class provides exports for the L<CatalystX::Imports> module. It does | ||||||||||
| 45 | not, however, define exports in the usual sense. Instead, it will export | ||||||||||
| 46 | inline accessors to your local controller configuration. | ||||||||||
| 47 | |||||||||||
| 48 | This library does not accept any tags. If you try to pass some anyway, for | ||||||||||
| 49 | example C<:all>, an error will be raised. Naturally, this module will not | ||||||||||
| 50 | export anything by default. | ||||||||||
| 51 | |||||||||||
| 52 | =head2 Aliasing | ||||||||||
| 53 | |||||||||||
| 54 | To avoid symbol name conflicts, you can pass a hash reference with | ||||||||||
| 55 | "configuration name"/"alias to export" pairs, like this: | ||||||||||
| 56 | |||||||||||
| 57 | use CatalystX::Imports | ||||||||||
| 58 | Config => { model => 'model_name' }; | ||||||||||
| 59 | |||||||||||
| 60 | Often you won't need to alias all of them, but just a few. Fortunately, you | ||||||||||
| 61 | can mix them: | ||||||||||
| 62 | |||||||||||
| 63 | use CatalystX::Imports | ||||||||||
| 64 | Config => ['model_order', { model => 'model_name' }]; | ||||||||||
| 65 | |||||||||||
| 66 | =cut | ||||||||||
| 67 | |||||||||||
| 68 | =head1 METHODS | ||||||||||
| 69 | |||||||||||
| 70 | =head2 context_export_into | ||||||||||
| 71 | |||||||||||
| 72 | This overrides the original in L<CatalystX::Imports::Context> to raise | ||||||||||
| 73 | an error when tags are used. It also contains some internal convenience | ||||||||||
| 74 | transformations for aliasing. | ||||||||||
| 75 | |||||||||||
| 76 | =cut | ||||||||||
| 77 | |||||||||||
| 78 | 3 | ▶ | sub context_export_into { | ||||||||
|
Subroutine
context_export_into: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 79 | 3 | my ($class, $target, @exports) = @_; | |||||||||
| 80 | |||||||||||
| 81 | # individualise aliasing hash refs | ||||||||||
| 82 | @exports = | ||||||||||
| 83 | 3 | ▶ | map { if (ref $_ eq 'HASH') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 3 | my $hr = $_; | |||||||||
| 85 | 3 | map { +{ $_ => $hr->{ $_ } } } keys %$hr; | |||||||||
| 86 | } | ||||||||||
| 87 | else { | ||||||||||
| 88 | 3 | $_; | |||||||||
| 89 | } | ||||||||||
| 90 | } @exports; | ||||||||||
| 91 | |||||||||||
| 92 | # we don't accept tags | ||||||||||
| 93 | croak __PACKAGE__ . ' does not accept tag specifications' | ||||||||||
| 94 | 3 | ▶ | if grep { /^:/ } @exports; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | |||||||||||
| 96 | # export cleaned symbols | ||||||||||
| 97 | 3 | return $class->next::method($target, @exports); | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | =head2 get_export | ||||||||||
| 101 | |||||||||||
| 102 | This too overrides its original in L<CatalystX::Imports::Context>. It | ||||||||||
| 103 | will return export information for returning the configuration value. | ||||||||||
| 104 | |||||||||||
| 105 | =cut | ||||||||||
| 106 | |||||||||||
| 107 | 9 | ▶ | sub get_export { | ||||||||
|
Subroutine
get_export: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 108 | 9 | my ($class, $export) = @_; | |||||||||
| 109 | 9 | my ($config, $name); | |||||||||
| 110 | |||||||||||
| 111 | # eventual aliasing | ||||||||||
| 112 | 9 | ▶ | if (ref $export eq 'HASH') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | 6 | ($config, $name) = %$export; | |||||||||
| 114 | } | ||||||||||
| 115 | else { | ||||||||||
| 116 | 3 | ($config, $name) = ( ($export) x 2 ); | |||||||||
| 117 | } | ||||||||||
| 118 | |||||||||||
| 119 | # export with empty prototype, returning a config key from the | ||||||||||
| 120 | # controller configuration | ||||||||||
| 121 | return { | ||||||||||
| 122 | name => $name, | ||||||||||
| 123 | 3 | ▶ | code => sub { $_[1]->{ $config } }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 124 | 9 | prototype => '', | |||||||||
| 125 | }; | ||||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | =head1 DIAGNOSTICS | ||||||||||
| 129 | |||||||||||
| 130 | =head2 CatalystX::Imports::Context::Config does not accept tag specifications | ||||||||||
| 131 | |||||||||||
| 132 | You tried to import symbols by specifying a tag, like C<:all>, to the import | ||||||||||
| 133 | arguments of this library. However, this module doesn't respond to tags, and | ||||||||||
| 134 | therefore fails to accept them generally. | ||||||||||
| 135 | |||||||||||
| 136 | To solve this problem, specify the configuration values you want to export | ||||||||||
| 137 | explicitly. | ||||||||||
| 138 | |||||||||||
| 139 | =head1 SEE ALSO | ||||||||||
| 140 | |||||||||||
| 141 | L<Catalyst>, | ||||||||||
| 142 | L<CatalystX::Imports::Context>, | ||||||||||
| 143 | L<CatalystX::Imports> | ||||||||||
| 144 | |||||||||||
| 145 | =head1 AUTHOR AND COPYRIGHT | ||||||||||
| 146 | |||||||||||
| 147 | Robert 'phaylon' Sedlacek C<E<lt>rs@474.atE<gt>> | ||||||||||
| 148 | |||||||||||
| 149 | =head1 LICENSE | ||||||||||
| 150 | |||||||||||
| 151 | This program is free software; you can redistribute it and/or modify | ||||||||||
| 152 | it under the same terms as perl itself. | ||||||||||
| 153 | |||||||||||
| 154 | =cut | ||||||||||
| 155 | |||||||||||
| 156 | 4 | 1; | |||||||||