File Coverage

blib/lib/Path/Resolver/Resolver/Hash.pm
Criterion Covered Total %
statement 13 18 72.2
branch n/a
condition n/a
subroutine 5 6 83.3
pod 0 1 0.0
total 18 25 72.0


line stmt bran cond sub pod time code
1             package Path::Resolver::Resolver::Hash 3.100455;
2             # ABSTRACT: glorified hash lookup
3 1     1   873 use Moose;
  1         383273  
  1         6  
4             with 'Path::Resolver::Role::Resolver';
5              
6 1     1   6634 use namespace::autoclean;
  1         6665  
  1         4  
7              
8 1     1   54 use Moose::Util::TypeConstraints;
  1         2  
  1         9  
9 1     1   2282 use Path::Resolver::SimpleEntity;
  1         3  
  1         280  
10              
11             #pod =head1 SYNOPSIS
12             #pod
13             #pod my $resolver = Path::Resolver::Resolver::Hash->new({
14             #pod hash => {
15             #pod foo => {
16             #pod 'bar.txt' => "This is the content.\n",
17             #pod },
18             #pod }
19             #pod });
20             #pod
21             #pod my $simple_entity = $resolver->entity_at('foo/bar.txt');
22             #pod
23             #pod This resolver looks through a has to find string content. Path parts are used
24             #pod to drill down through the hash. The final result must be a string. Unless you
25             #pod really know what you're doing, it should be a byte string and not a character
26             #pod string.
27             #pod
28             #pod The native type of the Hash resolver is a class type of
29             #pod Path::Resolver::SimpleEntity. There is no default converter.
30             #pod
31             #pod =cut
32              
33 3     3 0 9 sub native_type { class_type('Path::Resolver::SimpleEntity') }
34              
35             #pod =attr hash
36             #pod
37             #pod This is a hash reference in which lookups are performed. References to copies
38             #pod of the string values are returned.
39             #pod
40             #pod =cut
41              
42             has hash => (
43             is => 'ro',
44             isa => 'HashRef',
45             required => 1,
46             );
47              
48             sub __str_path {
49 0     0     my ($self, $path) = @_;
50              
51 0           my $str = join '/', map { my $part = $_; $part =~ s{/}{\\/}g; $part } @$path;
  0            
  0            
  0            
52             }
53              
54             sub entity_at {
55             my ($self, $path) = @_;
56              
57             my @path = @$path;
58             shift @path if $path[0] eq '';
59              
60             my $cwd = $self->hash;
61             my @path_so_far;
62             while (defined (my $name = shift @path)) {
63             push @path_so_far, $name;
64              
65             my $entry = $cwd->{ $name};
66              
67             if (! @path) {
68             return unless defined $entry;
69              
70             # XXX: Should we return because we're at a notional -d instead of -f?
71             return if ref $entry;
72              
73             return Path::Resolver::SimpleEntity->new({ content_ref => \$entry });
74             }
75              
76             return unless ref $entry and ref $entry eq 'HASH';
77              
78             $cwd = $entry;
79             }
80              
81             Carp::confess("this should never be reached -- rjbs, 2009-04-28")
82             }
83              
84             1;
85              
86             __END__
87              
88             =pod
89              
90             =encoding UTF-8
91              
92             =head1 NAME
93              
94             Path::Resolver::Resolver::Hash - glorified hash lookup
95              
96             =head1 VERSION
97              
98             version 3.100455
99              
100             =head1 SYNOPSIS
101              
102             my $resolver = Path::Resolver::Resolver::Hash->new({
103             hash => {
104             foo => {
105             'bar.txt' => "This is the content.\n",
106             },
107             }
108             });
109              
110             my $simple_entity = $resolver->entity_at('foo/bar.txt');
111              
112             This resolver looks through a has to find string content. Path parts are used
113             to drill down through the hash. The final result must be a string. Unless you
114             really know what you're doing, it should be a byte string and not a character
115             string.
116              
117             The native type of the Hash resolver is a class type of
118             Path::Resolver::SimpleEntity. There is no default converter.
119              
120             =head1 PERL VERSION
121              
122             This library should run on perls released even a long time ago. It should work
123             on any version of perl released in the last five years.
124              
125             Although it may work on older versions of perl, no guarantee is made that the
126             minimum required version will not be increased. The version may be increased
127             for any reason, and there is no promise that patches will be accepted to lower
128             the minimum required perl.
129              
130             =head1 ATTRIBUTES
131              
132             =head2 hash
133              
134             This is a hash reference in which lookups are performed. References to copies
135             of the string values are returned.
136              
137             =head1 AUTHOR
138              
139             Ricardo Signes <cpan@semiotic.systems>
140              
141             =head1 COPYRIGHT AND LICENSE
142              
143             This software is copyright (c) 2022 by Ricardo Signes.
144              
145             This is free software; you can redistribute it and/or modify it under
146             the same terms as the Perl 5 programming language system itself.
147              
148             =cut