Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add support for into meta key:

Allows us to move the contents of a specific meta block into a HashRef.
It is usefull if you don't want to mix your fields with nested keys,
thus moving all fields into a hashRef under the `into` value as key.

If the output without into is:

~~~perl
{
  k => 1,
  f1 => 'field 1',
  f2 => 'field 2',
  n1 => [
    ... nested1 ...
  ],
  n2 => [
    ... nested2 ...
  ],
}
~~~

Adding a `into => 'top'` to the that meta block definition would
transform it into:

~~~perl
{
  top => {
    k => 1,
    f1 => 'field 1',
    f2 => 'field 2',
  },
  n1 => [
    ... nested1 ...
  ],
  n2 => [
    ... nested2 ...
  ],
}
~~~

Signed-off-by: Pedro Melo <melo@simplicidade.org>
  • Loading branch information...
commit 763ad2149cb631fe1b3cf9f9c89013501ba54113 1 parent 62dbc86
@melo authored
Showing with 11 additions and 3 deletions.
  1. +6 −1 lib/DBIx/Nesting.pm
  2. +5 −2 t/10-compiler.t
View
7 lib/DBIx/Nesting.pm
@@ -40,7 +40,8 @@ sub _emit_meta_block {
$p_o_var = '@res' unless $p_o_var;
$p_s_var = '$seen' unless $p_s_var;
- my ($id, $flds, $key, $type, $nest, $prfx) = @{$meta}{qw(id fields key type nest prefix)};
+ my ($id, $flds, $key, $into, $type, $nest, $prfx) =
+ @{$meta}{qw(id fields key into type nest prefix)};
my $o_var = "\$o$id";
my $s_var = "\$s$id";
my $f_var = "\$f$id";
@@ -81,6 +82,7 @@ sub _emit_meta_block {
. "for my $loop_var (\@$f_var) {"
. "$o_var\->{$loop_var\->{name}} = $r_var\->{$loop_var\->{col}};" . '}';
}
+ $p .= "$o_var = { '$into' => $o_var };" if $into;
## per relation-type manipulation
if ($type eq 'multiple') {
@@ -151,6 +153,9 @@ sub _expand_meta_with_defaults {
}
$cm{key} = $key if $key;
+ # into elevation
+ $cm{into} = $meta->{into} if exists $meta->{into};
+
# Relation type
my $type = 'multiple';
if (exists $meta->{type}) {
View
7 t/10-compiler.t
@@ -122,7 +122,7 @@ __DATA__
> msg simple meta, single col set, no joins, explicit pk, extra field meta, explicit prefix
>+ meta
- { fields => ['k', n => { label => 'N' }], key => 'k', prefix => 'p' }
+ { fields => ['k', n => { label => 'N' }], key => 'k', prefix => 'p', into => 't' }
>+ expected
@@ -130,6 +130,7 @@ __DATA__
key => [{ name => 'k', col => 'p_k' }],
id => 1,
prefix => 'p_',
+ into => 't',
type => 'multiple',
nest => {},
}
@@ -611,10 +612,11 @@ __DATA__
> for _emit_code
-> msg complex multi-nested meta, without fields, mixed relation types
+> msg complex multi-nested meta, without fields, mixed relation types, with into
>+ meta
{ key => 'k',
+ into => 'm',
nest => {
s => { type => 'single', key => 'k' },
t => {
@@ -649,6 +651,7 @@ __DATA__
for my $f (@$f1) {\
$o1->{$f->{name}} = $r->{$f->{col}};\
}\
+ $o1 = { 'm' => $o1 };\
push @res, $s1->{o} = $o1;\
} \
$o1 = $s1->{o};\
Please sign in to comment.
Something went wrong with that request. Please try again.