Showing 4 changed files with 86 additions and 7 deletions
+3
Changes
... ...
@@ -1,3 +1,6 @@
1
+0.23
2
+  - DBIx::Custom::Mapper::map method support
3
+    {value => '%<value>%'} syntax
1 4
 0.22
2 5
   - added EXPERIMENTAL DBIx::Custom::Result::kv method
3 6
   - added EXPERIMENTAL DBIx::Custom::Result::flat method
+1 -1
lib/DBIx/Custom.pm
... ...
@@ -2,7 +2,7 @@ use 5.008007;
2 2
 package DBIx::Custom;
3 3
 use Object::Simple -base;
4 4
 
5
-our $VERSION = '0.22';
5
+our $VERSION = '0.23';
6 6
 
7 7
 use Carp 'croak';
8 8
 use DBI;
+67 -6
lib/DBIx/Custom/Mapper.pm
... ...
@@ -68,8 +68,16 @@ sub map {
68 68
       }
69 69
       else {
70 70
         if ($condition->($param->{$key})) {
71
-          $new_param->{$new_key} = defined $value
72
-            ? $value->($param->{$key}) : $param->{$key};
71
+          if (defined $value) {
72
+            if (ref $value) {
73
+              $new_param->{$new_key} = $value->($param->{$key});
74
+            }
75
+            else {
76
+              $value =~ s/<value>/$param->{$key}/e;
77
+              $new_param->{$new_key} = $value;
78
+            }
79
+          }
80
+          else { $new_param->{$new_key} = $param->{$key} }
73 81
         }
74 82
       }
75 83
     }
... ...
@@ -199,11 +207,17 @@ and implements the following new ones.
199 207
 
200 208
   my $new_param = $mapper->map(
201 209
     price => {key => 'book.price'}
202
-    title => {value => sub { '%' . $_[0] . '%'}}
203
-    author => ['book.author' => sub { '%' . $_[0] . '%'}] # Key and value
210
+    title => {value => '%<value>%'}
211
+    author => ['book.author' => '%<value>%']
212
+  );
213
+
214
+  my $new_param = $mapper->map(
215
+    price => {key => 'book.price'}
216
+    title => {value => sub { '%' . shift . '%'}}
217
+    author => ['book.author' => sub { '%' . shift . '%'}]
204 218
   );
205 219
 
206
-Map C<param> into new parameter.
220
+Map parameter in C<param> attribute into new parameter.
207 221
 
208 222
 For example, if C<param> is set to
209 223
 
... ...
@@ -222,6 +236,53 @@ The following hash reference is returned.
222 236
     'book.author' => '%Ken%',
223 237
   }
224 238
 
239
+=over 2
240
+
241
+B<Syntax:>
242
+
243
+=item * String => Hash reference
244
+
245
+  # String => Hash reference
246
+  price => {key => 'book.price'}
247
+  title => {value => '%<value>%'}
248
+  title => {value => sub { '%' . shift . '%'}}
249
+
250
+If C<key> is used, only key name is mapped to new parameter
251
+
252
+  # Rule
253
+  price => {key => 'book.price'}
254
+  # Parameter
255
+  price => 1900,
256
+  # New parameter
257
+  'book.price' => 1900,
258
+
259
+If C<value> is used, only value is mapped to new parameter
260
+
261
+  # Rule
262
+  title => {value => '%<value>%'}
263
+  title => {value => sub { '%' . shift . '%'}}
264
+  
265
+  # Parameter
266
+  title => 'Perl',
267
+  # New parameter
268
+  title => '%Perl%',
269
+
270
+C<E<lt>>valueE<gt>> is replaced by original value.
271
+You can use code reference to convert original value.
272
+
273
+=item * String => Array reference
274
+  
275
+  # String => Array reference
276
+  author => ['book.author' => '%<value>%']
277
+
278
+Both key name name and value is mapped to new parameter.
279
+This is same as the following syntax.
280
+
281
+  # Rule
282
+  {key => 'book.author', value => '%<value>%'}
283
+
284
+=back
285
+
225 286
 By default, If the value has length, key and value is mapped.
226 287
 
227 288
   title => 'Perl'  # Mapped
... ...
@@ -241,7 +302,7 @@ Or you can set C<condtion> option for each key.
241 302
     author => ['book.author', sub { '%' . $_[0] . '%'}, 'exists']
242 303
   );
243 304
 
244
-If C<pass> attrivute is set, the keys and value is copied without change.
305
+If C<pass> attribute is set, the keys and value is copied without change.
245 306
 
246 307
   $mapper->pass([qw/title author/]);
247 308
   my $new_param = $mapper->map(price => {key => 'book.price'});
+15
t/common.t
... ...
@@ -3288,6 +3288,21 @@ $param = $dbi->mapper(param => {id => 'a', author => 'b', price => 'c'}, pass =>
3288 3288
 ->map(price => {key => 'book.price'});
3289 3289
 is_deeply($param, {id => 'a', author => 'b', 'book.price' => 'c'});
3290 3290
 
3291
+$param = $dbi->mapper(param => {author => 'Ken',})->map(
3292
+  author => ["$table1.author" => '%<value>%'],
3293
+);
3294
+is_deeply($param, {"$table1.author" => '%Ken%'});
3295
+
3296
+$param = $dbi->mapper(param => {author => 'Ken'})->map(
3297
+  author => ["$table1.author" => 'p'],
3298
+);
3299
+is_deeply($param, {"$table1.author" => 'p'});
3300
+
3301
+$param = $dbi->mapper(param => {author => 'Ken',})->map(
3302
+  author => {value => '%<value>%'}
3303
+);
3304
+is_deeply($param, {"author" => '%Ken%'});
3305
+
3291 3306
 test 'order';
3292 3307
 $dbi = DBIx::Custom->connect;
3293 3308
 eval { $dbi->execute("drop table $table1") };