Merge branch 'q'
[dbsrgits/SQL-Abstract.git] / lib / SQL / Abstract / Role / Plugin.pm
CommitLineData
60add55a 1package SQL::Abstract::Role::Plugin;
2
3use Moo::Role;
4
5has sqla => (
6 is => 'ro', init_arg => undef,
7 handles => [ qw(
8 expand_expr render_aqt join_query_parts
9 ) ],
10);
11
12sub cb {
13 my ($self, $method, @args) = @_;
14 return sub {
15 local $self->{sqla} = shift;
16 $self->$method(@args, @_)
17 };
18}
19
20sub register {
21 my ($self, @pairs) = @_;
22 my $sqla = $self->sqla;
23 while (my ($method, $cases) = splice(@pairs, 0, 2)) {
24 my @cases = @$cases;
25 while (my ($name, $case) = splice(@cases, 0, 2)) {
26 $sqla->$method($name, $self->cb($case));
27 }
28 }
29 return $self;
30}
31
32sub apply_to {
33 my ($self, $sqla) = @_;
34 $self = $self->new unless ref($self);
35 local $self->{sqla} = $sqla;
36 $self->register_extensions($sqla);
37}
38
39requires 'register_extensions';
40
411;
42
43__END__
44
45=head1 NAME
46
47SQL::Abstract::Role::Plugin - helpful methods for plugin authors
48
49=head1 METHODS
50
51=head2 apply_to
52
53Applies the plugin to an L<SQL::Abstract> object.
54
55=head2 register_extensions
56
57Provided by the plugin, registers its extensions to the sqla object.
58
59=head2 cb
60
61Creates a callback to call a method on the plugin.
62
63=head2 register
64
65Calls methods on the sqla object with arguments wrapped as callbacks.
66
67=head2 sqla
68
69Available only during plugin callback executions, contains the currently
70active L<SQL::Abstract> object.
71
72=cut