Showing 3 changed files with 53 additions and 3 deletions
+1
lib/DBIx/Custom.pm
... ...
@@ -14,6 +14,7 @@ use DBIx::Custom::Model;
14 14
 use DBIx::Custom::Tag;
15 15
 use DBIx::Custom::Order;
16 16
 use DBIx::Custom::Util qw/_array_to_hash _subname/;
17
+use DBIx::Custom::Mapper;
17 18
 use Encode qw/encode encode_utf8 decode_utf8/;
18 19
 use Scalar::Util qw/weaken/;
19 20
 
+6 -3
lib/DBIx/Custom/Mapper.pm
... ...
@@ -1,13 +1,16 @@
1 1
 package DBIx::Custom::Mapper;
2 2
 use Object::Simple -base;
3 3
 
4
+use Carp 'croak';
4 5
 use DBIx::Custom::Util '_subname';
5 6
 
6 7
 # Carp trust relationship
7 8
 push @DBIx::Custom::CARP_NOT, __PACKAGE__;
8 9
 
9
-has [qw/param pass/]
10
-    condition => sub { defined $_[0] && length $_[0] };
10
+has [qw/param pass/],
11
+    condition => sub {
12
+        sub { defined $_[0] && length $_[0] }
13
+    };
11 14
 
12 15
 sub map {
13 16
     my ($self, %rule) = @_;
... ...
@@ -25,7 +28,7 @@ sub map {
25 28
         if (ref $rule{$key} eq 'ARRAY') {
26 29
             foreach my $some (@{$rule{$key}}) {
27 30
                 $new_key = $some unless ref $some;
28
-                $condition = $some->{if} if ref $some eq 'HASH';
31
+                $condition = $some->{condition} if ref $some eq 'HASH';
29 32
                 $value_cb = $some if ref $some eq 'CODE';
30 33
             }
31 34
         }
+46
t/common.t
... ...
@@ -2597,6 +2597,52 @@ $dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2});
2597 2597
 $rows = $dbi->select(prefix => "$key1,", column => $key2, table => $table1)->all;
2598 2598
 is_deeply($rows, [{$key1 => 1, $key2 => 2}], "table");
2599 2599
 
2600
+test 'mapper';
2601
+$DB::single = 1;
2602
+$dbi = DBIx::Custom->connect;
2603
+$param = $dbi->mapper(param => {id => 1, author => 'Ken', price => 1900})->map(
2604
+    id => "$table1.id",
2605
+    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2606
+    price => ["$table1.price", {condition => sub { $_[0] eq 1900 }}]
2607
+);
2608
+is_deeply($param, {"$table1.id" => 1, "$table1.author" => '%Ken%',
2609
+  "$table1.price" => 1900});
2610
+
2611
+$param = $dbi->mapper(param => {id => 0, author => 0, price => 0})->map(
2612
+    id => "$table1.id",
2613
+    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2614
+    price => ["$table1.price", sub { '%' . $_[0] . '%' },
2615
+      {condition => sub { $_[0] eq 0 }}]
2616
+);
2617
+is_deeply($param, {"$table1.id" => 0, "$table1.author" => '%0%', "$table1.price" => '%0%'});
2618
+
2619
+$param = $dbi->mapper(param => {id => '', author => '', price => ''})->map(
2620
+    id => "$table1.id",
2621
+    author => ["$table1.author", sub { '%' . $_[0] . '%' }],
2622
+    price => ["$table1.price", sub { '%' . $_[0] . '%' },
2623
+      {condition => sub { $_[0] eq 1 }}]
2624
+);
2625
+is_deeply($param, {});
2626
+
2627
+$DB::single = 1;
2628
+$param = $dbi->mapper(param => {id => undef, author => undef, price => undef})->map(
2629
+    id => "$table1.id",
2630
+    price => ["$table1.price", {condition => 'exists'}]
2631
+);
2632
+is_deeply($param, {"$table1.price" => undef});
2633
+
2634
+$param = $dbi->mapper(param => {price => 'a'})->map(
2635
+    id => ["$table1.id", {condition => 'exists'}],
2636
+    price => ["$table1.price", sub { '%' . $_[0] }, {condition => 'exists'}]
2637
+);
2638
+is_deeply($param, {"$table1.price" => '%a'});
2639
+
2640
+$param = $dbi->mapper(param => {price => 'a'}, condition => 'exists')->map(
2641
+    id => ["$table1.id"],
2642
+    price => ["$table1.price", sub { '%' . $_[0] }]
2643
+);
2644
+is_deeply($param, {"$table1.price" => '%a'});
2645
+
2600 2646
 test 'map_param';
2601 2647
 $dbi = DBIx::Custom->connect;
2602 2648
 $param = $dbi->map_param(