Object/Format.pm

filter: Statementstmt 88.5% 93 / 105 Branchbran 46.6% 28 / 60 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 84.8% 28 / 33 Podpod 100% 20 / 20 totaltotal 76.9% 170 / 221 CC 34 SCAR 15.3
CC
34
Cov
73%
properties_as_string
12.0
values_as_string
12.0
rule_as_string
9.9
CRAP 4.6
« Object/Builder.pm ↑ Summary Object/Rule/Keyframes.pm »
1 ##----------------------------------------------------------------------------
2 ## CSS Object Oriented - ~/lib/CSS/Object/Format.pm
3 ## Version v0.1.0
4 ## Copyright(c) 2020 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <@sitael.local>
6 ## Created 2020/06/21
7 ## Modified 2020/06/21
8 ##
9 ##----------------------------------------------------------------------------
10 package CSS::Object::Format;
11 BEGIN
12 {
13 6 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶ use Devel::Confess;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶ our $VERSION = 'v0.1.0';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 };
19  
20 sub init
21 {
22 57 ▶ my $self = shift( @_ );
Subroutine init: called CC=1 SCAR=0.0
Pod documented
23 57 $self->{new_line} = "\n";
24 57 $self->{open_brace_on_new_line} = 1;
25 57 $self->{close_brace_on_new_line} = 1;
26 57 $self->{open_brace_and_new_line} = 1;
27 57 $self->{indent} = '';
28 57 $self->{property_separator} = "\n";
29 57 $self->{_init_strict_use_sub} = 1;
30 57 $self->SUPER::init( @_ );
31 57 $self->{_params} = [qw(
32 close_brace_on_new_line
33 indent
34 new_line
35 open_brace_and_new_line
36 open_brace_on_new_line
37 property_separator
38 )];
39 57 return( $self );
40 }
41  
42 18 ▶sub backup_parameters { return( shift->clone ); }
Subroutine backup_parameters: called CC=1 SCAR=0.0
Pod documented
43  
44 31 ▶sub class { return( ref( $_[0] ) ); }
Subroutine class: called CC=1 SCAR=0.0
Pod documented
45  
46 409 ▶sub close_brace_on_new_line { return( shift->_set_get_boolean( 'close_brace_on_new_line', @_ ) ); }
Subroutine close_brace_on_new_line: called CC=1 SCAR=0.0
Pod documented
47  
48 sub comment_as_string
49 {
50 47 ▶ my( $self, $elem ) = @_;
Subroutine comment_as_string: called CC=3 SCAR=11.4
Pod documented
51 # no overloading;
52 47 ▶ return( $self->error( "No comment object was provided." ) ) if( !defined( $elem ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $elem 0 47 47
53 47 ▶ return( $self->error( "Comment object provied is not a CSS::Object::Comment object." ) ) if( !$self->_is_a( $elem, 'CSS::Object::Comment' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($elem, "CSS::Object::Comment") 0 47 47
54 47 return( '/* ' . $elem->values->join( "\n" )->scalar . ' */' );
55 }
56  
57 sub copy_parameters_from
58 {
59 22 ▶ my $self = shift( @_ );
Subroutine copy_parameters_from: called CC=5 SCAR=16.7
Pod documented
60 22 ▶ my $fmt = shift( @_ ) || return( $self->error( "No formatter object was provided to copy the parameters from." ) );
Condition50%1/2
shift @_ || (return $self->error('No formatter object was provided to copy the parameters from.'))
l!l
220
MC/DC0%0/1
shift @_ || (return $self->error('No formatter object was provided to copy the parameters from.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
61 22 ▶ return( $self->error( "Formatter object provided is actually not a formatter object." ) ) if( !$self->_is_a( $fmt, 'CSS::Object::Format' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($fmt, "CSS::Object::Format") 0 22 22
62 # my( $p, $f, $l ) = caller();
63 # $self->message( 3, "copy_parameters_from called from package $p at line $l in file $f to set indent from '", $self->indent->scalar, "' to '", $fmt->indent->scalar, "'." );
64 22 my $ok_params = $self->{_params};
65 22 for( @$ok_params )
66 {
67 132 ▶ $self->$_( $fmt->$_ ) if( $fmt->can( $_ ) );
Branch: 1 branch
BranchTrueFalseTotal
if $fmt->can($_) 132 0 132
68 }
69 22 return( $self );
70 }
71  
72 sub elements_as_string
73 {
74 331 ▶ my( $self, $elems ) = @_;
Subroutine elements_as_string: called CC=3 SCAR=11.1
Pod documented
75 # no overloading;
76 331 ▶ return( $self->error( "No elements array was provided." ) ) if( !defined( $elems ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $elems 0 331 331
77 331 ▶ return( $self->error( "Elements provided is not an array object." ) ) if( !$self->_is_a( $elems, 'Module::Generic::Array' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($elems, "Module::Generic::Array") 0 331 331
78 331 my $result = Module::Generic::Array->new;
79 331 my $nl = $self->new_line->scalar;
80 331 my $prop_sep = $self->property_separator->scalar;
81 # $self->message( 3, "Property separator is set to \"$prop_sep\"." );
82 ## $prop_sep .= $self->indent->scalar;
83 $elems->foreach(sub
84 {
85 # $self->message( 3, "Stringifying element object of class \"" . $_->class . "\"." );
86 279 ▶ $result->push( $_->format->indent->scalar . $_->as_string . ( $_->isa( 'CSS::Object::Comment' ) ? '' : ';' ) );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_->isa('CSS::Object::Comment') ? : 19 260 279
87 331 });
88 331 return( $result->join( $prop_sep )->scalar );
89 }
90  
91 ## sub indent { return( shift->_set_get_scalar_as_object( 'indent', @_ ) ); }
92 sub indent
93 {
94 1073 ▶ my $self = shift( @_ );
Subroutine indent: called CC=2 SCAR=6.9
Pod documented
95 1073 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 161 912 1073
96 {
97 161 my $val = shift( @_ );
98 # my( $p, $f, $l ) = caller();
99 # $self->message( 3, "Format called from package $p at line $l in file $f to set indent to '$val'." );
100 161 return( $self->_set_get_scalar_as_object( 'indent', $val ) );
101 }
102 912 return( $self->_set_get_scalar_as_object( 'indent' ) );
103 }
104  
105 sub keyframes_as_string
106 {
107 19 ▶ my( $self, $keyf ) = @_;
Subroutine keyframes_as_string: called CC=6 SCAR=19.0
Pod documented
108 19 ▶ return( $self->error( "No keyframes rule object was provided." ) ) if( !defined( $keyf ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $keyf 0 19 19
109 19 ▶ return( $self->error( "Keyframes object provided (", overload::StrVal( $keyf ), ") is not actually an CSS::Object::Rule::Keyframes" ) ) if( !$self->_is_a( $keyf, 'CSS::Object::Rule::Keyframes' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($keyf, "CSS::Object::Rule::Keyframes") 0 19 19
110 ## no overloading;
111 ## Calling rule_as_string on each item in our stack
112 19 ▶ my $rule_str = $keyf->elements->map(sub{ $_->as_string })->join( "\n" )->scalar;
Subroutine __ANON__: called CC=1 SCAR=0.0
113 19 my $type = $keyf->type->tr( '_', '-' )->scalar;
114 19 my $name = $keyf->name->scalar;
115 19 my $nl = $self->new_line->scalar;
116 19 ▶ return( '@' . "${type} ${name}" . ( $self->open_brace_on_new_line ? $nl : ' ' ) . "{" . ( $self->open_brace_and_new_line ? $nl : '' ) . $rule_str . ( $self->close_brace_on_new_line ? $nl : ' ' ) . "}" );
Branch: 3 branches
BranchTrueFalseTotal
$self->open_brace_on_new_line ? : 19 0 19
$self->open_brace_and_new_line ? : 19 0 19
$self->close_brace_on_new_line ? : 19 0 19
117 }
118  
119 742 ▶sub new_line { return( shift->_set_get_scalar_as_object( 'new_line', @_ ) ); }
Subroutine new_line: called CC=1 SCAR=0.0
Pod documented
120  
121 409 ▶sub open_brace_on_new_line { return( shift->_set_get_boolean( 'open_brace_on_new_line', @_ ) ); }
Subroutine open_brace_on_new_line: called CC=1 SCAR=0.0
Pod documented
122  
123 409 ▶sub open_brace_and_new_line { return( shift->_set_get_boolean( 'open_brace_on_new_line', @_ ) ); }
Subroutine open_brace_and_new_line: called CC=1 SCAR=0.0
Pod documented
124  
125 ## Outdated. See elements_as_string
126 sub properties_as_string
127 {
128 0 ▶ my( $self, $properties ) = @_;
Subroutine properties_as_string: not called CC=3 SCAR=24.8
Pod documented
129 # no overloading;
130 0 ▶ return( $self->error( "No properties array was provided." ) ) if( !defined( $properties ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $properties 0 0 0
131 0 ▶ return( $self->error( "Properties provided is not an array object." ) ) if( !$self->_is_a( $properties, 'Module::Generic::Array' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($properties, "Module::Generic::Array") 0 0 0
132 # return( join( '; ', map{ $_->name . ": " . $_->values->map(sub{ $_->value->scalar })->join( '' ) } @$properties ) );
133 0 ▶ return( $properties->map(sub{ $_->name->scalar . ': ' . $_->values->map(sub{ $_->value->scalar })->join( '' )->scalar })->join( ";" )->scalar );
Subroutine __ANON__: not called CC=1 SCAR=6.9
134 }
135  
136 sub property_as_string
137 {
138 260 ▶ my( $self, $prop ) = @_;
Subroutine property_as_string: called CC=3 SCAR=11.2
Pod documented
139 # no overloading;
140 260 ▶ return( $self->error( "No property object was provided." ) ) if( !defined( $prop ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prop 0 260 260
141 260 ▶ return( $self->error( "Property object provied is not a CSS::Object::Property object." ) ) if( !$self->_is_a( $prop, 'CSS::Object::Property' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($prop, "CSS::Object::Property") 0 260 260
142 260 my $indent = $prop->format->indent->scalar;
143 260 ▶ return( $prop->name->scalar . ': ' . $prop->values->map(sub{ $_->as_string })->join( '' )->scalar );
Subroutine __ANON__: called CC=1 SCAR=0.0
144 }
145  
146 427 ▶sub property_separator { return( shift->_set_get_scalar_as_object( 'property_separator', @_ ) ); }
Subroutine property_separator: called CC=1 SCAR=0.0
Pod documented
147  
148 sub restore_parameters
149 {
150 18 ▶ my $self = shift( @_ );
Subroutine restore_parameters: called CC=3 SCAR=11.0
Pod documented
151 18 my $this = shift( @_ );
152 18 ▶ return( $self->error( "Data provided is not an hash reference." ) ) if( !$self->_is_hash( $this ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_hash($this) 0 18 18
153 18 my $params = $self->{_params};
154 18 foreach my $p ( @$params )
155 {
156 108 $self->$p( $this->{ $p } );
157 }
158 18 return( $self );
159 }
160  
161 sub rule_as_string
162 {
163 328 ▶ my( $self, $rule ) = @_;
Subroutine rule_as_string: called CC=7 SCAR=22.9
Pod documented
164 # no overloading;
165 # my( $pack, $file, $line ) = caller;
166 # $self->message( 3, "Stringifying rule called from package $pack at line $line in file $file" );
167 328 ▶ return( $self->error( "No rule object was provided." ) ) if( !defined( $rule ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rule 0 328 328
168 328 ▶ return( $self->error( "Rule object provided (", overload::Overloaded( $rule ) ? overload::StrVal( $rule ) : $rule ,") is not an actual rule object." ) ) if( !$self->_is_a( $rule, 'CSS::Object::Rule' ) );
Branch: 2 branches
BranchTrueFalseTotal
unless $self->_is_a($rule, "CSS::Object::Rule") 0 328 328
overload::Overloaded($rule) ? : 0 0 0
169 328 my $rule_indent = $rule->format->indent->scalar;
170 328 my $nl = $self->new_line->scalar;
171 # $self->message( 3, "Indent string is '$rule_indent'." );
172 ## return( $rule->selectors_as_string . ' { ' . $rule->properties_as_string . " }\n" );
173 return(
174 328 ▶ $rule_indent . $rule->selectors_as_string .
Branch: 3 branches
BranchTrueFalseTotal
$self->open_brace_on_new_line ? : 328 0 328
$self->open_brace_and_new_line ? : 328 0 328
$self->close_brace_on_new_line ? : 328 0 328
175 ( $self->open_brace_on_new_line ? $nl : ' ' ) .
176 $rule_indent . '{' .
177 ( $self->open_brace_and_new_line ? $nl : ' ' ) .
178 $rule->elements_as_string .
179 ( $self->close_brace_on_new_line ? $nl : ' ' ) .
180 "${rule_indent}}"
181 );
182 }
183  
184 sub selectors_as_string
185 {
186 328 ▶ my( $self, $selectors ) = @_;
Subroutine selectors_as_string: called CC=1 SCAR=0.0
Pod documented
187 328 ▶ return( $selectors->map(sub{ $_->name->scalar })->join( ', ' )->scalar );
Subroutine __ANON__: called CC=1 SCAR=0.0
188 }
189  
190 sub value_as_string
191 {
192 268 ▶ my( $self, $val ) = @_;
Subroutine value_as_string: called CC=5 SCAR=17.2
Pod documented
193 268 ▶ return( $self->error( "No value object was provided." ) ) if( !$self->_is_a( $val, 'CSS::Object::Value' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($val, "CSS::Object::Value") 0 268 268
194 268 ▶ return( $self->error( "Value object provided is not a CSS::Object::Value object." ) ) if( !$self->_is_a( $val, 'CSS::Object::Value' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($val, "CSS::Object::Value") 0 268 268
195 # $self->messagef( 3, "%d comments before and %d comments after.", $val->comment_before->length, $val->comment_after->length );
196 return(
197 12 ▶ $val->comment_before->map(sub{ $_->as_string })->join( ' ' )->scalar . ( $val->comment_before->length > 0 ? ' ' : '' ) .
Subroutine __ANON__: called CC=1 SCAR=0.0
198 $val->value->as_string .
199 268 ▶ ( $val->comment_after->length > 0 ? ' ' : '' ) . $val->comment_after->map(sub{ $_->as_string })->join( ' ' )->scalar
Subroutine __ANON__: not called CC=1 SCAR=0.4
Branch: 2 branches
BranchTrueFalseTotal
$val->comment_before->length > 0 ? : 12 256 268
$val->comment_after->length > 0 ? : 0 268 268
200 );
201 }
202  
203 sub values_as_string
204 {
205 0 ▶ my( $self, $values ) = @_;
Subroutine values_as_string: not called CC=3 SCAR=24.8
Pod documented
206 0 ▶ return( $self->error( "No properties values array was provided." ) ) if( !defined( $values ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $values 0 0 0
207 0 ▶ return( $self->error( "Properties values provided is not an array." ) ) if( !$self->_is_a( $values, 'Module::Generic::Array' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($values, "Module::Generic::Array") 0 0 0
208 ## return( join( '', map{ $_->value } @$values ) );
209 ## return( $values->map(sub{ $_->value->scalar })->join( '' )->scalar );
210 0 ▶ return( $values->map(sub{ $_->as_string })->join( '' )->scalar );
Subroutine __ANON__: not called CC=1 SCAR=6.9
211 }
212  
213 6 1;
214  
215 __END__
« Object/Builder.pm ↑ Summary Object/Rule/Keyframes.pm »