| 1 | package Giddy::Role::DocumentLoader; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Any::Moose 'Role'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Encode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use YAML::XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | our $VERSION = "0.013_001"; | |||||||||||||||||||||||||||
| 12 | 2 | $VERSION = eval $VERSION; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | requires 'db'; | |||||||||||||||||||||||||||
| 15 | 2 | requires '_path_to'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 NAME | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | Giddy::Role::DocumentLoader - Provides document loading methods for Giddy::Collection | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 VERSION | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | version 0.013_001 | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # used internally | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | This role provides document loading capabilities to L<Giddy::Collection> and L<Giddy::Collection::InMemory>. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | Requires the attributes 'db' and '_path_to' to be implemented by consuming classes. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =head1 METHODS | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =head3 _load_document_file( $name ) | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =cut | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | sub _load_document_file { | ||||||||||||||||||||||||||
|
Subroutine
_load_document_file: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 42 | 0 | my ($self, $name) = @_; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | my $content = ''.$self->db->_repo->run('show', 'HEAD:'.$self->_path_to($name)); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | my ($yaml, $body) = ('', ''); | |||||||||||||||||||||||||||
| 47 | 0 | ▶ | if ($content && $content =~ m/\n\n/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$content and $content =~ /\n\n/
MC/DC0%0/2
$content and $content =~ /\n\n/
$content
$content =~ /\n\n/
Truth table0%0/3
A and B
A= $contentB= $content =~ /\n\n/
| |||||||||||||||||||||||||||||
| 48 | 0 | ($yaml, $body) = ($`, $'); | |||||||||||||||||||||||||||
| 49 | } else { | ||||||||||||||||||||||||||||
| 50 | 0 | $body = $content; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | return try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 54 | 0 | my $doc = Load("---\n$yaml"); | |||||||||||||||||||||||||||
| 55 | 0 | $doc->{_body} = $body; | |||||||||||||||||||||||||||
| 56 | 0 | $doc->{_name} = $name; | |||||||||||||||||||||||||||
| 57 | 0 | $doc->{_coll} = $self->path; | |||||||||||||||||||||||||||
| 58 | 0 | return $doc; | |||||||||||||||||||||||||||
| 59 | 0 | ▶ | } catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 60 | 0 | return { _body => $body, _name => $name, _coll => $self->path }; | |||||||||||||||||||||||||||
| 61 | 0 | }; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =head3 _load_document_dir( $name, [ $skip_binary ] ) | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =cut | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | sub _load_document_dir { | ||||||||||||||||||||||||||
|
Subroutine
_load_document_dir: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 69 | 0 | my ($self, $name, $skip_bin) = @_; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | my $doc; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # try to load the attributes | ||||||||||||||||||||||||||||
| 74 | 0 | my $yaml = $self->db->_repo->run('show', 'HEAD:'.$self->_path_to($name, 'attributes.yaml')); | |||||||||||||||||||||||||||
| 75 | 0 | ▶ | croak "Can't find/read attributes.yaml file of document $name." unless $yaml; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | $doc = try { Load($yaml) } catch { {} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # load child collections | ||||||||||||||||||||||||||||
| 79 | 0 | ▶ | $doc->{_has_many} = [sort grep { !$self->db->_is_document_dir($self->_path_to($name, $_)) && !$self->db->_is_static_dir($self->_path_to($name, $_)) } $self->db->_list_dirs($self->_path_to($name))]; | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->db->_is_document_dir($self->_path_to($name, $_)) || $self->db->_is_static_dir($self->_path_to($name, $_))
MC/DC0%0/2
$self->db->_is_document_dir($self->_path_to($name, $_)) || $self->db->_is_static_dir($self->_path_to($name, $_))
$self->db->_is_document_dir($self->_path_to($name, $_))
$self->db->_is_static_dir($self->_path_to($name, $_))
Truth table0%0/3
A || B
A= $self->db->_is_document_dir($self->_path_to($name, $_))B= $self->db->_is_static_dir($self->_path_to($name, $_))
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # load child documents | ||||||||||||||||||||||||||||
| 82 | 0 | $doc->{_has_one} = [sort grep { $self->db->_is_document_dir($self->_path_to($name, $_)) } $self->db->_list_dirs($self->_path_to($name))]; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # try to load binary files (unless we're skipping binary) | ||||||||||||||||||||||||||||
| 85 | 0 | ▶ | unless ($skip_bin) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 0 | foreach (grep { $_ ne 'attributes.yaml' } $self->db->_list_files($self->_path_to($name))) { | |||||||||||||||||||||||||||
| 87 | 0 | $doc->{$_} = $self->_path_to($name, $_); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | $doc->{_name} = $name | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$doc and scalar keys %$doc
MC/DC0%0/1
$doc and scalar keys %$doc
$doc
Truth table0%0/2
A
A= $doc
| |||||||||||||||||||||||||||||
| 92 | if $doc && scalar keys %$doc; | ||||||||||||||||||||||||||||
| 93 | 0 | $doc->{_coll} = $self->path; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | return $doc; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | Ido Perlmuter, C<< <ido at ido50.net> >> | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head1 BUGS | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | Please report any bugs or feature requests to C<bug-giddy at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 105 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Giddy>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 106 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | perldoc Giddy::Role::DocumentLoader | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | You can also look for information at: | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =over 4 | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =item * RT: CPAN's request tracker | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Giddy> | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | L<http://annocpan.org/dist/Giddy> | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | L<http://cpanratings.perl.org/d/Giddy> | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | L<http://search.cpan.org/dist/Giddy/> | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =back | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | Copyright 2011 Ido Perlmuter. | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 141 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||
| 142 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =cut | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 2 | 1; | |||||||||||||||||||||||||||