File Coverage

blib/lib/Pod/Elemental/Objectifier.pm
Criterion Covered Total %
statement 30 30 100.0
branch 4 6 66.6
condition n/a
subroutine 9 9 100.0
pod 2 2 100.0
total 45 47 95.7


line stmt bran cond sub pod time code
1             package Pod::Elemental::Objectifier;
2             # ABSTRACT: it turns a Pod::Eventual event stream into objects
3             $Pod::Elemental::Objectifier::VERSION = '0.103005';
4 12     12   343409 use Moose;
  12         1341547  
  12         90  
5              
6             #pod =head1 OVERVIEW
7             #pod
8             #pod An objectifier is responsible for taking the events produced by
9             #pod L<Pod::Eventual|Pod::Eventual> and converting them into objects that perform
10             #pod the Pod::Elemental::Paragraph role.
11             #pod
12             #pod In general, it does this by producing a sequence of element objects in the
13             #pod Pod::Elemental::Element::Generic namespace.
14             #pod
15             #pod =cut
16              
17 12     12   85512 use namespace::autoclean;
  12         24335  
  12         147  
18              
19 12     12   2251 use Pod::Elemental::Element::Generic::Blank;
  12         40  
  12         412  
20 12     12   1797 use Pod::Elemental::Element::Generic::Command;
  12         32  
  12         1797  
21 12     12   6082 use Pod::Elemental::Element::Generic::Nonpod;
  12         44  
  12         449  
22 12     12   1527 use Pod::Elemental::Element::Generic::Text;
  12         34  
  12         3660  
23              
24             #pod =method objectify_events
25             #pod
26             #pod my $elements = $objectifier->objectify_events(\@events);
27             #pod
28             #pod Given an arrayref of Pod events, this method returns an arrayref of objects
29             #pod formed from the event stream.
30             #pod
31             #pod =cut
32              
33             sub objectify_events {
34 14     14 1 9969 my ($self, $events) = @_;
35             return [ map {
36 14 50       121 Carp::croak("not a valid event") unless ref $_;
  344         784  
37              
38 344         786 my $class = $self->element_class_for_event($_);
39              
40             my %guts = (
41             content => $_->{content},
42             start_line => $_->{start_line},
43              
44 344 100       1326 ($_->{type} eq 'command' ? (command => $_->{command}) : ()),
45             );
46              
47 344         12432 $class->new(\%guts);
48             } @$events ];
49             }
50              
51              
52             #pod =method element_class_for_event
53             #pod
54             #pod This method returns the name of the class to be used for the given event.
55             #pod
56             #pod =cut
57              
58             sub __class_for {
59             return {
60 344     344   1092 blank => 'Pod::Elemental::Element::Generic::Blank',
61             command => 'Pod::Elemental::Element::Generic::Command',
62             nonpod => 'Pod::Elemental::Element::Generic::Nonpod',
63             text => 'Pod::Elemental::Element::Generic::Text',
64             };
65             }
66              
67             sub element_class_for_event {
68 344     344 1 658 my ($self, $event) = @_;
69 344         591 my $t = $event->{type};
70 344         641 my $class_for = $self->__class_for;
71              
72 344 50       738 Carp::croak "unknown event type: $t" unless exists $class_for->{ $t };
73              
74 344         864 return $class_for->{ $t };
75             }
76              
77             __PACKAGE__->meta->make_immutable;
78              
79             1;
80              
81             __END__
82              
83             =pod
84              
85             =encoding UTF-8
86              
87             =head1 NAME
88              
89             Pod::Elemental::Objectifier - it turns a Pod::Eventual event stream into objects
90              
91             =head1 VERSION
92              
93             version 0.103005
94              
95             =head1 OVERVIEW
96              
97             An objectifier is responsible for taking the events produced by
98             L<Pod::Eventual|Pod::Eventual> and converting them into objects that perform
99             the Pod::Elemental::Paragraph role.
100              
101             In general, it does this by producing a sequence of element objects in the
102             Pod::Elemental::Element::Generic namespace.
103              
104             =head1 METHODS
105              
106             =head2 objectify_events
107              
108             my $elements = $objectifier->objectify_events(\@events);
109              
110             Given an arrayref of Pod events, this method returns an arrayref of objects
111             formed from the event stream.
112              
113             =head2 element_class_for_event
114              
115             This method returns the name of the class to be used for the given event.
116              
117             =head1 AUTHOR
118              
119             Ricardo SIGNES <rjbs@cpan.org>
120              
121             =head1 COPYRIGHT AND LICENSE
122              
123             This software is copyright (c) 2020 by Ricardo SIGNES.
124              
125             This is free software; you can redistribute it and/or modify it under
126             the same terms as the Perl 5 programming language system itself.
127              
128             =cut