5985ecea842c1046fa6a9e595dec28e29b3c3e3c
[kivitendo-erp.git] / modules / override / YAML / Marshall.pm
1 package YAML::Marshall;
2 use strict; use warnings;
3 use YAML::Node();
4
5 sub import {
6     my $class = shift;
7     no strict 'refs';
8     my $package = caller;
9     unless (grep { $_ eq $class} @{$package . '::ISA'}) {
10         push @{$package . '::ISA'}, $class;
11     }
12
13     my $tag = shift;
14     if ($tag) {
15         no warnings 'once';
16         $YAML::TagClass->{$tag} = $package;
17         ${$package . "::YamlTag"} = $tag;
18     }
19 }
20
21 sub yaml_dump {
22     my $self = shift;
23     no strict 'refs';
24     my $tag = ${ref($self) . "::YamlTag"} || 'perl/' . ref($self);
25     $self->yaml_node($self, $tag);
26 }
27
28 sub yaml_load {
29     my ($class, $node) = @_;
30     if (my $ynode = $class->yaml_ynode($node)) {
31         $node = $ynode->{NODE};
32     }
33     bless $node, $class;
34 }
35
36 sub yaml_node {
37     shift;
38     YAML::Node->new(@_);
39 }
40
41 sub yaml_ynode {
42     shift;
43     YAML::Node::ynode(@_);
44 }
45
46 1;
47
48 __END__
49
50 =head1 NAME
51
52 YAML::Marshall - YAML marshalling class you can mixin to your classes
53
54 =head1 SYNOPSIS
55
56     package Bar;
57     use Foo -base;
58     use YAML::Marshall -mixin;
59
60 =head1 DESCRIPTION
61
62 For classes that want to handle their own YAML serialization.
63
64 =head1 AUTHOR
65
66 Ingy döt Net <ingy@cpan.org>
67
68 =head1 COPYRIGHT
69
70 Copyright (c) 2006. Ingy döt Net. All rights reserved.
71
72 This program is free software; you can redistribute it and/or modify it
73 under the same terms as Perl itself.
74
75 See L<http://www.perl.com/perl/misc/Artistic.html>
76
77 =cut