| 1 | package Fey::Meta::Attribute::FromSelect; | ||||||||||
| 2 | { | ||||||||||
| 3 | 10 | $Fey::Meta::Attribute::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::Attribute'; | |||||||||
| 13 | |||||||||||
| 14 | 10 | with 'Fey::Meta::Role::FromSelect'; | |||||||||
| 15 | |||||||||||
| 16 | sub _process_options { | ||||||||||
| 17 | 0 | ▶ | my $class = shift; | ||||||||
|
Subroutine
_process_options: not called CC=1 SCAR=6.9
| |||||||||||
| 18 | 0 | my $name = shift; | |||||||||
| 19 | 0 | my $options = shift; | |||||||||
| 20 | |||||||||||
| 21 | 0 | $options->{lazy} = 1; | |||||||||
| 22 | |||||||||||
| 23 | $options->{default} = $class->_make_sub_from_select( | ||||||||||
| 24 | $options->{select}, | ||||||||||
| 25 | $options->{bind_params}, | ||||||||||
| 26 | $options->{multi_column}, | ||||||||||
| 27 | $options->{isa}, | ||||||||||
| 28 | 0 | ); | |||||||||
| 29 | |||||||||||
| 30 | 0 | return $class->SUPER::_process_options( $name, $options ); | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | sub _new { | ||||||||||
| 34 | 0 | ▶ | my $class = shift; | ||||||||
|
Subroutine
_new: not called CC=2 SCAR=17.9
| |||||||||||
| 35 | 0 | ▶ | my $options = @_ == 1 ? $_[0] : {@_}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | |||||||||||
| 37 | 0 | my $self = $class->SUPER::_new($options); | |||||||||
| 38 | |||||||||||
| 39 | 0 | $self->{select} = $options->{select}; | |||||||||
| 40 | 0 | $self->{bind_params} = $options->{bind_params}; | |||||||||
| 41 | 0 | $self->{is_multi_column} = $options->{multi_column}; | |||||||||
| 42 | |||||||||||
| 43 | 0 | return $self; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | # The parent class's constructor is not a Moose::Object-based | ||||||||||
| 47 | # constructor, so we don't want to inline one that is. | ||||||||||
| 48 | 10 | __PACKAGE__->meta()->make_immutable( inline_constructor => 0 ); | |||||||||
| 49 | |||||||||||
| 50 | package # hide from PAUSE | ||||||||||
| 51 | Moose::Meta::Attribute::Custom::FromSelect; | ||||||||||
| 52 | 0 | ▶ | sub register_implementation {'Fey::Meta::Attribute::FromSelect'} | ||||||||
|
Subroutine
register_implementation: not called CC=1 SCAR=6.9
| |||||||||||
| 53 | |||||||||||
| 54 | 10 | 1; | |||||||||
| 55 | |||||||||||
| 56 | # ABSTRACT: An attribute metaclass for SELECT-based attributes | ||||||||||
| 57 | |||||||||||
| 58 | __END__ | ||||||||||
| 59 | |||||||||||
| 60 | =pod | ||||||||||
| 61 | |||||||||||
| 62 | =head1 NAME | ||||||||||
| 63 | |||||||||||
| 64 | Fey::Meta::Attribute::FromSelect - An attribute metaclass for SELECT-based attributes | ||||||||||
| 65 | |||||||||||
| 66 | =head1 VERSION | ||||||||||
| 67 | |||||||||||
| 68 | version 0.46 | ||||||||||
| 69 | |||||||||||
| 70 | =head1 SYNOPSIS | ||||||||||
| 71 | |||||||||||
| 72 | package MyApp::Song; | ||||||||||
| 73 | |||||||||||
| 74 | has average_rating => ( | ||||||||||
| 75 | metaclass => 'FromSelect', | ||||||||||
| 76 | is => 'ro', | ||||||||||
| 77 | isa => 'Float', | ||||||||||
| 78 | select => $select, | ||||||||||
| 79 | bind_params => sub { $_[0]->song_id() }, | ||||||||||
| 80 | ); | ||||||||||
| 81 | |||||||||||
| 82 | =head1 DESCRIPTION | ||||||||||
| 83 | |||||||||||
| 84 | This attribute metaclass allows you to set an attribute's default | ||||||||||
| 85 | based on a C<SELECT> query and optional bound parameters. This is a | ||||||||||
| 86 | fairly common need when writing ORM-based classes. | ||||||||||
| 87 | |||||||||||
| 88 | =head1 OPTIONS | ||||||||||
| 89 | |||||||||||
| 90 | This metaclass accepts two additional parameters in addition to the | ||||||||||
| 91 | normal Moose attribute options. | ||||||||||
| 92 | |||||||||||
| 93 | =over 4 | ||||||||||
| 94 | |||||||||||
| 95 | =item * select | ||||||||||
| 96 | |||||||||||
| 97 | This must do the L<Fey::Role::SQL::ReturnsData> role. It is required. | ||||||||||
| 98 | |||||||||||
| 99 | =item * bind_params | ||||||||||
| 100 | |||||||||||
| 101 | This must be a subroutine reference, which when called will return an | ||||||||||
| 102 | array of bind parameters for the query. This subref will be called as | ||||||||||
| 103 | a method on the object which has the attribute. This is an optional | ||||||||||
| 104 | parameter. | ||||||||||
| 105 | |||||||||||
| 106 | =back | ||||||||||
| 107 | |||||||||||
| 108 | Note that this metaclass overrides any value you provide for "default" | ||||||||||
| 109 | with a subroutine that executes the query and gets the value it | ||||||||||
| 110 | returns. | ||||||||||
| 111 | |||||||||||
| 112 | =head1 METHODS | ||||||||||
| 113 | |||||||||||
| 114 | This class adds a few methods to those provided by | ||||||||||
| 115 | C<Moose::Meta::Attribute>: | ||||||||||
| 116 | |||||||||||
| 117 | =head2 $attr->select() | ||||||||||
| 118 | |||||||||||
| 119 | Returns the query object associated with this attribute. | ||||||||||
| 120 | |||||||||||
| 121 | =head2 $attr->bind_params() | ||||||||||
| 122 | |||||||||||
| 123 | Returns the bind_params subroutine reference associated with this | ||||||||||
| 124 | attribute, if any. | ||||||||||
| 125 | |||||||||||
| 126 | =head1 ArrayRef TYPES | ||||||||||
| 127 | |||||||||||
| 128 | By default, the C<SELECT> is expected to return just a single row with | ||||||||||
| 129 | one column. However, if you set the type of the attribute to ArrayRef | ||||||||||
| 130 | (or a subtype), then the select can return multiple rows, still with a | ||||||||||
| 131 | single column. | ||||||||||
| 132 | |||||||||||
| 133 | =head1 AUTHOR | ||||||||||
| 134 | |||||||||||
| 135 | Dave Rolsky <autarch@urth.org> | ||||||||||
| 136 | |||||||||||
| 137 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 138 | |||||||||||
| 139 | This software is copyright (c) 2011 by Dave Rolsky. | ||||||||||
| 140 | |||||||||||
| 141 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 142 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 143 | |||||||||||
| 144 | =cut | ||||||||||