File Coverage

blib/lib/PPIx/EditorTools/ReturnObject.pm
Criterion Covered Total %
statement 28 30 93.3
branch 8 10 80.0
condition 4 9 44.4
subroutine 8 8 100.0
pod 4 4 100.0
total 52 61 85.2


line stmt bran cond sub pod time code
1             package PPIx::EditorTools::ReturnObject;
2             our $AUTHORITY = 'cpan:YANICK';
3             # ABSTRACT: Simple object to return values from PPIx::EditorTools
4             $PPIx::EditorTools::ReturnObject::VERSION = '0.20';
5 11     11   207 use 5.008;
  11         33  
6 11     11   48 use strict;
  11         19  
  11         198  
7 11     11   49 use warnings;
  11         24  
  11         251  
8 11     11   53 use Carp;
  11         16  
  11         2810  
9              
10              
11             sub new {
12 22     22 1 1088 my $class = shift;
13 22   33     262 return bless {@_}, ref($class) || $class;
14             }
15              
16             sub element {
17 14     14 1 4196 my ($self) = @_;
18              
19             # If element is a code ref, run the code once then cache the
20             # result
21 14 100 33     106 if ( exists $self->{element}
      66        
22             and ref( $self->{element} )
23             and ref( $self->{element} ) eq 'CODE' )
24             {
25 2         7 $self->{element} = $self->{element}->(@_);
26             }
27              
28 14         121 return $self->{element};
29             }
30              
31             sub ppi {
32 3     3 1 7 my ( $self ) = @_;
33              
34             # $self->{ppi} = $doc if $doc; # TODO: and isa?
35              
36 3 100       15 return $self->{ppi} if $self->{ppi};
37              
38 2 50       5 if ( $self->{code} ) {
39 2         3 my $code = $self->{code};
40 2         8 $self->{ppi} = PPI::Document->new( \$code );
41 2         15136 return $self->{ppi};
42             }
43              
44 0         0 return;
45             }
46              
47             sub code {
48 16     16 1 1040 my ( $self ) = @_;
49              
50             # $self->{code} = $doc if $doc;
51              
52 16 100       60 return $self->{code} if $self->{code};
53              
54 12 50       39 if ( $self->{ppi} ) {
55 12         44 $self->{code} = $self->{ppi}->serialize;
56 12         8242 return $self->{code};
57             }
58              
59 0           return;
60             }
61              
62             1;
63              
64             =pod
65              
66             =encoding UTF-8
67              
68             =head1 NAME
69              
70             PPIx::EditorTools::ReturnObject - Simple object to return values from PPIx::EditorTools
71              
72             =head1 VERSION
73              
74             version 0.20
75              
76             =head1 SYNOPSIS
77              
78             my $brace = PPIx::EditorTools::FindUnmatchedBrace->new->find(
79             code => "package TestPackage;\nsub x { 1;\n"
80             );
81             my $location = $brace->element->location;
82             my $ppi = $brace->element->ppi;
83              
84             =head1 DESCRIPTION
85              
86             Retuning a simple C<PPI::Element> from many of the C<PPIx::EditorTools>
87             methods often results in the loss of the overall context for that element.
88             C<PPIx::EditorTools::ReturnObject> provides an object that can be passed
89             around which retains the overall context.
90              
91             For example, in C<PPIx::EditorTools::FindUnmatchedBrace> if the unmatched
92             brace were returned by its C<PPI::Structure::Block> the containing
93             C<PPI::Document> is likely to go out of scope, thus the C<location>
94             method no longer returns a valid location (rather it returns undef).
95             Using the C<ReturnObject> preserves the C<PPI::Document> and the containing
96             context.
97              
98             =head1 METHODS
99              
100             =over 4
101              
102             =item new()
103              
104             Constructor which should be used by C<PPIx::EditorTools>. Accepts the following
105             named parameters:
106              
107             =over 4
108              
109             =item ppi
110              
111             A C<PPI::Document> representing the (possibly modified) code.
112              
113             =item code
114              
115             A string representing the (possibly modified) code.
116              
117             =item element
118              
119             A C<PPI::Element> or a subclass thereof representing the interesting element.
120              
121             =back
122              
123             =item ppi
124              
125             Accessor to retrieve the C<PPI::Document>. May create the C<PPI::Document>
126             from the $code string (lazily) if needed.
127              
128             =item code
129              
130             Accessor to retrieve the string representation of the code. May be retrieved
131             from the C<PPI::Document> via the serialize method (lazily) if needed.
132              
133             =back
134              
135             =head1 SEE ALSO
136              
137             C<PPIx::EditorTools>, L<App::EditorTools>, L<Padre>, and L<PPI>.
138              
139             =head1 AUTHORS
140              
141             =over 4
142              
143             =item *
144              
145             Steffen Mueller C<smueller@cpan.org>
146              
147             =item *
148              
149             Mark Grimes C<mgrimes@cpan.org>
150              
151             =item *
152              
153             Ahmad M. Zawawi <ahmad.zawawi@gmail.com>
154              
155             =item *
156              
157             Gabor Szabo <gabor@szabgab.com>
158              
159             =item *
160              
161             Yanick Champoux <yanick@cpan.org>
162              
163             =back
164              
165             =head1 COPYRIGHT AND LICENSE
166              
167             This software is copyright (c) 2017, 2014, 2012 by The Padre development team as listed in Padre.pm..
168              
169             This is free software; you can redistribute it and/or modify it under
170             the same terms as the Perl 5 programming language system itself.
171              
172             =cut
173              
174             __END__
175              
176              
177             # Copyright 2008-2009 The Padre development team as listed in Padre.pm.
178             # LICENSE
179             # This program is free software; you can redistribute it and/or
180             # modify it under the same terms as Perl 5 itself.