# lib/Dist/Zilla/Plugin/MetaResources/Template.pm #


# --------------------------------------------------------------------------------------------------



# --------------------------------------------------------------------------------------------------


# --------------------------------------------------------------------------------------------------

package Dist::Zilla::Plugin::MetaResources::Template;

use Moose;
use namespace::autoclean;

# ABSTRACT: Use Perl code in distribution "resource" metadata
our $VERSION = '0.001'; # VERSION

extends 'Dist::Zilla::Plugin::MetaResources';
with 'Dist::Zilla::Role::TextTemplate';

# --------------------------------------------------------------------------------------------------


sub BUILD {
    my ( $self ) = @_;
    $self->{ resources } = $self->_expand( $self->{ resources } );
};

# --------------------------------------------------------------------------------------------------

sub _expand {
    my ( $self, $item ) = @_;
    if ( 0 ) {
    } elsif ( ref( $item ) eq '' ) {
        $item = $self->fill_in_string( $item, { plugin => \$self, dist => \($self->zilla) } );
    } elsif ( ref( $item ) eq 'ARRAY' ) {
        for my $elem ( @$item ) {
            $elem = $self->_expand( $elem );
        };
    } elsif ( ref( $item ) eq 'HASH' ) {
        for my $key ( keys( %$item ) ) {
            $item->{ $key } = $self->_expand( $item->{ $key } );
        };
    } else {
        die "Unexpected ref type: " . ref( $item );
    };
    return $item;
};

# --------------------------------------------------------------------------------------------------

__PACKAGE__->meta->make_immutable;

1;

# end of file #

__END__

=pod

=encoding UTF-8

=head1 NAME

Dist::Zilla::Plugin::MetaResources::Template - Use Perl code in distribution "resource" metadata

=head1 VERSION

Version 0.001, released on 2015-07-09 18:08 UTC.

This is C<Dist::Zilla::Plugin::MetaResources::Template> module documentation. Read this if you are going to hack or
extend C<Dist-Zilla-Plugin-MetaResources-Template>.

If you want to use Perl code in distribution "resource" metadata, read the L<manual|Dist::Zilla::Plugin::MetaResources::Template::Manual>. General
topics like getting source, building, installing, bug reporting and some others are covered in the
L<readme|Dist::Zilla::Plugin::MetaResources::Template::ReadMe>.

=head1 SYNOPSIS

Oops.

=head1 DESCRIPTION

C<Dist::Zilla::Plugin::MetaResources::Template> extends C<Dist::Zilla::Plugin::MetaResources>. The class implements C<BUILD>
method, which expand templates in option values, all other work is done by the parent class.

Template processing abilities achieved by consuming C<Dist::Zilla::Role::TextTemplate> role.

=head1 INSTANCE METHODS

=head2 BUILD

The method is automatically called after object creation. The method recursively walks through the
C<< $self->{ resources } >> and expand templates in string by calling C<< $self->fill_in_string >>
(it is a method from C<Dist::Zilla::Role::TextTemplate> role).

Defining C<BUILDARGS> seems like a simpler approach because all the options are in plain list and
so there is no need in recursive walking trough a complex data structure (C<< $self->{ resources }
>>), but we need C<$self> to perform template expansion while C<BUILDARGS> is a class method.

=head1 SEE ALSO

=over 4

=item L<Dist::Zilla>

=item L<Dist::Zilla::Role>

=item L<Dist::Zilla::Role::Plugin>

=item L<Dist::Zilla::Role::TextTemplate>

=item L<Dist::Zilla::Plugin::MetaResources>

=item L<Dist::Zilla::Plugin::MetaResources::Template::Manual>

=item L<Dist::Zilla::Plugin::MetaResources::Template::ReadMe>

=item L<CPAN::Meta::Spec>

=item L<CPAN::Meta::Spec/"resources">

=item L<Moose::Manual::Construction/"BUILD">

=back

=head1 AUTHOR

Van de Bugger <van.de.bugger@gmail.com>

=head1 COPYRIGHT AND LICENSE

Copyright © 2015 Van de Bugger

This file is part of perl-Dist-Zilla-Plugin-MetaResources-Template.

perl-Dist-Zilla-Plugin-MetaResources-Template is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.

perl-Dist-Zilla-Plugin-MetaResources-Template is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
perl-Dist-Zilla-Plugin-MetaResources-Template. If not, see <http://www.gnu.org/licenses/>.

=cut
