File Coverage

blib/lib/CSS/Object/Element.pm
Criterion Covered Total %
statement 37 49 75.5
branch 7 14 50.0
condition 1 9 11.1
subroutine 8 11 72.7
pod 4 5 80.0
total 57 88 64.7


line stmt bran cond sub pod time code
1             ##----------------------------------------------------------------------------
2             ## CSS Object Oriented - ~/lib/CSS/Object/Element.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::Element;
11             BEGIN
12             {
13 6     6   2621 use strict;
  6         15  
  6         200  
14 6     6   28 use warnings;
  6         13  
  6         190  
15 6     6   32 use parent qw( Module::Generic );
  6         10  
  6         41  
16 6     6   3102 use CSS::Object::Format;
  6         20  
  6         107  
17 6     6   2014 use Devel::Confess;
  6         16  
  6         31  
18 6     6   2813 our $VERSION = 'v0.1.0';
19             };
20              
21             sub init
22             {
23 175     175 1 411 my $self = shift( @_ );
24 175         465 $self->{format} = '';
25 175         400 $self->{_init_strict_use_sub} = 1;
26 175         793 $self->SUPER::init( @_ );
27 175 100       3147 unless( $self->_is_a( $self->{format}, 'CSS::Object::Format' ) )
28             {
29 7         79 my $format = CSS::Object::Format->new(
30             debug => $self->debug
31             );
32 7         64 $self->format( $format );
33             }
34 175         3220 return( $self );
35             }
36              
37             sub add_to
38             {
39 0     0 1 0 my $self = shift( @_ );
40 0   0     0 my $css = shift( @_ ) || return( $self->error( "No css object was provided to add our element to it." ) );
41 0 0       0 return( $self->error( "CSS object provided (", overload::StrVal( $css ), ") is not actually a CSS::Object object." ) ) if( !$self->_is_a( $css, 'CSS::Object' ) );
42 0         0 $self->format->indent( $css->format->indent );
43 0         0 $css->add_element( $self );
44 0         0 return( $self );
45             }
46              
47 0     0 1 0 sub as_string { return( shift->error( "This method has not been implemented in this class." ) ); }
48              
49 0     0 0 0 sub class { return( ref( $_[0] ) ); }
50              
51             sub format
52             {
53 928     928 1 11562 my $self = shift( @_ );
54 928         1444 my $format;
55 928 100       2230 if( @_ )
56             {
57             # $format = $self->_set_get_object( 'format', 'CSS::Object::Format', @_ ) || return;
58 161         360 my $val = shift( @_ );
59             # $self->message( 3, "Setting new formatter '$val' for our class '", $self->class, "'." );
60 161         272 my $format;
61 161 50 0     576 if( $self->_is_a( $val, 'CSS::Object::Format' ) )
    0          
62             {
63 161         3327 my $clone = $val->clone;
64             ## Make a copy for ourself
65 161 100       132374 if( $self->format )
66             {
67 30         725 my( $p, $f, $l ) = caller();
68             # $self->message( 3, "New formatter provided for our class '", $self->class, "' called from package $p at line $l in file $f to set indent from '", $self->format->indent->scalar, "' to '", $clone->indent->scalar, "'." );
69 30         97 $clone->copy_parameters_from( $self->format );
70             }
71 161   50     10314 $format = $self->_set_get_object( 'format', 'CSS::Object::Format', $clone ) ||
72             return( $self->pass_error );
73             # die( "Provided value (", overload::StrVal( $val ), " and stored value (", overload::StrVal( $format ), ") are the same. It should have been cloned.\n" ) if( overload::StrVal( $val ) eq overload::StrVal( $format ) );
74             }
75             ## format as a class name
76             elsif( !ref( $val ) && CORE::index( $val, '::' ) != -1 )
77             {
78 0 0       0 $self->_load_class( $val ) || return( $self->pass_error );
79 0   0     0 $format = $val->new( debug => $self->debug ) || return( $self->pass_error( $val->error ) );
80 0         0 $self->_set_get_object( 'format', 'CSS::Object::Format', $format );
81             }
82             else
83             {
84 0         0 return( $self->error( "Unknown format \"$val\". I do not know what to do with it." ) );
85             }
86 161         5438 return( $format );
87             }
88 767         2253 return( $self->_set_get_object( 'format', 'CSS::Object::Format' ) );
89             }
90              
91             1;
92              
93             __END__
94              
95             =encoding utf-8
96              
97             =head1 NAME
98              
99             CSS::Object::Element - CSS Object Oriented Element
100              
101             =head1 SYNOPSIS
102              
103             package CSS::Object::Comment;
104             use parent qw( CSS::Object::Element );
105             my $cmt = CSS::Object::Comment->new( "No comment" ) ||
106             die( CSS::Object::Comment->error );
107              
108             =head1 VERSION
109              
110             v0.1.0
111              
112             =head1 DESCRIPTION
113              
114             L<CSS::Object::Element> is a base class for all L<CSS::Object> elements that get added to the style sheet.
115              
116             =head1 CONSTRUCTOR
117              
118             =head2 new
119              
120             This is the base method to instantiate object. It takes by default the following arguments and instantiate an L<CSS::Object::Format> object if none was provided.
121              
122             =over 4
123              
124             =item I<debug>
125              
126             This is an integer. The bigger it is and the more verbose is the output.
127              
128             =item I<format>
129              
130             This is a L<CSS::Object::Format> object or one of its child modules.
131              
132             =back
133              
134             =head1 METHODS
135              
136             =head2 add_to
137              
138             Provided with a L<CSS::Object> and this add our object to the css object array of elements by calling L<CSS::Object/add_element>. Elements here are top level elements which are css rules.
139              
140             =head2 as_string
141              
142             This method must be overridden by the child class.
143              
144             =head2 format
145              
146             This is a L<CSS::Object::Format> object or one of its child modules.
147              
148             head1 AUTHOR
149              
150             Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
151              
152             =head1 SEE ALSO
153              
154             L<CSS::Object>
155              
156             =head1 COPYRIGHT & LICENSE
157              
158             Copyright (c) 2020 DEGUEST Pte. Ltd.
159              
160             You can use, copy, modify and redistribute this package and associated
161             files under the same terms as Perl itself.
162              
163             =cut