| 1 | package Pod::Weaver::Section::MooseConsumes; | ||
| 2 | # ABSTRACT: Add Pod::Weaver section with consumed roles (what I am implementing) based on Moose OOP framework | ||
| 3 | |||
| 4 | our $VERSION = '0.02'; | ||
| 5 | |||
| 6 | |||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | |||
| 10 | use Class::Inspector; | ||
| 11 | use Module::Load; | ||
| 12 | use Moose; | ||
| 13 | use Module::Metadata; | ||
| 14 | |||
| 15 | with 'Pod::Weaver::Role::Section'; | ||
| 16 | |||
| 17 | use aliased 'Pod::Elemental::Element::Nested'; | ||
| 18 | use aliased 'Pod::Elemental::Element::Pod5::Command'; | ||
| 19 | |||
| 20 | # This is implicit method of plugin for extending Pod::Weaver, cannot be called directly | ||
| 21 | sub weave_section { | ||
| 22 | my ( $self, $doc, $input ) = @_; | ||
| 23 | |||
| 24 | my $filename = $input->{filename}; | ||
| 25 | |||
| 26 | return if $filename !~ m{\.pm$}; | ||
| 27 | |||
| 28 | my $info = Module::Metadata->new_from_file( $filename ); | ||
| 29 | my $module = $info->name(); | ||
| 30 | |||
| 31 | return unless $module; | ||
| 32 | |||
| 33 | unless ( Class::Inspector->loaded( $module ) ) { | ||
| 34 | eval { local @INC = ( 'lib', @INC ); Module::Load::load $module }; | ||
| 35 | print "$@" if $@; #warn | ||
| 36 | } | ||
| 37 | |||
| 38 | return unless $module->can('meta'); | ||
| 39 | |||
| 40 | my @roles = sort | ||
| 41 | grep { $_ ne $module } $self->_get_roles($module); | ||
| 42 | |||
| 43 | return unless @roles; | ||
| 44 | |||
| 45 | my @pod = ( | ||
| 46 | Command->new( { | ||
| 47 | command => 'over', | ||
| 48 | content => 4 | ||
| 49 | } ), | ||
| 50 | ( | ||
| 51 | map { | ||
| 52 | Command->new( { | ||
| 53 | command => 'item', | ||
| 54 | content => "* L<$_>", | ||
| 55 | } ), | ||
| 56 | } @roles | ||
| 57 | ), | ||
| 58 | Command->new( { | ||
| 59 | command => 'back', | ||
| 60 | content => '' | ||
| 61 | } ) | ||
| 62 | ); | ||
| 63 | |||
| 64 | push @{ $doc->children }, | ||
| 65 | Nested->new( { | ||
| 66 | type => 'command', | ||
| 67 | command => 'head1', | ||
| 68 | content => 'CONSUMES', | ||
| 69 | children => \@pod | ||
| 70 | } ); | ||
| 71 | } | ||
| 72 | |||
| 73 | # Private method for extracting consumed roles through meta/superclasses | ||
| 74 | sub _get_roles { | ||
| 75 | my ( $self, $module ) = @_; | ||
| 76 | |||
| 77 | my @roles = map { $_->name } eval { $module->meta->calculate_all_roles }; | ||
| 78 | print "Possibly harmless: $@" if $@; | ||
| 79 | |||
| 80 | return @roles; | ||
| 81 | } | ||
| 82 | |||
| 83 | __PACKAGE__->meta->make_immutable; | ||
| 84 | no Moose; | ||
| 85 | 1; | ||
| 86 | |||
| 87 | __END__ |