| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Fey::Role::HasAliasName; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 28 |  |  | 28 |  | 16836 | use strict; | 
|  | 28 |  |  |  |  | 53 |  | 
|  | 28 |  |  |  |  | 1139 |  | 
| 4 | 28 |  |  | 28 |  | 128 | use warnings; | 
|  | 28 |  |  |  |  | 41 |  | 
|  | 28 |  |  |  |  | 793 |  | 
| 5 | 28 |  |  | 28 |  | 122 | use namespace::autoclean; | 
|  | 28 |  |  |  |  | 44 |  | 
|  | 28 |  |  |  |  | 202 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | our $VERSION = '0.42'; | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 28 |  |  | 28 |  | 2480 | use Fey::Types qw( Bool Str ); | 
|  | 28 |  |  |  |  | 41 |  | 
|  | 28 |  |  |  |  | 219 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 | 28 |  |  | 28 |  | 142939 | use MooseX::Role::Parameterized 0.04; | 
|  | 28 |  |  |  |  | 1384085 |  | 
|  | 28 |  |  |  |  | 156 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | parameter 'generated_alias_prefix' => ( | 
| 14 |  |  |  |  |  |  | isa      => Str, | 
| 15 |  |  |  |  |  |  | required => 1, | 
| 16 |  |  |  |  |  |  | ); | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | parameter 'sql_needs_parens' => ( | 
| 19 |  |  |  |  |  |  | isa     => Bool, | 
| 20 |  |  |  |  |  |  | default => 0, | 
| 21 |  |  |  |  |  |  | ); | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | has 'alias_name' => ( | 
| 24 |  |  |  |  |  |  | is     => 'rw', | 
| 25 |  |  |  |  |  |  | isa    => Str, | 
| 26 |  |  |  |  |  |  | writer => 'set_alias_name', | 
| 27 |  |  |  |  |  |  | ); | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | requires 'sql'; | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | sub alias { | 
| 32 | 1 |  |  | 1 | 1 | 54 | $_[0]->set_alias_name( $_[1] ); | 
| 33 | 1 |  |  |  |  | 4 | return $_[0]; | 
| 34 |  |  |  |  |  |  | } | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | sub sql_with_alias { | 
| 37 | 13 | 100 |  | 13 | 1 | 542 | $_[0]->_make_alias() | 
| 38 |  |  |  |  |  |  | unless $_[0]->alias_name(); | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 13 |  |  |  |  | 71 | my $sql = $_[0]->_sql_for_alias( $_[1] ); | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 13 |  |  |  |  | 29 | $sql .= ' AS '; | 
| 43 | 13 |  |  |  |  | 461 | $sql .= $_[1]->quote_identifier( $_[0]->alias_name() ); | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 13 |  |  |  |  | 955 | return $sql; | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | sub sql_or_alias { | 
| 49 | 29 | 100 |  | 29 | 1 | 1224 | return $_[1]->quote_identifier( $_[0]->alias_name() ) | 
| 50 |  |  |  |  |  |  | if $_[0]->alias_name(); | 
| 51 |  |  |  |  |  |  |  | 
| 52 | 23 |  |  |  |  | 145 | return $_[0]->sql( $_[1] ); | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | role { | 
| 56 |  |  |  |  |  |  | my $p = shift; | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | my $parens = $p->sql_needs_parens(); | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | method _sql_for_alias => sub { | 
| 61 | 13 |  |  | 13 |  | 76 | my $sql = $_[0]->sql( $_[1] ); | 
|  |  |  |  | 13 |  |  |  | 
|  |  |  |  | 13 |  |  |  | 
|  |  |  |  | 13 |  |  |  | 
|  |  |  |  | 13 |  |  |  | 
|  |  |  |  | 13 |  |  |  | 
| 62 | 13 | 100 |  |  |  | 65 | $sql = "( $sql )" if $parens; | 
| 63 | 13 |  |  |  |  | 34 | return $sql; | 
| 64 |  |  |  |  |  |  | }; | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | my $prefix = $p->generated_alias_prefix(); | 
| 67 |  |  |  |  |  |  | my $num    = 0; | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | method '_make_alias' => sub { | 
| 70 | 13 |  |  | 13 |  | 83 | my $self = shift; | 
|  |  |  |  | 13 |  |  |  | 
|  |  |  |  | 13 |  |  |  | 
|  |  |  |  | 13 |  |  |  | 
|  |  |  |  | 13 |  |  |  | 
|  |  |  |  | 13 |  |  |  | 
| 71 | 13 |  |  |  |  | 423 | $self->set_alias_name( $prefix . $num++ ); | 
| 72 |  |  |  |  |  |  | }; | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | }; | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | 1; | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | # ABSTRACT: A role for objects that bring an alias with them | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | __END__ | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | =pod | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | =head1 NAME | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | Fey::Role::HasAliasName - A role for objects that bring an alias with them | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | =head1 VERSION | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | version 0.42 | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | package My::Thing; | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | use Moose 0.90; | 
| 97 |  |  |  |  |  |  | with 'Fey::Role::HasAliasName' | 
| 98 |  |  |  |  |  |  | => { generated_alias_prefix => 'THING' }; | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | This role adds an C<alias_name> attribute to objects, as well as some | 
| 103 |  |  |  |  |  |  | methods for making use of that alias. | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | =head1 PARAMETERS | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | =head2 generated_alias_prefix | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | The prefix that generated aliases will have, e.g. C<LITERAL>, | 
| 110 |  |  |  |  |  |  | C<FUNCTION>, etc. Required. | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | =head2 sql_needs_parens | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | If true, C<sql_with_alias()> will wrap the output of C<sql()> when | 
| 115 |  |  |  |  |  |  | generating its own output. Default is false. | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | =head1 METHODS | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | =head2 $obj->alias_name() | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | Returns the current alias name, if any. | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | =head2 $obj->set_alias_name() | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | $obj->set_alias_name('my object'); | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | Sets the current alias name. | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | =head2 $obj->alias() | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | $obj->alias('my object')->do_something_else(...); | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | Sets the current alias name, then returns the object. | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | =head2 $obj->sql_with_alias() | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | =head2 $obj->sql_or_alias() | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | Returns the appropriate SQL snippet.  C<sql_with_alias> will generate | 
| 140 |  |  |  |  |  |  | an alias if one has not been set (using C<generated_alias_prefix>, | 
| 141 |  |  |  |  |  |  | above). | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | =head1 BUGS | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | See L<Fey> for details on how to report bugs. | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | =head1 AUTHOR | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | Dave Rolsky <autarch@urth.org> | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | This software is Copyright (c) 2011 - 2015 by Dave Rolsky. | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | This is free software, licensed under: | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | The Artistic License 2.0 (GPL Compatible) | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | =cut |