File Coverage

blib/lib/Datify/Path.pm
Criterion Covered Total %
statement 113 115 98.2
branch 38 58 65.5
condition 15 25 60.0
subroutine 20 20 100.0
pod 1 1 100.0
total 187 219 85.3


line stmt bran cond sub pod time code
1 1     1   7162 use v5.14;
  1         4  
2 1     1   9 use warnings;
  1         2  
  1         49  
3              
4             package Datify::Path v0.20.052;
5             # ABSTRACT: Describe structures like filesystem paths.
6             # VERSION
7              
8 1     1   6 use Carp (); #qw( carp croak );
  1         2  
  1         13  
9 1     1   607 use Datify (); #qw( self _internal );
  1         4  
  1         36  
10 1     1   8 use List::Util (); #qw( reduce );
  1         2  
  1         16  
11 1     1   5 use Scalar::Util (); #qw( blessed refaddr reftype );
  1         3  
  1         20  
12 1     1   4 use String::Tools qw( subst );
  1         2  
  1         82  
13              
14 1     1   570 use parent 'Datify';
  1         289  
  1         7  
15              
16             ### Public methods ###
17              
18              
19             ### Constructor ###
20              
21              
22              
23             ### Accessor ###
24              
25              
26              
27              
28              
29             ### Setter ###
30              
31              
32              
33              
34             sub pathify {
35 194 50   194 1 1232 return unless defined( my $wantarray = wantarray );
36 194         346 my $self = &self;
37 194 50       488 local $_ = @_ == 0 ? $_ : @_ == 1 ? shift : \@_;
    50          
38              
39 194   100     416 my $values = $self->_cache_get($_) // [ $self->_scalar($_) ];
40 190 100       1008 if ( $self->_internal ) {
41 186         350 $self->_cache_add( $_ => $values );
42             } else {
43 4         18 $values = [ map $self->_flatten, @$values ];
44 4         362 $self->_cache_reset();
45             }
46 190 50       599 return $wantarray ? @$values : $values;
47             }
48              
49             ### Private Methods ###
50             ### Do not use these methods outside of this package,
51             ### they are subject to change or disappear at any time.
52             *self = \&Datify::self;
53             sub _settings() {
54 876 50   876   1607 Carp::croak('Illegal use of private method') unless $_[0]->_internal;
55 876         2723 \state %SETTINGS;
56             }
57              
58              
59             __PACKAGE__->set(
60             datify_options => {},
61             );
62              
63             sub _datify {
64 169     169   283 my $self = &self;
65 169         354 my $datify = $self->get('_datify');
66 169 50       323 if ( not $datify ) {
67 169   50     244 $datify = Datify->new( %{ $self->get('datify_options') // {} } );
  169         310  
68 169         378 $self->set( _datify => $datify );
69             }
70 169         371 return $datify;
71             }
72              
73             __PACKAGE__->set(
74             statement => '$key = $value',
75             );
76              
77             sub _flatten {
78 340     340   7956 my $self = &self;
79 340 100       746 local $_ = shift if @_;
80 340         583 my $ref = Scalar::Util::reftype($_);
81 340 50 33     1207 my ( $key, $value ) = $ref && $ref eq 'ARRAY' ? @$_ : ($_);
82              
83 340 100       551 if ( defined $value ) {
84 316         535 $ref = Scalar::Util::reftype($value);
85 316         576 my $statement = $self->get('statement');
86 316 100       642 if ( not $ref ) {
    50          
    0          
87 114         224 return subst(
88             $statement,
89             key => $key,
90             value => $self->_datify->keyify($value)
91             );
92             } elsif ( $ref eq 'ARRAY' ) {
93 202         383 return $key . $self->_flatten($value);
94             #} elsif ( $ref eq 'HASH' ) {
95             # return subst(
96             # $self->get('object'),
97             # class => $value->{class},
98             # key => $key,
99             # value => $value->{value}
100             # );
101             } elsif ( $ref eq 'SCALAR' ) {
102 0         0 return subst(
103             $statement,
104             key => $key,
105             value => $$value
106             );
107             } else {
108 0         0 die 'Unsure of how to handle ', $ref;
109             }
110             } else {
111 24         98 return $key;
112             }
113             }
114              
115             __PACKAGE__->set(
116             list_count => '[$i/$n]',
117             );
118              
119             sub _array {
120 25     25   44 my $self = &self;
121 25 50       57 local $_ = shift if @_;
122              
123 25         43 my $datify = $self->_datify;
124 25         61 my $list_count = $self->get('list_count');
125 25         76 my $size = $datify->numify( scalar @$_ );
126 25 100       80 return [ subst( $list_count, i => 0, n => 0 ), undef ]
127             if ( $size eq '0' );
128              
129 19         77 my $format = subst(
130             $list_count,
131             i => '%' . length($size) . 's',
132             n => $size
133             );
134              
135 19         1538 my @structure;
136 19         81 while ( my ( $i, $v ) = each @$_ ) {
137 101         260 my $key = sprintf( $format, $datify->numify( 1 + $i ) );
138 101         296 $self->_push_position($key);
139 101         206 push @structure, map { [ $key, $_ ] } $self->pathify($v);
  170         419  
140 100         228 $self->_pop_position();
141             }
142 18         72 return @structure;
143             }
144              
145             __PACKAGE__->set(
146             path_separator => '/',
147             );
148              
149             sub _hash {
150 36     36   70 my $self = &self;
151 36 50       80 local $_ = shift if @_;
152              
153 36         90 my $path_separator = $self->get('path_separator');
154 36 100       130 return [ $path_separator, undef ]
155             if ( 0 == scalar keys %$_ );
156              
157 30         59 my $datify = $self->_datify;
158 30         54 my @structure;
159 30         82 foreach my $k ( $datify->hashkeys($_) ) {
160 87         198 my $key = $path_separator . $datify->keyify($k);
161 87         259 $self->_push_position($key);
162 87         213 push @structure, map { [ $key, $_ ] } $self->pathify( $_->{$k} );
  166         385  
163 86         206 $self->_pop_position();
164             }
165 29         124 return @structure;
166             }
167              
168             # TODO:
169             #{
170             # foo => bless(
171             # {
172             # alpha => {},
173             # bravo => [],
174             # charlie => 123,
175             # },
176             # 'Foo::Bar'
177             # )
178             #}
179             # /foo/Foo::Bar=alpha/
180             # /foo/Foo::Bar=bravo[0/0]
181             # /foo/Foo::Bar=charlie = 123
182             #sub _object {
183             #}
184              
185             sub _scalar {
186 174     174   332 my $self = &self;
187 174 50       380 local $_ = shift if @_;
188              
189 174 100       334 return undef unless defined;
190              
191             #if ( defined( my $blessed = Scalar::Util::blessed($_) ) ) {
192             # return $blessed eq 'Regexp' ? $self->_scalar("$_")
193             # : $self->_object($_);
194             #}
195              
196 164         310 my $ref = Scalar::Util::reftype $_;
197             return
198 164 0 0     578 not($ref) ? $_
    0          
    0          
    50          
    100          
    100          
199             : $ref eq 'ARRAY' ? $self->_array($_)
200             : $ref eq 'HASH' ? $self->_hash($_)
201             : $ref eq 'REGEXP' ? $self->_scalarify("$_")
202             : $ref eq 'SCALAR' ? $self->_scalarify($$_)
203             : $ref eq 'REF' && 'REF' ne Scalar::Util::reftype($$_)
204             ? $self->pathify($$_)
205             : die 'Cannot handle ', $ref;
206             }
207              
208             __PACKAGE__->set(
209             _cache_hit => 1,
210             nested => '$key$subkey',
211             );
212              
213             sub _cache_position {
214 63     63   93 my $self = shift;
215              
216 63         124 my $nest = $self->get('nested');
217             my $pos = List::Util::reduce(
218 12     12   137 sub { subst( $nest, key => $a, subkey => $b ) },
219 63   100     253 @{ $self->{_position} //= [] }
  63         348  
220             );
221 63   100     2038 return $pos // '';
222             }
223             sub _cache_add {
224 186     186   269 my $self = shift;
225 186         261 my $ref = shift;
226 186         229 my $value = shift;
227              
228 186 100       422 return $self unless my $refaddr = Scalar::Util::refaddr $ref;
229 73   50     166 my $_cache = $self->{_cache} //= {};
230 73   50     168 my $entry = $_cache->{$refaddr} //= [ [ \$self->_cache_position ] ];
231 73 100       166 push @$entry, $value if @$entry == $self->get('_cache_hit');
232              
233 73         136 return $self;
234             }
235             sub _cache_get {
236 194     194   266 my $self = shift;
237 194         238 my $item = shift;
238              
239 194 100       609 return unless my $refaddr = Scalar::Util::refaddr $item;
240              
241 81   100     196 my $_cache = $self->{_cache} //= {};
242 81 100       194 if ( my $entry = $_cache->{$refaddr} ) {
243 20         41 my $repr = $self->get('_cache_hit');
244 20   66     69 return $entry->[$repr]
245             // Carp::croak( 'Recursive structures not allowed at ',
246             $self->_cache_position );
247             } else {
248             # Pre-populate the cache, so that we can check for loops
249 61         122 $_cache->{$refaddr} = [ [ \$self->_cache_position ] ];
250 61         243 return;
251             }
252             }
253             sub _cache_reset {
254 4     4   8 my $self = shift;
255 4   50     8 %{ $self->{_cache} //= {} } = ();
  4         100  
256 4         9 delete $self->{_datify};
257 4         8 return $self;
258             }
259              
260              
261              
262             1;
263              
264             =pod
265              
266             =encoding UTF-8
267              
268             =head1 NAME
269              
270             Datify::Path - Describe structures like filesystem paths.
271              
272             =head1 METHODS
273              
274             =head2 C<< new( name => value, name => value, ... ) >>
275              
276             Create a C object with the following options.
277              
278             See L for a description of the options and their default values.
279              
280             =head2 C
281              
282             Determine if values exists for one or more settings.
283              
284             Can be called as a class method or an object method.
285              
286             =head2 C
287              
288             Get one or more existing values for one or more settings.
289             If passed no names, returns all parameters and values.
290              
291             Can be called as a class method or an object method.
292              
293             =head2 C<< set( name => value, name => value, ... ) >>
294              
295             Change the L settings.
296             When called as a class method, changes default options.
297             When called as an object method, changes the settings and returns a
298             new object.
299              
300             See L for a description of the options and their default values.
301              
302             B When called as a object method, this returns a new instance
303             with the values set, so you will need to capture the return if you'd like to
304             persist the change:
305              
306             $datify = $datify->set( ... );
307              
308             =head2 pathify( ... )
309              
310             =head1 BUGS
311              
312             Please report any bugs or feature requests on the bugtracker website
313             L
314              
315             When submitting a bug or request, please include a test-file or a
316             patch to an existing test-file that illustrates the bug or desired
317             feature.
318              
319             =head1 VERSION
320              
321             This document describes version v0.20.052 of this module.
322              
323             =head1 AUTHOR
324              
325             Bob Kleemann
326              
327             =head1 COPYRIGHT AND LICENSE
328              
329             This software is Copyright (c) 2014-2019 by Bob Kleemann.
330              
331             This is free software, licensed under:
332              
333             The Artistic License 2.0 (GPL Compatible)
334              
335             =cut
336              
337             __DATA__