sub
as_hash {
my
$self
=
shift
;
my
%args
=
@_
;
my
$skip_re
=
$args
{skip_re};
my
$parent
=
$args
{_parent};
my
%parent_columns
;
if
(
$parent
) {
%parent_columns
=
reverse
$parent
->column_map;
}
my
%h
;
for
my
$col
(
$self
->meta->columns ) {
next
if
$parent_columns
{
$col
->name};
my
$accessor
=
$col
->accessor_method_name;
my
$value
=
scalar
(
$self
->
$accessor
);
if
(
ref
$value
eq
'DateTime'
) {
my
$offset
=
$value
->time_zone->name;
$value
=
$value
->iso8601;
if
(
$offset
=~ /\d{4}/ ) {
$offset
=~ s/00$/:00/;
$value
.=
$offset
;
}
elsif
(
$offset
=~ /^UTC|floating/) {
}
else
{
WARN
"unrecognized timezone name : $offset"
;
}
}
next
if
$skip_re
&&
$accessor
=~ /
$skip_re
/;
$h
{
$accessor
} =
$value
;
}
for
my
$rel
(
$self
->meta->relationships) {
next
unless
$rel
->object_has_related_objects(
$self
);
my
$name
=
$rel
->name;
die
"cannot recurse"
unless
$self
->can(
$name
);
if
(
$rel
->type eq
'one to one'
) {
$h
{
$name
} =
$self
->
$name
->as_hash(
_parent
=>
$rel
);
}
elsif
(
$rel
->type eq
'one to many'
) {
my
@children
=
$self
->
$name
;
for
my
$child
(
@children
) {
die
"cannot dump $name"
unless
$child
->can(
'as_hash'
);
$h
{
$name
} ||= [];
push
@{
$h
{
$name
} },
$child
->as_hash(
_parent
=>
$rel
);
}
}
else
{
}
}
return
\
%h
;
}
our
%NestedMap
;
sub
nested_tables {
my
$self
=
shift
;
my
$class
=
ref
$self
||
$self
;
return
$NestedMap
{
$class
}
unless
@_
> 0;
$NestedMap
{
$class
} =
ref
(
$_
[0]) ?
shift
: [
@_
];
return
$NestedMap
{
$class
};
}
1;