1package Exporter::Declare::Export::Generator;
2use strict;
3use warnings;
4
5use base 'Exporter::Declare::Export::Sub';
6use Exporter::Declare::Export::Variable;
7use Carp qw/croak/;
8
9sub required_specs {
10    my $self = shift;
11    return(
12        $self->SUPER::required_specs(),
13        qw/ type /,
14    );
15}
16
17sub type { shift->_data->{ type }}
18
19sub new {
20    my $class = shift;
21    croak "Generators must be coderefs, not " . ref($_[0])
22        unless ref( $_[0] ) eq 'CODE';
23    $class->SUPER::new( @_ );
24}
25
26sub generate {
27    my $self = shift;
28    my ( $import_class, @args ) = @_;
29    my $ref = $self->( $self->exported_by, $import_class, @args );
30
31    return Exporter::Declare::Export::Sub->new(
32        $ref,
33        %{ $self->_data },
34    ) if $self->type eq 'sub';
35
36    return Exporter::Declare::Export::Variable->new(
37        $ref,
38        %{ $self->_data },
39    ) if $self->type eq 'variable';
40
41    return $self->type->new(
42        $ref,
43        %{ $self->_data },
44    );
45}
46
47sub inject {
48    my $self = shift;
49    my ( $class, $name, @args ) = @_;
50    $self->generate( $class, @args )->inject( $class, $name );
51}
52
531;
54
55=head1 NAME
56
57Exporter::Declare::Export::Generator - Export class for exports that should be
58generated when imported.
59
60=head1 DESCRIPTION
61
62Export class for exports that should be generated when imported.
63
64=head1 OVERRIDEN METHODS
65
66=over 4
67
68=item $class->new( $ref, $ref, exported_by => $package, type => $type, %data )
69
70You must specify the type as 'sub' or 'variable'.
71
72=item $export->inject( $package, $name, @args )
73
74Calls generate() with @args to create a generated export. The new export is
75then injected.
76
77=back
78
79=head1 ADDITIONAL METHODS
80
81=over 4
82
83=item $new = $export->generate( $import_class, @args )
84
85Generates a new export object.
86
87=item $type = $export->type()
88
89Returns the type of object to be generated (sub or variable)
90
91=back
92
93=head1 AUTHORS
94
95Chad Granum L<exodist7@gmail.com>
96
97=head1 COPYRIGHT
98
99Copyright (C) 2010 Chad Granum
100
101Exporter-Declare is free software; Standard perl licence.
102
103Exporter-Declare is distributed in the hope that it will be useful, but
104WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
105FITNESS FOR A PARTICULAR PURPOSE.  See the license for more details.
106