File Coverage

blib/lib/Fey/Meta/Method/FromSelect.pm
Criterion Covered Total %
statement 12 24 50.0
branch 0 6 0.0
condition 0 3 0.0
subroutine 4 7 57.1
pod 0 1 0.0
total 16 41 39.0


line stmt bran cond sub pod time code
1             package Fey::Meta::Method::FromSelect;
2              
3 10     10   53 use strict;
  10         20  
  10         362  
4 10     10   49 use warnings;
  10         19  
  10         345  
5 10     10   52 use namespace::autoclean;
  10         23  
  10         85  
6              
7             our $VERSION = '0.47';
8              
9 10     10   909 use Moose;
  10         17  
  10         69  
10              
11             extends 'Moose::Meta::Method', 'Class::MOP::Method::Generated';
12              
13             with 'Fey::Meta::Role::FromSelect';
14              
15             ## no critic (Moose::ProhibitNewMethod)
16             sub new {
17 0     0 0   my $class = shift;
18 0           my %options = @_;
19              
20             ( $options{package_name} && $options{name} )
21 0 0 0       || confess
22             "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT";
23              
24             $options{select}
25 0 0         || confess 'You must supply a select query';
26              
27 0           my $self = $class->_new( \%options );
28              
29 0           $self->_initialize_body;
30              
31 0           return $self;
32             }
33             ## use critic
34              
35             sub _new {
36 0     0     my $class = shift;
37 0 0         my $options = @_ == 1 ? $_[0] : {@_};
38              
39 0           return bless $options, $class;
40             }
41              
42             sub _initialize_body {
43 0     0     my $self = shift;
44              
45 0           $self->{body} = $self->_make_sub_from_select(
46             $self->select(),
47             $self->bind_params(),
48             $self->is_multi_column(),
49             );
50              
51             }
52              
53             __PACKAGE__->meta()->make_immutable( inline_constructor => 0 );
54              
55             1;
56              
57             # ABSTRACT: A method metaclass for SELECT-based methods
58              
59             __END__
60              
61             =pod
62              
63             =head1 NAME
64              
65             Fey::Meta::Method::FromSelect - A method metaclass for SELECT-based methods
66              
67             =head1 VERSION
68              
69             version 0.47
70              
71             =head1 SYNOPSIS
72              
73             package MyApp::Song;
74              
75             query average_rating => (
76             select => $select,
77             bind_params => sub { $_[0]->song_id() },
78             );
79              
80             =head1 DESCRIPTION
81              
82             This method metaclass allows you to generate a method based on a C<SELECT>
83             query and an optional C<bind_params> subroutine reference.
84              
85             =head1 OPTIONS
86              
87             This metaclass accepts two additional parameters in addition to the
88             normal Moose method options.
89              
90             =over 4
91              
92             =item * select
93              
94             This must do the L<Fey::Role::SQL::ReturnsData> role. It is required.
95              
96             =item * bind_params
97              
98             This must be a subroutine reference, which when called will return an array of
99             bind parameters for the query. This subref will be called as a method on the
100             object which has the method. This is an optional parameter.
101              
102             =back
103              
104             Note that this metaclass overrides any value you provide for "default"
105             with a subroutine that executes the query and gets the value it
106             returns.
107              
108             =head1 METHODS
109              
110             This class adds a few methods to those provided by
111             C<Moose::Meta::Attribute>:
112              
113             =head2 $attr->select()
114              
115             Returns the query object associated with this attribute.
116              
117             =head2 $attr->bind_params()
118              
119             Returns the bind_params subroutine reference associated with this
120             attribute, if any.
121              
122             =head1 WANTARRAY
123              
124             The generated method will use DBI's C<selectcol_arrayref()> method to fetch
125             data from the database. If called in a list context, it returns all the values
126             it retrieves. In scalar context, it returns just the first value.
127              
128             =head1 AUTHOR
129              
130             Dave Rolsky <autarch@urth.org>
131              
132             =head1 COPYRIGHT AND LICENSE
133              
134             This software is copyright (c) 2011 - 2015 by Dave Rolsky.
135              
136             This is free software; you can redistribute it and/or modify it under
137             the same terms as the Perl 5 programming language system itself.
138              
139             =cut