| 1 | package Mason::Component::ClassMeta; | ||
| 2 | $Mason::Component::ClassMeta::VERSION = '2.23'; | ||
| 3 | use File::Basename; | ||
| 4 | use Mason::Moose; | ||
| 5 | use Log::Any; | ||
| 6 | |||
| 7 | my $next_id = 0; | ||
| 8 | |||
| 9 | # Passed attributes (generated in compiled component) | ||
| 10 | has 'class' => ( required => 1 ); | ||
| 11 | has 'dir_path' => ( required => 1 ); | ||
| 12 | has 'interp' => ( required => 1, weak_ref => 1 ); | ||
| 13 | has 'is_dhandler' => ( init_arg => undef, lazy_build => 1 ); | ||
| 14 | has 'is_top_level' => ( required => 1 ); | ||
| 15 | has 'object_file' => ( required => 1 ); | ||
| 16 | has 'path' => ( required => 1 ); | ||
| 17 | has 'source_file' => ( required => 1 ); | ||
| 18 | |||
| 19 | # Derived attributes | ||
| 20 | has 'id' => ( init_arg => undef, default => sub { $next_id++ } ); | ||
| 21 | has 'log' => ( init_arg => undef, lazy_build => 1 ); | ||
| 22 | has 'name' => ( init_arg => undef, lazy_build => 1 ); | ||
| 23 | |||
| 24 | method _build_is_dhandler () { | ||
| 25 | return grep { $self->name eq $_ } @{ $self->interp->dhandler_names }; | ||
| 26 | } | ||
| 27 | |||
| 28 | method _build_log () { | ||
| 29 | my $log_category = "Mason::Component" . $self->path; | ||
| 30 | $log_category =~ s/\//::/g; | ||
| 31 | return Log::Any->get_logger( category => $log_category ); | ||
| 32 | } | ||
| 33 | |||
| 34 | method _build_name () { | ||
| 35 | return basename( $self->path ); | ||
| 36 | } | ||
| 37 | |||
| 38 | __PACKAGE__->meta->make_immutable(); | ||
| 39 | |||
| 40 | 1; | ||
| 41 | |||
| 42 | __END__ | ||
| 43 | |||
| 44 | =pod | ||
| 45 | |||
| 46 | =head1 NAME | ||
| 47 | |||
| 48 | Mason::Component::ClassMeta - Meta-information about Mason component class | ||
| 49 | |||
| 50 | =head1 SYNOPSIS | ||
| 51 | |||
| 52 | # In a component: | ||
| 53 | My path is <% $.cmeta->path %> | ||
| 54 | My source file is <% $.cmeta->source_file %> | ||
| 55 | |||
| 56 | =head1 DESCRIPTION | ||
| 57 | |||
| 58 | Every L<Mason::Component|Mason::Component> class has an associated | ||
| 59 | L<Mason::Component::ClassMeta|Mason::Component::ClassMeta> object, containing | ||
| 60 | meta-information such as the component's path and source file. It can be | ||
| 61 | accessed with the L<cmeta|Mason::Component/cmeta> method. | ||
| 62 | |||
| 63 | =over | ||
| 64 | |||
| 65 | =item class | ||
| 66 | |||
| 67 | The component class that this meta object is associated with. | ||
| 68 | |||
| 69 | =item dir_path | ||
| 70 | |||
| 71 | The directory of the component path, relative to the component root - e.g. for | ||
| 72 | a component '/foo/bar', the dir_path is '/foo'. | ||
| 73 | |||
| 74 | =item is_top_level | ||
| 75 | |||
| 76 | Whether the component is considered "top level", accessible directly from C<< | ||
| 77 | $interp->run >> or a web request. See L<Mason::Interp/top_level_extensions>. | ||
| 78 | |||
| 79 | =item name | ||
| 80 | |||
| 81 | The component base name, e.g. 'bar' for component '/foo/bar'. | ||
| 82 | |||
| 83 | =item object_file | ||
| 84 | |||
| 85 | The object file produced from compiling the component. | ||
| 86 | |||
| 87 | =item path | ||
| 88 | |||
| 89 | The component path, relative to the component root - e.g. '/foo/bar'. | ||
| 90 | |||
| 91 | =item source_file | ||
| 92 | |||
| 93 | The component source file. | ||
| 94 | |||
| 95 | =back | ||
| 96 | |||
| 97 | =head1 SEE ALSO | ||
| 98 | |||
| 99 | L<Mason|Mason> | ||
| 100 | |||
| 101 | =head1 AUTHOR | ||
| 102 | |||
| 103 | Jonathan Swartz <swartz@pobox.com> | ||
| 104 | |||
| 105 | =head1 COPYRIGHT AND LICENSE | ||
| 106 | |||
| 107 | This software is copyright (c) 2012 by Jonathan Swartz. | ||
| 108 | |||
| 109 | This is free software; you can redistribute it and/or modify it under | ||
| 110 | the same terms as the Perl 5 programming language system itself. | ||
| 111 | |||
| 112 | =cut |