File Coverage

blib/lib/Siebel/Srvrmgr/ListParser/Output/ToString.pm
Criterion Covered Total %
statement 35 35 100.0
branch 5 6 83.3
condition 3 6 50.0
subroutine 7 7 100.0
pod 2 2 100.0
total 52 56 92.8


line stmt bran cond sub pod time code
1             package Siebel::Srvrmgr::ListParser::Output::ToString;
2              
3 27     27   24083 use Moose::Role;
  27         83  
  27         255  
4 27     27   148504 use Carp;
  27         62  
  27         2012  
5 27     27   151 use warnings;
  27         59  
  27         789  
6 27     27   143 use strict;
  27         59  
  27         9797  
7              
8             =head1 NAME
9              
10             Siebel::Srvrmgr::ListParser::Output::ToString - Moose role to "stringfy" objects
11              
12             =head1 SYNOPSIS
13              
14             with 'Siebel::Srvrmgr::ListParser::Output::ToString';
15              
16             =head1 DESCRIPTION
17              
18             This role will use some introspection to enable a object to return all it's attributes as a string.
19              
20             =head1 METHODS
21              
22             =head2 to_string_header
23              
24             Returns a string with all attributes values, ordered alphabetically by their respective attribute names, concatenated with a single character.
25              
26             Expects as parameter a single character to be used as field separator.
27              
28             =cut
29              
30             sub to_string_header {
31              
32 1     1 1 413 my $self = shift;
33 1         3 my $separator = shift;
34              
35 1 50 33     10 confess 'separator must be a single character'
36             unless ( ( defined($separator) ) and ( $separator =~ /^.$/ ) );
37              
38 1         4 my $meta = $self->meta;
39              
40 1         21 my $attribs_names_ref = $self->_to_string;
41              
42 1         2 return join( $separator, @{$attribs_names_ref} );
  1         6  
43              
44             }
45              
46             =head2 to_string
47              
48             Returns a string with all attributes values, ordered alphabetically by their respective attribute names, concatenated with a single character.
49              
50             Expects as parameter a single character to be used as field separator.
51              
52             =cut
53              
54             sub to_string {
55              
56 2     2 1 36 my $self = shift;
57 2         3 my $separator = shift;
58              
59 2 100 66     29 confess 'separator must be a single character'
60             unless ( ( defined($separator) ) and ( $separator =~ /^.$/ ) );
61              
62 1         7 my $meta = $self->meta;
63 1         24 my $attribs_names_ref = $self->_to_string;
64 1         2 my @values;
65              
66 1         2 foreach my $name ( @{$attribs_names_ref} ) {
  1         3  
67              
68 17         56 my $attrib = $meta->get_attribute($name);
69 17         132 my $reader = $attrib->get_read_method;
70              
71 17 100       955 push( @values, ( defined( $self->$reader ) ? $self->$reader : '' ) );
72              
73             }
74              
75 1         8 return join( $separator, @values );
76              
77             }
78              
79             sub _to_string {
80              
81 2     2   5 my $self = shift;
82              
83 2         6 my $meta = $self->meta;
84              
85 2         45 my @attribs = sort( $meta->get_attribute_list );
86              
87 2         40 return \@attribs;
88              
89             }
90              
91             =head1 AUTHOR
92              
93             Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>.
94              
95             =head1 COPYRIGHT AND LICENSE
96              
97             This software is copyright (c) 2012 of Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>.
98              
99             This file is part of Siebel Monitoring Tools.
100              
101             Siebel Monitoring Tools is free software: you can redistribute it and/or modify
102             it under the terms of the GNU General Public License as published by
103             the Free Software Foundation, either version 3 of the License, or
104             (at your option) any later version.
105              
106             Siebel Monitoring Tools is distributed in the hope that it will be useful,
107             but WITHOUT ANY WARRANTY; without even the implied warranty of
108             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
109             GNU General Public License for more details.
110              
111             You should have received a copy of the GNU General Public License
112             along with Siebel Monitoring Tools. If not, see L<http://www.gnu.org/licenses/>.
113              
114             =cut
115              
116             1;