File Coverage

blib/lib/Fey/Meta/Attribute/FromSelect.pm
Criterion Covered Total %
statement 12 26 46.1
branch 0 2 0.0
condition n/a
subroutine 4 7 57.1
pod n/a
total 16 35 45.7


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