File Coverage

blib/lib/Pod/Elemental/Objectifier.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


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.103004';
4 3     3   193753 use Moose;
  0            
  0            
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             use namespace::autoclean;
18              
19             use Pod::Elemental::Element::Generic::Blank;
20             use Pod::Elemental::Element::Generic::Command;
21             use Pod::Elemental::Element::Generic::Nonpod;
22             use Pod::Elemental::Element::Generic::Text;
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             my ($self, $events) = @_;
35             return [ map {
36             Carp::croak("not a valid event") unless ref $_;
37              
38             my $class = $self->element_class_for_event($_);
39              
40             my %guts = (
41             content => $_->{content},
42             start_line => $_->{start_line},
43              
44             ($_->{type} eq 'command' ? (command => $_->{command}) : ()),
45             );
46              
47             $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             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             my ($self, $event) = @_;
69             my $t = $event->{type};
70             my $class_for = $self->__class_for;
71              
72             Carp::croak "unknown event type: $t" unless exists $class_for->{ $t };
73              
74             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.103004
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) 2014 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