Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / MooseX / Declare / Context / Namespaced.pm
1 package MooseX::Declare::Context::Namespaced;
2
3 use Moose::Role;
4
5 use Carp                  qw( croak );
6 use MooseX::Declare::Util qw( outer_stack_peek );
7
8 use namespace::clean -except => 'meta';
9
10 has namespace => (
11     is          => 'rw',
12     isa         => 'Str',
13 );
14
15 sub strip_namespace {
16     my ($self) = @_;
17
18     my $namespace = $self->strip_word;
19
20     $self->namespace($namespace)
21         if defined $namespace and length $namespace;
22
23     return $namespace;
24 }
25
26 sub qualify_namespace {
27     my ($self, $namespace) = @_;
28
29     # only qualify namespaces starting with ::
30     return $namespace
31         unless $namespace =~ /^::/;
32
33     # try to find the enclosing package
34     my $outer = outer_stack_peek($self->caller_file)
35         or croak "No outer namespace found to apply relative $namespace to";
36
37     return $outer . $namespace;
38 }
39
40 1;
41
42 __END__
43
44 =head1 NAME
45
46 MooseX::Declare::Context::Namespaced - Namespaced context
47
48 =head1 DESCRIPTION
49
50 This context trait will add namespace functionality to the context.
51
52 =head1 ATTRIBUTES
53
54 =head2 namespace
55
56 This will be set when the C<strip_namespace> method is called and the
57 namespace wasn't anonymous. It will contain the specified namespace, not
58 the fully qualified one.
59
60 =head1 METHODS
61
62 =head2 strip_namespace
63
64   Maybe[Str] Object->strip_namespace()
65
66 This method is intended to parse the main namespace of a namespaced keyword.
67 It will use L<Devel::Declare::Context::Simple>s C<strip_word> method and store
68 the result in the L</namespace> attribute if true.
69
70 =head2 qualify_namespace
71
72   Str Object->qualify_namespace(Str $namespace)
73
74 If the C<$namespace> passed it begins with a C<::>, it will be prefixed with
75 the outer namespace in the file. If there is no outer namespace, an error
76 will be thrown.
77
78 =head1 SEE ALSO
79
80 =over
81
82 =item * L<MooseX::Declare>
83
84 =item * L<MooseX::Declare::Context>
85
86 =back
87
88 =head1 AUTHOR, COPYRIGHT & LICENSE
89
90 See L<MooseX::Declare>
91
92 =cut
93