File Coverage

blib/lib/DBIx/Custom/Order.pm
Criterion Covered Total %
statement 28 29 96.5
branch 4 6 66.6
condition n/a
subroutine 6 7 85.7
pod 2 2 100.0
total 40 44 90.9


line stmt bran cond sub pod time code
1             package DBIx::Custom::Order;
2 16     16   95 use Object::Simple -base;
  16         24  
  16         83  
3 16     16   1366 use DBIx::Custom::Util '_deprecate';
  16         23  
  16         1172  
4              
5             use overload
6 0     0   0 'bool' => sub {1},
7 24     24   44 '""' => sub { shift->to_string },
8 16     16   89 fallback => 1;
  16         27  
  16         122  
9              
10             has 'dbi';
11             has orders => sub { [] };
12              
13             sub prepend {
14 6     6 1 3110 my $self = shift;
15            
16 6         11 for my $order (reverse @_) {
17 9         15 unshift @{$self->orders}, $order;
  9         163  
18             }
19            
20 6         41 return $self;
21             }
22              
23             sub to_string {
24 24     24 1 30 my $self = shift;
25            
26 24         30 my $exists = {};
27 24         30 my @orders;
28 24         27 for my $order (@{$self->orders}) {
  24         339  
29 60 50       180 next unless defined $order;
30 60         88 $order =~ s/^\s+//;
31 60         90 $order =~ s/\s+$//;
32 60         141 my ($column, $direction) = split /\s+/, $order;
33 60 100       112 push @orders, $order unless $exists->{$column};
34 60         95 $exists->{$column} = 1;
35             }
36            
37 24 50       39 return '' unless @orders;
38 24         90 return 'order by ' . join(', ', @orders);
39             }
40              
41             1;
42              
43             =head1 NAME
44              
45             DBIx::Custom::Order - Order by clause
46              
47             =head1 SYNOPSIS
48              
49             # Result
50             my $order = DBIx::Custom::Order->new;
51             $order->prepend('title', 'author desc');
52             my $order_by = "$order";
53            
54             =head1 ATTRIBUTES
55              
56             =head2 dbi
57              
58             my $dbi = $order->dbi;
59             $order = $order->dbi($dbi);
60              
61             L object.
62              
63             =head2 orders
64              
65             my $orders = $result->orders;
66             $result = $result->orders(\%orders);
67              
68             Parts of order by clause
69              
70             =head1 METHODS
71              
72             L inherits all methods from L
73             and implements the following new ones.
74              
75             =head2 prepend
76              
77             $order->prepend('title', 'author desc');
78              
79             Prepend order parts to C.
80              
81             =head2 to_string
82              
83             my $order_by = $order->to_string;
84              
85             Create order by clause. If column name is duplicated, First one is used.
86             C override stringification. so you can write the following way.
87              
88             my $order_by = "$order";
89              
90             =cut
91