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   2155 use strict;
  6         12  
  6         158  
14 6     6   25 use warnings;
  6         10  
  6         140  
15 6     6   27 use parent qw( Module::Generic );
  6         9  
  6         41  
16 6     6   2559 use CSS::Object::Format;
  6         14  
  6         79  
17 6     6   1590 use Devel::Confess;
  6         11  
  6         19  
18 6     6   2182 our $VERSION = 'v0.1.0';
19             };
20              
21             sub init
22             {
23 175     175 1 294 my $self = shift( @_ );
24 175         303 $self->{format} = '';
25 175         290 $self->{_init_strict_use_sub} = 1;
26 175         573 $self->SUPER::init( @_ );
27 175 100       2125 unless( $self->_is_a( $self->{format}, 'CSS::Object::Format' ) )
28             {
29 7         62 my $format = CSS::Object::Format->new(
30             debug => $self->debug
31             );
32 7         24 $self->format( $format );
33             }
34 175         2007 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 800     800 1 7594 my $self = shift( @_ );
54 800         1204 my $format;
55 800 100       1644 if( @_ )
56             {
57             # $format = $self->_set_get_object( 'format', 'CSS::Object::Format', @_ ) || return;
58 161         240 my $val = shift( @_ );
59             # $self->message( 3, "Setting new formatter '$val' for our class '", $self->class, "'." );
60 161         210 my $format;
61 161 50 0     487 if( $self->_is_a( $val, 'CSS::Object::Format' ) )
    0          
62             {
63 161         2214 my $clone = $val->clone;
64             ## Make a copy for ourself
65 161 100       12801 if( $self->format )
66             {
67 30         509 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         64 $clone->copy_parameters_from( $self->format );
70             }
71 161   50     8926 $format = $self->_set_get_object( 'format', 'CSS::Object::Format', $clone ) || return;
72             # 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 ) );
73             }
74             ## format as a class name
75             elsif( !ref( $val ) && CORE::index( $val, '::' ) != -1 )
76             {
77 0 0       0 $self->_load_class( $val ) || return;
78 0   0     0 $format = $val->new( debug => $self->debug ) || return( $self->pass_error( $val->error ) );
79 0         0 $self->_set_get_object( 'format', 'CSS::Object::Format', $format );
80             }
81             else
82             {
83 0         0 return( $self->error( "Unknown format \"$val\". I do not know what to do with it." ) );
84             }
85 161         4110 return( $format );
86             }
87 639         1487 return( $self->_set_get_object( 'format', 'CSS::Object::Format' ) );
88             }
89              
90             1;
91              
92             __END__
93              
94             =encoding utf-8
95              
96             =head1 NAME
97              
98             CSS::Object::Element - CSS Object Oriented Element
99              
100             =head1 SYNOPSIS
101              
102             package CSS::Object::Comment;
103             use parent qw( CSS::Object::Element );
104             my $cmt = CSS::Object::Comment->new( "No comment" ) ||
105             die( CSS::Object::Comment->error );
106              
107             =head1 VERSION
108              
109             v0.1.0
110              
111             =head1 DESCRIPTION
112              
113             L<CSS::Object::Element> is a base class for all L<CSS::Object> elements that get added to the style sheet.
114              
115             =head1 CONSTRUCTOR
116              
117             =head2 new
118              
119             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.
120              
121             =over 4
122              
123             =item I<debug>
124              
125             This is an integer. The bigger it is and the more verbose is the output.
126              
127             =item I<format>
128              
129             This is a L<CSS::Object::Format> object or one of its child modules.
130              
131             =back
132              
133             =head1 METHODS
134              
135             =head2 add_to
136              
137             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.
138              
139             =head2 as_string
140              
141             This method must be overridden by the child class.
142              
143             =head2 format
144              
145             This is a L<CSS::Object::Format> object or one of its child modules.
146              
147             head1 AUTHOR
148              
149             Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
150              
151             =head1 SEE ALSO
152              
153             L<CSS::Object>
154              
155             =head1 COPYRIGHT & LICENSE
156              
157             Copyright (c) 2020 DEGUEST Pte. Ltd.
158              
159             You can use, copy, modify and redistribute this package and associated
160             files under the same terms as Perl itself.
161              
162             =cut