| 1 | package Template::Flute::Specification::Scoped; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Template::Flute::Specification; | ||
| 7 | use Config::Scoped; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Template::Flute::Specification::Scoped - Config::Scoped Specification Parser | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | $scoped = new Template::Flute::Specification::Scoped; | ||
| 16 | |||
| 17 | $spec = $scoped->parse_file($specification_file); | ||
| 18 | $spec = $scoped->parse($specification_text); | ||
| 19 | |||
| 20 | =head1 CONSTRUCTOR | ||
| 21 | |||
| 22 | =head2 new | ||
| 23 | |||
| 24 | Create a Template::Flute::Specification::Scoped object. | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | # Constructor | ||
| 29 | |||
| 30 | sub new { | ||
| 31 | my ($class, $self); | ||
| 32 | my (%params); | ||
| 33 | |||
| 34 | $class = shift; | ||
| 35 | %params = @_; | ||
| 36 | |||
| 37 | $self = \%params; | ||
| 38 | |||
| 39 | bless ($self, $class); | ||
| 40 | } | ||
| 41 | |||
| 42 | =head1 METHODS | ||
| 43 | |||
| 44 | =head2 parse [ STRING | SCALARREF ] | ||
| 45 | |||
| 46 | Parses text from STRING or SCALARREF and returns L<Template::Flute::Specification> | ||
| 47 | object in case of success. | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | sub parse { | ||
| 52 | my ($self, $text) = @_; | ||
| 53 | my ($scoped, $config); | ||
| 54 | |||
| 55 | # create Config::Scoped parser and parse text | ||
| 56 | $scoped = new Config::Scoped; | ||
| 57 | |||
| 58 | if (ref($text) eq 'SCALAR') { | ||
| 59 | $config = $scoped->parse(text => $$text); | ||
| 60 | } | ||
| 61 | else { | ||
| 62 | $config = $scoped->parse(text => $text); | ||
| 63 | } | ||
| 64 | |||
| 65 | $self->{spec} = $self->create_specification($config); | ||
| 66 | |||
| 67 | return $self->{spec}; | ||
| 68 | } | ||
| 69 | |||
| 70 | =head2 parse_file FILENAME | ||
| 71 | |||
| 72 | Parses text from file FILENAME and returns L<Template::Flute::Specification> | ||
| 73 | object in case of success. | ||
| 74 | |||
| 75 | =cut | ||
| 76 | |||
| 77 | sub parse_file { | ||
| 78 | my ($self, $file) = @_; | ||
| 79 | my ($scoped, $config, $key, $value, %list); | ||
| 80 | |||
| 81 | # create Config::Scoped parser and parse file | ||
| 82 | $scoped = new Config::Scoped(file => $file); | ||
| 83 | $config = $scoped->parse(); | ||
| 84 | |||
| 85 | $self->{spec} = $self->create_specification($config); | ||
| 86 | |||
| 87 | return $self->{spec}; | ||
| 88 | } | ||
| 89 | |||
| 90 | =head2 create_specification [ HASHREF ] | ||
| 91 | |||
| 92 | Takes a L<Config::Scoped> hash reference and returns a | ||
| 93 | L<Template::Flute::Specification> object. Mostly used for parse and | ||
| 94 | parse_file methods. | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | sub create_specification { | ||
| 99 | my ($self, $config) = @_; | ||
| 100 | my ($spec, $scoped, $key, $value, %list, $encoding); | ||
| 101 | |||
| 102 | # specification object | ||
| 103 | $spec = new Template::Flute::Specification; | ||
| 104 | |||
| 105 | if ($encoding = $config->{specification}->{encoding}) { | ||
| 106 | $spec->encoding($encoding); | ||
| 107 | } | ||
| 108 | |||
| 109 | # lists | ||
| 110 | while (($key, $value) = each %{$config->{list}}) { | ||
| 111 | $value->{name} = $key; | ||
| 112 | $list{$key} = $value; | ||
| 113 | } | ||
| 114 | |||
| 115 | # adding list tokens: params, separators, inputs and filters | ||
| 116 | my ($list); | ||
| 117 | |||
| 118 | for my $cname (qw/param input filter separator/) { | ||
| 119 | while (($key, $value) = each %{$config->{$cname}}) { | ||
| 120 | $list = delete $value->{list}; | ||
| 121 | $value->{name} = $key; | ||
| 122 | |||
| 123 | if ($list) { | ||
| 124 | if (exists $list{$list}) { | ||
| 125 | push @{$list{$list}->{$cname}}, {%$value}; | ||
| 126 | } | ||
| 127 | else { | ||
| 128 | die "List missing for $cname $key."; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | else { | ||
| 132 | die "No list assigned to $cname $key."; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | # adding other tokens: values and i18n | ||
| 138 | for my $cname (qw/value i18n/) { | ||
| 139 | while (($key, $value) = each %{$config->{$cname}}) { | ||
| 140 | $value->{name} = $key; | ||
| 141 | |||
| 142 | if ($cname eq 'value') { | ||
| 143 | $spec->value_add({value => $value}); | ||
| 144 | } | ||
| 145 | elsif ($cname eq 'i18n') { | ||
| 146 | $spec->i18n_add({i18n => $value}); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | while (($key, $value) = each %{$config->{list}}) { | ||
| 152 | $spec->list_add({list => $value, | ||
| 153 | param => $value->{param}, | ||
| 154 | separator => $value->{separator}, | ||
| 155 | input => $value->{input}, | ||
| 156 | filter => $value->{filter}, | ||
| 157 | }); | ||
| 158 | } | ||
| 159 | |||
| 160 | return $spec; | ||
| 161 | } | ||
| 162 | |||
| 163 | =head2 error | ||
| 164 | |||
| 165 | Returns last error. | ||
| 166 | |||
| 167 | =cut | ||
| 168 | |||
| 169 | sub error { | ||
| 170 | my ($self) = @_; | ||
| 171 | |||
| 172 | if (@{$self->{errors}}) { | ||
| 173 | return $self->{errors}->[0]->{error}; | ||
| 174 | } | ||
| 175 | } | ||
| 176 | |||
| 177 | sub _add_error { | ||
| 178 | my ($self, @args) = @_; | ||
| 179 | my (%error); | ||
| 180 | |||
| 181 | %error = @args; | ||
| 182 | |||
| 183 | unshift (@{$self->{errors}}, \%error); | ||
| 184 | } | ||
| 185 | |||
| 186 | =head1 AUTHOR | ||
| 187 | |||
| 188 | Stefan Hornburg (Racke), <racke@linuxia.de> | ||
| 189 | |||
| 190 | =head1 LICENSE AND COPYRIGHT | ||
| 191 | |||
| 192 | Copyright 2010-2019 Stefan Hornburg (Racke) <racke@linuxia.de>. | ||
| 193 | |||
| 194 | This program is free software; you can redistribute it and/or modify it | ||
| 195 | under the terms of either: the GNU General Public License as published | ||
| 196 | by the Free Software Foundation; or the Artistic License. | ||
| 197 | |||
| 198 | See http://dev.perl.org/licenses/ for more information. | ||
| 199 | |||
| 200 | =cut | ||
| 201 | |||
| 202 | 1; |