File Coverage

blib/lib/CSS/Object/Property.pm
Criterion Covered Total %
statement 53 64 82.8
branch 9 18 50.0
condition 2 7 28.5
subroutine 15 17 88.2
pod 8 8 100.0
total 87 114 76.3


line stmt bran cond sub pod time code
1             ##----------------------------------------------------------------------------
2             ## CSS Object Oriented - ~/lib/CSS/Object/Property.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::Property;
11             BEGIN
12             {
13 6     6   42 use strict;
  6         13  
  6         204  
14 6     6   33 use warnings;
  6         13  
  6         183  
15 6     6   31 use parent qw( CSS::Object::Element );
  6         12  
  6         32  
16 6     6   387 use CSS::Object::Format;
  6         12  
  6         34  
17 6     6   4126 use CSS::Object::Value;
  6         16  
  6         60  
18 6     6   1551 use Devel::Confess;
  6         15  
  6         31  
19 6     6   3868 our $VERSION = 'v0.1.0';
20             };
21              
22             sub init
23             {
24 45     45 1 2783 my $self = shift( @_ );
25             ## print( STDERR ref( $self ), "::init() Args received are: ", $self->dump( @_ ), "\n" );
26             ## $self->{format} = '';
27 45         169 $self->{name} = '';
28 45         137 $self->{value} = '';
29 45         143 $self->{values} = [];
30             ## $self->{_init_strict_use_sub} = 1;
31 45         261 $self->SUPER::init( @_ );
32 45         153 $self->format->indent( ' ' );
33 45         2450 return( $self );
34             }
35              
36             sub add_to
37             {
38 7     7 1 20 my $self = shift( @_ );
39 7   50     28 my $rule = shift( @_ ) || return( $self->error( "No rule object was provided to add our property to it." ) );
40 7 50       241 return( $self->error( "Rule object provided (", overload::StrVal( $rule ), ") is not actually a CSS::Object::Rule object." ) ) if( !$self->_is_a( $rule, 'CSS::Object::Rule' ) );
41 7         120 $self->format->indent( $rule->format->indent );
42 7         404 $rule->add_property( $self );
43 7         27 return( $self );
44             }
45              
46 202     202 1 9134 sub as_string { return( $_[0]->format->property_as_string( $_[0] ) ); }
47              
48             sub format
49             {
50 814     814 1 3555 my $self = shift( @_ );
51 814 100       2037 if( @_ )
52             {
53             # my( $p, $f, $l ) = caller;
54             # $self->message( 3, "Property format called in package $p at line $l in file $f" );
55 16   50     81 my $format = $self->SUPER::format( @_ ) || return;
56             $self->values->foreach(sub
57             {
58 15 50   15   534 shift->format( $format ) || return;
59 16         59 });
60 16         186 $format->indent( ' ' );
61 16         1070 return( $format );
62             }
63 798         1955 return( $self->_set_get_object( 'format', 'CSS::Object::Format' ) );
64             }
65              
66 250     250 1 4132 sub name { return( shift->_set_get_scalar_as_object( 'name', @_ ) ); }
67              
68             sub value
69             {
70 48     48 1 5273 my $self = shift( @_ );
71 48 100       175 if( @_ )
72             {
73 44         101 my $val = shift( @_ );
74 44         77 my $valObj;
75 44 50       164 if( $self->_is_a( $val, 'CSS::Object::Value' ) )
    50          
76             {
77 0         0 $valObj = $val;
78 0         0 $valObj->format( $self->format );
79 0         0 $valObj->debug( $self->debug );
80             }
81             ## Array of values, which could be
82             elsif( $self->_is_array( $val ) )
83             {
84             ## Make sure this is a Module::Generic::Array
85 0         0 $val = $self->new_array( $val );
86             $val->foreach(sub
87             {
88 0 0 0 0   0 if( $self->_is_a( $_, 'CSS::Object::Value' ) )
    0          
89             {
90 0         0 $self->values->push( $_ );
91             }
92             elsif( !ref( $_ ) || $self->_is_a( $_, 'Module::Generic::Scalar' ) )
93             {
94 0         0 $valObj = CSS::Object::Value->new( "$_",
95             debug => $self->debug,
96             format => $self->format,
97             );
98 0         0 $self->values->push( $_ );
99             }
100             else
101             {
102 0         0 CORE::warn( "Got value \"$_\" in an array of values provided for this property, but I just do not know what to do with it.\n" );
103             }
104 0         0 });
105             }
106             else
107             {
108             # $self->message( 3, "Creating a value object for value '$val'." );
109 44         916 $valObj = CSS::Object::Value->new( $val,
110             debug => $self->debug,
111             format => $self->format,
112             );
113 44 50       192 defined( $valObj ) || return( $self->error( "Unable to initialise CSS::Object::Value object for value \"$val\"." ) );
114             }
115             # $self->message( 3, "Adding new value object '", overload::StrVal( $valObj ), " ($val) to our stack." );
116 44         192 $self->values->set( $valObj );
117             # $self->message( 3, "Stack of values contains ", $self->values->length, " elements. Returning the CSS::Object::Value object (", $valObj->as_string, ")." );
118 44         2064 return( $valObj );
119             }
120             # my $last = $self->values->last;
121             # $self->message( 3, "Returning value object '", $last, "'." );
122 4         20 return( $self->values->last );
123             }
124              
125             ## Array of CSS::Object::Value objects
126 266     266 1 9034 sub values { return( shift->_set_get_array_as_object( 'values', @_ ) ); }
127              
128 0     0 1   sub values_as_string { return( $_[0]->format->values_as_string( $_[0]->values ) ); }
129              
130             1;
131              
132             __END__
133              
134             =encoding utf-8
135              
136             =head1 NAME
137              
138             CSS::Object::Property - CSS Object Oriented Property
139              
140             =head1 SYNOPSIS
141              
142             use CSS::Object::Property;
143             my $prop = CSS::Object::Property->new(
144             name => 'display',
145             value => 'inline-block',
146             format => $format_object,
147             debug => 3,
148             ) || die( CSS::Object::Property->error );
149              
150             =head1 VERSION
151              
152             v0.1.0
153              
154             =head1 DESCRIPTION
155              
156             L<CSS::Object::Property> is a class to represent a CSS property.
157              
158             =head1 CONSTRUCTOR
159              
160             =head2 new
161              
162             To instantiate a new L<CSS::Object::Property> object, pass an hash reference of following parameters:
163              
164             =over 4
165              
166             =item I<debug>
167              
168             This is an integer. The bigger it is and the more verbose is the output.
169              
170             =item I<format>
171              
172             This is a L<CSS::Object::Format> object or one of its child modules.
173              
174             =item I<name>
175              
176             This is the property's name. When provided, this calls the method L</name> to store the value.
177              
178             =item I<value>
179              
180             This is the property's name. When provided, this calls the method L</value> to store the value.
181              
182             =back
183              
184             =head1 METHODS
185              
186             =head2 format
187              
188             This is a L<CSS::Object::Format> object or one of its child modules.
189              
190             =head2 as_string
191              
192             This calls the L</format> and its method L<CSS::Object::Format/property_as_string>
193              
194             It returns the css string produced or undef and sets an L<Module::Generic::Exception> upon error.
195              
196             =head2 format
197              
198             This sets or gets the L<CSS::Object::Format> object. When set, this will share the formatter with all its L<CSS::Object::Value> objects.
199              
200             =head2 name
201              
202             Sets or gets the property's name. The name stored here becomes a L<Module::Generic::Scalar> and thus all its object methods can be used
203              
204             =head2 value
205              
206             Sets the value or get the last value for this property.
207              
208             It takes either a string or a L<CSS::Object::Value> object and add it to the list of stored values for this property using the L</properties> method.
209              
210             It returns the last property value object in the L</values> array.
211              
212             =head2 values
213              
214             This sets or gets the list of L<CSS::Object::Value> objects. It uses a L<Module::Generic::Array> object to store those value objects.
215              
216             It returns the array object.
217              
218             =head2 values_as_string
219              
220             This returns a string representation of all the L<CSS::Object::Value> objects currently stored. It does this by calling the format's method L<CSS::Object::Format/values_as_string>
221              
222             =head1 AUTHOR
223              
224             Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
225              
226             =head1 SEE ALSO
227              
228             L<CSS::Object>
229              
230             =head1 COPYRIGHT & LICENSE
231              
232             Copyright (c) 2020 DEGUEST Pte. Ltd.
233              
234             You can use, copy, modify and redistribute this package and associated
235             files under the same terms as Perl itself.
236              
237             =cut