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 0.103006;
2             # ABSTRACT: it turns a Pod::Eventual event stream into objects
3              
4 12     12   320027 use Moose;
  12         1148425  
  12         83  
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   72935 use namespace::autoclean;
  12         19758  
  12         70  
18              
19 12     12   1841 use Pod::Elemental::Element::Generic::Blank;
  12         29  
  12         386  
20 12     12   1486 use Pod::Elemental::Element::Generic::Command;
  12         29  
  12         1455  
21 12     12   5084 use Pod::Elemental::Element::Generic::Nonpod;
  12         35  
  12         383  
22 12     12   1363 use Pod::Elemental::Element::Generic::Text;
  12         31  
  12         3113  
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 8150 my ($self, $events) = @_;
35             return [ map {
36 14 50       40 Carp::croak("not a valid event") unless ref $_;
  344         686  
37              
38 344         655 my $class = $self->element_class_for_event($_);
39              
40             my %guts = (
41             content => $_->{content},
42             start_line => $_->{start_line},
43              
44 344 100       1397 ($_->{type} eq 'command' ? (command => $_->{command}) : ()),
45             );
46              
47 344         10498 $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   924 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 588 my ($self, $event) = @_;
69 344         509 my $t = $event->{type};
70 344         538 my $class_for = $self->__class_for;
71              
72 344 50       623 Carp::croak "unknown event type: $t" unless exists $class_for->{ $t };
73              
74 344         753 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.103006
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 PERL VERSION
105              
106             This library should run on perls released even a long time ago. It should work
107             on any version of perl released in the last five years.
108              
109             Although it may work on older versions of perl, no guarantee is made that the
110             minimum required version will not be increased. The version may be increased
111             for any reason, and there is no promise that patches will be accepted to lower
112             the minimum required perl.
113              
114             =head1 METHODS
115              
116             =head2 objectify_events
117              
118             my $elements = $objectifier->objectify_events(\@events);
119              
120             Given an arrayref of Pod events, this method returns an arrayref of objects
121             formed from the event stream.
122              
123             =head2 element_class_for_event
124              
125             This method returns the name of the class to be used for the given event.
126              
127             =head1 AUTHOR
128              
129             Ricardo SIGNES <cpan@semiotic.systems>
130              
131             =head1 COPYRIGHT AND LICENSE
132              
133             This software is copyright (c) 2022 by Ricardo SIGNES.
134              
135             This is free software; you can redistribute it and/or modify it under
136             the same terms as the Perl 5 programming language system itself.
137              
138             =cut