From: Matt S Trout Date: Mon, 26 Oct 2015 06:38:55 +0000 (+0000) Subject: initial skeleton for deparser X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=381e8dad121cf71a2a50c0a325214badf6363dcd;p=scpubgit%2FDX.git initial skeleton for deparser --- diff --git a/lib/DX/Deparse.pm b/lib/DX/Deparse.pm new file mode 100644 index 0000000..ecee7ba --- /dev/null +++ b/lib/DX/Deparse.pm @@ -0,0 +1,59 @@ +package DX::Deparse; + +use DX::Class; + +sub indent_by { ' ' } + +sub max_width { 78 } + +sub _inner_meta { + my ($self, $ometa) = @_; + +{ %$ometa, width_left => $ometa->{width_left} - length $self->indent_by }; +} + +sub _indent_one { + my ($self, $to_indent) = @_; + $to_indent =~ s/^/${\$self->indent_by}/mg; + $to_indent; +} + +sub fmt { + my ($self, $thing) = @_; + $self->_fmt($thing, { width_left => $self->max_width })."\n"; +} + +sub _fmt { + my ($self, $thing, $meta) = @_; + return $thing unless ref($thing); + my $type = join'_', split '::', lc +(ref($thing) =~ /^DX::(.*)/)[0]; + $self->${\"_fmt_${type}"}($thing, $meta); +} + +sub _fmt_value_dict { + my ($self, $dict, $ometa) = @_; + my $imeta = $self->_inner_meta($ometa); + my $chunks = $self->_indent_one(join "\n", map { + "$_ ".$self->_fmt($dict->get_member_at($_), $imeta) + } $dict->index_list); + "{{\n${chunks}\n}}"; +} + +sub _fmt_value_string { + my ($self, $string) = @_; + my $val = $string->string_value; + if ($val =~ /^\w+$/) { + qq{'${val}'} + } else { + qq{{'${val}'}} + } +} + +sub _fmt_value_number { $_[1]->number_value } + +sub _fmt_value_true { 'true' } + +sub _fmt_value_false { 'false' } + +sub _fmat_value_unset { 'unset' } + +1;