File: Serializer.pm

package info (click to toggle)
libextutils-builder-perl 0.019-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 284 kB
  • sloc: perl: 1,394; makefile: 2
file content (160 lines) | stat: -rw-r--r-- 3,422 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package ExtUtils::Builder::Serializer;
$ExtUtils::Builder::Serializer::VERSION = '0.019';
use strict;
use warnings;

use Carp 'croak';

use ExtUtils::Builder::Action::Command;
use ExtUtils::Builder::Action::Code;
use ExtUtils::Builder::Node;
use ExtUtils::Builder::Plan;

sub serialize_plan {
	my ($self, $plan) = @_;

	my %nodes;
	for my $node_name ($plan->node_names) {
		$nodes{$node_name} = $self->serialize_node($plan->node($node_name));
	}

	return {
		nodes => \%nodes,
	}
}

sub serialize_node {
	my ($self, $node, %opts) = @_;
	my @actions = map { $self->serialize_action($_, %opts) } $node->flatten;
	return {
		dependencies => [ $node->dependencies ],
		actions      => \@actions,
		type         => $node->type,
	}
}

sub serialize_action {
	my ($self, $action, %opts) = @_;
	my $preference = $action->preference('code', 'command');
	my $method = $preference eq 'code' ? 'serialize_code' : 'serialize_command';
	return $self->$method($action, %opts);
}

sub serialize_code {
	my ($self, $action, %opts) = @_;
	return [ 'code', $action->to_code_hash(%opts) ];
}

sub serialize_command {
	my ($self, $action, %opts) = @_;
	return map { [ 'command', $_ ] } $action->to_command(%opts)
}


sub deserialize_plan {
	my ($self, $serialized, %options) = @_;

	my %nodes;
	for my $node_name (keys %{ $serialized->{nodes} }) {
		$nodes{$node_name} = $self->deserialize_node($node_name, $serialized->{nodes}{$node_name}, %options);
	}

	return ExtUtils::Builder::Plan->new(
		nodes => \%nodes,
	);
}

sub deserialize_node {
	my ($self, $name, $serialized, %options) = @_;

	my @actions = map { $self->deserialize_action($_, %options) } @{ $serialized->{actions} };

	return ExtUtils::Builder::Node->new(
		target       => $name,
		dependencies => [ @{ $serialized->{dependencies} } ],
		actions      => \@actions,
		type         => $serialized->{type},
	);
}

sub deserialize_action {
	my ($self, $serialized, %options) = @_;
	my ($command, @args) = @{$serialized};

	if ($command eq 'command') {
		return ExtUtils::Builder::Action::Command->new(command => $args[0]);
	} elsif ($command eq 'code') {
		return map { ExtUtils::Builder::Action::Code->new(%$_) } @args;
	} else {
		croak "Unknown serialized command $command";
	}
}

1;

#ABSTRACT: 

__END__

=pod

=encoding UTF-8

=head1 NAME

ExtUtils::Builder::Serializer -  

=head1 VERSION

version 0.019

=head1 DESCRIPTION

XXX

=head1 METHODS

=head2 serialize_plan($plan)

Serialize a plan into a JSON compatible hash structure.

=head2 serialize_node($node)

Serialize a node into a JSON compatible hash structure.

=head2 serialize_action($action)

Serialize an action into a JSON compatible hash structure.

=head2 serialize_code($action)

Serialize a code action into a JSON compatible hash structure.

=head2 serialize_command($action)

Serialize a command action into a JSON compatible hash structure.

=head2 deserialize_plan($plan)

Deserialize a plan from a JSON compatible hash structure.

=head2 deserialize_node($node)

Deserialize a node from a JSON compatible hash structure.

=head2 deserialize_action($action)

Deserialize an action from a JSON compatible hash structure.

=head1 AUTHOR

Leon Timmermans <fawaka@gmail.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by Leon Timmermans.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut