1# ABSTRACT: Simple Exporter for Validation::Class Classes
2
3package Validation::Class::Exporter;
4
5use 5.008001;
6
7use strict;
8use warnings;
9
10our $VERSION = '7.900057'; # VERSION
11
12
13sub apply_spec {
14
15    my ($this, %args) = @_;
16
17    no strict 'refs';
18    no warnings 'once';
19    no warnings 'redefine';
20
21    my $parent = caller(0);
22
23    my @keywords = @{$args{keywords}}   if $args{keywords};
24    my @routines = @{$args{routines}}   if $args{routines};
25    my $settings = {@{$args{settings}}} if $args{settings};
26
27    *{"$parent\::import"} = sub {
28
29        my $child = caller(0);
30
31        *{"$child\::$_"} = *{"$parent\::$_"} for @{$args{keywords}};
32
33        *{"$child\::$_"} = *{"$parent\::$_"} for @{$args{routines}};
34
35        my $ISA = "$child\::ISA";
36
37        push @$ISA, 'Validation::Class'
38          unless grep { $_ eq 'Validation::Class' } @$ISA;
39
40        *{"$child\::$_"} = *{"Validation\::Class\::$_"}
41          for @Validation::Class::EXPORT;
42
43        strict->import;
44        warnings->import;
45
46        $child->load({@{$args{settings}}}) if $args{settings};
47
48        return $child;
49
50    };
51
52    return $this;
53
54}
55
561;
57
58__END__
59
60=pod
61
62=head1 NAME
63
64Validation::Class::Exporter - Simple Exporter for Validation::Class Classes
65
66=head1 VERSION
67
68version 7.900057
69
70=head1 SYNOPSIS
71
72    package MyApp::Validator;
73
74    use Validation::Class;
75    use Validation::Class::Exporter;
76
77    my @settings = (
78        classes => [
79            MyApp::Validator::DomainAlpha
80            MyApp::Validator::DomainBeta
81        ]
82    );
83
84    Validation::Class::Exporter->apply_spec(
85        routines => ['thing'], # export additional routines as is
86        settings => [@settings] # passed to the `load` keyword in V::C
87    );
88
89    sub thing {
90
91        my $args = pop;
92
93        my $class = shift || caller;
94
95        # routine as a keyword
96
97        # ... do some thing
98
99    };
100
101... in your application class:
102
103    package MyApp;
104
105    use MyApp::Validator;
106
107    thing ['a', 'b'];
108
109... in your application:
110
111    package main;
112
113    my $app = MyApp->new;
114
115=head1 DESCRIPTION
116
117This module (while experimental) encapsulates the exporting of keywords and
118routines. It applies the L<Validation::Class> framework along with any keyword
119routines and/or sub-routines specified with the apply_spec() method. It does
120this by simply by copying the spec into the calling class.
121
122To simplify writing exporter modules, C<Validation::Class::Exporter> also
123imports C<strict> and C<warnings> into your exporter module, as well as into
124modules that use it.
125
126=head1 METHODS
127
128=head2 apply_spec
129
130When you call this method, C<Validation::Class::Exporter> builds a custom
131C<import> method on the calling class. The C<import> method will export the
132functions you specify, and can also automatically export C<Validation::Class>
133making the calling class a Validation::Class derived class.
134
135This method accepts the following parameters:
136
137=over 8
138
139=item * routines => [ ... ]
140
141This list of function I<names only> will be exported into the calling class
142exactly as is, the functions can be used traditionally or as keywords so their
143parameter handling should be configured accordingly.
144
145=item * settings => [ ... ]
146
147This list of key/value pair will be passed to the load method imported from
148C<Validation::Class::load> and will be applied on the calling class.
149
150This approach affords you some trickery in that you can utilize the load method
151to apply the current class' configuration to the calling class' configuration,
152etc.
153
154=back
155
156=head1 AUTHOR
157
158Al Newkirk <anewkirk@ana.io>
159
160=head1 COPYRIGHT AND LICENSE
161
162This software is copyright (c) 2011 by Al Newkirk.
163
164This is free software; you can redistribute it and/or modify it under
165the same terms as the Perl 5 programming language system itself.
166
167=cut
168