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