File Coverage

blib/lib/Forest/Tree/Reader/SimpleTextFile.pm
Criterion Covered Total %
statement 20 21 95.2
branch n/a
condition n/a
subroutine 7 8 87.5
pod 2 3 66.6
total 29 32 90.6


line stmt bran cond sub pod time code
1             package Forest::Tree::Reader::SimpleTextFile;
2 10     10   18443 use Moose;
  10         26  
  10         90  
3              
4             our $VERSION = '0.10';
5             our $AUTHORITY = 'cpan:STEVAN';
6              
7 10     10   116046 use Forest::Tree::Builder::SimpleTextFile;
  10         41  
  10         3879  
8              
9             with qw(Forest::Tree::Reader Forest::Tree::Constructor); # see new_subtree_callback below
10              
11             # FIXME these are for compat... remove them?
12              
13             has 'tab_width' => (
14             is => 'rw',
15             isa => 'Int',
16             default => 4
17             );
18              
19             has 'parser' => (
20             is => 'rw',
21             isa => 'CodeRef',
22             lazy => 1,
23             builder => 'build_parser',
24             );
25              
26             sub build_parser {
27             return sub {
28 71     71   349 my ($self, $line) = @_;
29 71         399 my ($indent, $node) = ($line =~ /^(\s*)(.*)$/);
30 71         3329 my $depth = ((length $indent) / $self->tab_width);
31 71         490 return ($depth, $node);
32             }
33 7     7 1 372 }
34              
35 0     0 0 0 sub parse_line { $_[0]->parser->(@_) }
36              
37             # compat endscreate_new_subtree(@_);},
38              
39             sub read {
40 8     8 1 3061 my ($self, $fh) = @_;
41              
42             my $builder = Forest::Tree::Builder::SimpleTextFile->new(
43             tree_class => ref( $self->tree ),
44             tab_width => $self->tab_width,
45             parser => $self->parser,
46             fh => $fh,
47              
48             # since it's possible to subclass reader and implement this method, we
49             # include Forest::Tree::Constructor into this class as well, and make
50             # the builder use that definition (which under normal circumstances
51             # will be the same, Forest::Tree::Constructor::create_new_subtree)
52             new_subtree_callback => sub {
53 76     76   197 my ( $builder, @args ) = @_;
54 76         331 $self->create_new_subtree(@args);
55             },
56 8         362 );
57              
58 8         23 $self->tree->add_child($_) for @{ $builder->subtrees };
  8         374  
59             }
60              
61             __PACKAGE__->meta->make_immutable;
62              
63 10     10   105 no Moose; 1;
  10         22  
  10         87  
64              
65             __END__
66              
67             =pod
68              
69             =head1 NAME
70              
71             Forest::Tree::Reader::SimpleTextFile - A reader for Forest::Tree heirarchies
72              
73             =head1 DESCRIPTION
74              
75             B<This module is deprecated>. You should use L<Forest::Tree::Builder::SimpleTextFile> instead.
76              
77             This reads simple F<.tree> files, which are basically the tree represented
78             as a tabbed heirarchy.
79              
80             =head1 ATTRIBUTES
81              
82             =over 4
83              
84             =item I<tab_width>
85              
86             =back
87              
88             =head1 METHODS
89              
90             =over 4
91              
92             =item B<read ($fh)>
93              
94             =item B<build_parser>
95              
96             =item B<create_new_subtree (%options)>
97              
98             =back
99              
100             =head1 BUGS
101              
102             All complex software has bugs lurking in it, and this module is no
103             exception. If you find a bug please either email me, or add the bug
104             to cpan-RT.
105              
106             =head1 AUTHOR
107              
108             Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
109              
110             =head1 COPYRIGHT AND LICENSE
111              
112             Copyright 2008-2014 Infinity Interactive, Inc.
113              
114             L<http://www.iinteractive.com>
115              
116             This library is free software; you can redistribute it and/or modify
117             it under the same terms as Perl itself.
118              
119             =cut