Mecabist/Document.pm untested

filter: Statementstmt 0.0% 0 / 36 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 59 CC 5 SCAR 19.5
CC
5
Cov
0%
each
12.0
join
12.0
add
2.0
CRAP 7.0
1 package Text::Mecabist::Document;
2 use strict;
3 use warnings;
4  
5 use overload (
6 q{""} => 'stringify',
7 fallback => 1,
8 );
9  
10 use Moo;
11  
12 has count => (
13 is => 'rw',
14 default => 0,
15 );
16  
17 has nodes => (
18 is => 'ro',
19 default => sub { [ ] },
20 );
21  
22 sub add {
23 my ($self, $node) = @_;
24 $node->doc($self);
25 $node->index($self->count);
26 $self->count(push @{ $self->nodes }, $node);
27 }
28  
29 sub each {
30 my ($self, $cb) = @_;
31 for my $node (@{ $self->nodes }) {
32 next if $node->skip;
33 $cb->($node);
34 last if $node->last;
35 }
36 }
37  
38 sub stringify {
39 my ($self) = @_;
40 $self->join('text');
41 }
42  
43 sub join {
44 my ($self, $key) = @_;
45
46 my @r;
47 for my $node (@{ $self->nodes }) {
48 next if $node->skip;
49 push @r, $node->$key // "";
50 last if $node->last;
51 }
52  
53 join "", @r;
54 }
55  
56 1;