Showing 2 changed files with 44 additions and 35 deletions
+6 -6
lib/DBIx/Custom.pm
... ...
@@ -1,6 +1,6 @@
1 1
 package DBIx::Custom;
2 2
 
3
-our $VERSION = '0.1623';
3
+our $VERSION = '0.1624';
4 4
 
5 5
 use 5.008001;
6 6
 use strict;
... ...
@@ -393,11 +393,11 @@ sub execute{
393 393
 	    
394 394
 		# Result
395 395
 		my $result = $self->result_class->new(
396
-            sth             => $sth,
397
-            filters         => $self->filters,
398
-            filter_check    => $self->filter_check,
399
-            _auto_filter     => $auto_fetch_filter || {},
400
-            _default_filter => $self->default_fetch_filter
396
+            sth            => $sth,
397
+            filters        => $self->filters,
398
+            filter_check   => $self->filter_check,
399
+            default_filter => $self->default_fetch_filter,
400
+            _auto_filter   => $auto_fetch_filter || {}
401 401
         );
402 402
 
403 403
         return $result;
+38 -29
lib/DBIx/Custom/Result.pm
... ...
@@ -11,46 +11,55 @@ __PACKAGE__->attr([qw/filter_check filters sth/]);
11 11
 
12 12
 sub default_filter {
13 13
     my $self = shift;
14
-    my $fname = $_[0];
15 14
     
16
-    if (@_ && !$fname) {
17
-        $self->{_default_filter} = undef;
18
-    }
19
-    else {
20
-        croak qq{"$fname" is not registered}
21
-          unless exists $self->filters->{$fname};
22
-    
23
-        $self->{_default_filter} = $self->filters->{$fname};
15
+    if (@_) {
16
+        my $fname = $_[0];
17
+        if (@_ && !$fname) {
18
+            $self->{default_filter} = undef;
19
+        }
20
+        else {
21
+            croak qq{"$fname" is not registered}
22
+              unless exists $self->filters->{$fname};
23
+        
24
+            $self->{default_filter} = $self->filters->{$fname};
25
+        }
26
+        
27
+        return $self;
24 28
     }
25 29
     
26
-    return $self;
30
+    return $self->{default_filter};
27 31
 }
28 32
 
29 33
 sub filter {
30 34
     my $self = shift;
31
-    my $filter = ref $_[0] eq 'HASH' ? $_[0] : {@_};
32
-    
33
-    foreach my $column (keys %$filter) {
34
-        my $fname = $filter->{$column};
35
-        unless (ref $fname eq 'CODE') {
36
-          croak qq{"$fname" is not registered"}
37
-            unless exists $self->filters->{$fname};
38
-          
39
-          $filter->{$column} = $self->filters->{$fname};
35
+    
36
+    if (@_) {
37
+        my $filter = ref $_[0] eq 'HASH' ? $_[0] : {@_};
38
+        
39
+        foreach my $column (keys %$filter) {
40
+            my $fname = $filter->{$column};
41
+            unless (ref $fname eq 'CODE') {
42
+              croak qq{"$fname" is not registered"}
43
+                unless exists $self->filters->{$fname};
44
+              
45
+              $filter->{$column} = $self->filters->{$fname};
46
+            }
40 47
         }
48
+        
49
+        $self->{filter} = $filter;
50
+        
51
+        return $self;
41 52
     }
42 53
     
43
-    $self->{_filter} = $filter;
44
-    
45
-    return $self;
54
+    return $self->{filter};
46 55
 }
47 56
 
48 57
 sub fetch {
49 58
     my $self = shift;
50 59
     
51 60
     # Filters
52
-    my $filters = $self->{filters} || {};
53
-    my $filter  = $self->{_filter}  || {};
61
+    my $filters = $self->filters || {};
62
+    my $filter  = $self->{filter}  || {};
54 63
     my $auto_filter = $self->{_auto_filter} || {};
55 64
     $filter = {%$auto_filter, %$filter};
56 65
     
... ...
@@ -68,7 +77,7 @@ sub fetch {
68 77
         my $column = $columns->[$i];
69 78
         my $f  = exists $filter->{$column}
70 79
                ? $filter->{$column}
71
-               : $self->{_default_filter};
80
+               : $self->default_filter;
72 81
         
73 82
         # Filtering
74 83
         $row[$i] = $f->($row[$i]) if $f;
... ...
@@ -107,8 +116,8 @@ sub fetch_hash {
107 116
     my $self = shift;
108 117
     
109 118
     # Filters
110
-    my $filters = $self->{filters} || {};
111
-    my $filter  = $self->{_filter}  || {};
119
+    my $filters = $self->filters || {};
120
+    my $filter  = $self->filter  || {};
112 121
     my $auto_filter = $self->{_auto_filter} || {};
113 122
     $filter = {%$auto_filter, %$filter};
114 123
     
... ...
@@ -127,7 +136,7 @@ sub fetch_hash {
127 136
         my $column = $columns->[$i];
128 137
         my $f  = exists $filter->{$column}
129 138
                ? $filter->{$column}
130
-               : $self->{_default_filter};
139
+               : $self->default_filter;
131 140
         
132 141
         # Filtering
133 142
         $row_hash->{$column} = $f ? $f->($row->[$i]) : $row->[$i];
... ...
@@ -256,7 +265,6 @@ Fetch row into hash.
256 265
         my $first_author  = $rows->[0]{author};
257 266
         my $second_title  = $rows->[1]{title};
258 267
         my $second_author = $rows->[1]{author};
259
-    
260 268
     }
261 269
     
262 270
     # Fetch all rows into array of hash
... ...
@@ -295,6 +303,7 @@ and implements the following new ones.
295 303
 
296 304
 =head2 C<(deprecated) default_filter>
297 305
 
306
+    my $default_filter = $result->default_filter;
298 307
     $result = $result->default_filter($filter);
299 308
 
300 309
 Default filter when a row is fetched.