File Coverage

blib/lib/CSS/Object/Rule/Keyframes.pm
Criterion Covered Total %
statement 44 45 97.7
branch 4 6 66.6
condition 2 5 40.0
subroutine 11 12 91.6
pod 3 5 60.0
total 64 73 87.6


line stmt bran cond sub pod time code
1             ##----------------------------------------------------------------------------
2             ## CSS Object Oriented - ~/lib/CSS/Object/Rule.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::Rule::Keyframes;
11             BEGIN
12             {
13 6     6   43 use strict;
  6         12  
  6         197  
14 6     6   50 use warnings;
  6         12  
  6         198  
15             ## We add CSS::Object in our @ISA so that we can add rule to our KeyframesRule package
16             ## KeyFrames are special rules that are blocks of further rules
17 6     6   31 use parent qw( CSS::Object::Rule::At CSS::Object );
  6         20  
  6         39  
18 6     6   423 use Devel::Confess;
  6         13  
  6         36  
19 6     6   380 use Want ();
  6         14  
  6         144  
20             use overload (
21 6         33 '""' => 'as_string',
22             fallback => 1,
23 6     6   30 );
  6         12  
24 6     6   2732 our $VERSION = 'v0.1.0';
25             };
26              
27             sub init
28             {
29 1     1 1 64 my $self = shift( @_ );
30 1         11 $self->SUPER::init( @_ );
31 1         4 $self->format->indent( ' ' );
32 1         61 return( $self );
33             }
34              
35             sub as_string
36             {
37 17     17 1 323 my $self = shift( @_ );
38             # $self->messagef( 3, "There are %d elements for this keyframes.", $self->elements->length );
39 17         78 return( $self->format->keyframes_as_string( $self ) );
40             }
41              
42             sub frame
43             {
44 7     7 0 22 my $self = shift( @_ );
45             # no overloading;
46             ## $offset can be a scalar or an array reference
47 7         23 my( $offset, @props ) = @_;
48             ## If we are given an hash reference, we make it an array with hash keys sorted
49 7 50 33     58 if( scalar( @props ) == 1 && $self->_is_hash( $props[0] ) )
50             {
51 7         63 @props = map{ $_ => $props[0]->{ $_ } } sort( keys( %{$props[0]} ) );
  7         37  
  7         47  
52             }
53 7 50       30 return( $self->error( "Uneven number of parameters to set keyframes" ) ) if( scalar( @props ) % 2 );
54 7         32 my $css = $self->css;
55 7   50     151 my $frame_rule = $css->new_rule->add_to( $self ) ||
56             return( $self->error( "Cannot add new rule: ", CSS::Object::Rule::Keyframes->error ) );
57 7         283 $frame_rule->format->indent( ' ' );
58             ## $self->message( 3, "Frame rule indent is now '", $frame_rule->format->indent->scalar, "'." );
59 7 100       476 if( $self->_is_array( $offset ) )
60             {
61 1         10 foreach my $this ( @$offset )
62             {
63 6         30 $css->new_selector( name => "${this}\%" )->add_to( $frame_rule );
64             }
65             }
66             else
67             {
68 6         114 $css->new_selector( name => "${offset}\%" )->add_to( $frame_rule );
69             }
70 7         83 while( my( $prop, $val ) = CORE::splice( @props, 0, 2 ) )
71             {
72             # $self->message( 3, "Adding property '$prop' with value '$val' to keyframes." );
73 7         25 $prop =~ tr/_/-/;
74 7         54 $css->new_property( name => $prop, value => $val )->add_to( $frame_rule )->format->indent( ' ' );
75             }
76             # $self->messagef( 3, "Frame rules completed registering %d elements.", $frame_rule->elements->length );
77             ## For chaining
78 7         488 return( $self );
79             }
80              
81             ## In our @ rule, we hold all the rules. This makes our @ rule special, because it is a rule that contains a set of rules
82             ## Array of CSS::Object::Rule::Keyframes objects
83 0     0 1 0 sub rules { return( shift->_set_get_array_as_object( 'rules', @_ ) ); }
84              
85 18     18 0 217 sub type { return( shift->_set_get_scalar_as_object( 'type', @_ ) ); }
86              
87              
88             1;
89              
90             __END__
91              
92             =encoding utf-8
93              
94             =head1 NAME
95              
96             CSS::Object::Rule::Keyframes - CSS Object Oriented Rule
97              
98             =head1 SYNOPSIS
99              
100             use CSS::Object::Rule::Keyframes;
101             my $rule = CSS::Object::Rule::Keyframes->new( debug => 3, format => $format_object ) ||
102             die( CSS::Object::Rule::Keyframes->error );
103              
104             =head1 VERSION
105              
106             v0.1.0
107              
108             =head1 DESCRIPTION
109              
110             L<CSS::Object::Rule::Keyframes> a class containing one or more L<CSS::Object::Selector> objects> and one ore more L<CSS::Object::Property> objects.
111              
112             =head1 CONSTRUCTOR
113              
114             =head2 new
115              
116             To instantiate a new L<CSS::Object::Rule::Keyframes> object, pass an hash reference of following parameters:
117              
118             =over 4
119              
120             =item I<debug>
121              
122             This is an integer. The bigger it is and the more verbose is the output.
123              
124             =item I<format>
125              
126             This is a L<CSS::Object::Format> object or one of its child modules.
127              
128             =back
129              
130             =head1 METHODS
131              
132             =head2 add_element
133              
134             Provided with a L<CSS::Object::Element> object and this add it to the array of L</elements> for this rule.
135              
136             =head2 add_property
137              
138             Provided with a L<CSS::Object::Property> object and this adds it to the list of properties contained in this rule.
139              
140             The object is added to the L</properties> array object, which is an L<Module::Generic::Array> object.
141              
142             =head2 add_selector
143              
144             Provided with a L<CSS::Object::Selector> object and this adds it to the list of selectors contained in this rule.
145              
146             The object is added to the L</selectors> array object, which is an L<Module::Generic::Array> object.
147              
148             =head2 add_to
149              
150             Provided with a L<CSS::Object> object and this add our object to its list of elements by calling L<CSS::Object/add_rule>
151              
152             =head2 as_string
153              
154             This calls the L</format> and its method L<CSS::Object::Format/rule_as_string>
155              
156             It returns the css string produced or undef and sets an L<Module::Generic::Exception> upon error.
157              
158             =head2 elements
159              
160             Sets or gets the list of elements for this rule. This uses an array object from L<Module::Generic::Array>
161              
162             Typical elements for a rule are properties (L<CSS::Object::Property>) and comments (L<CSS::Object::Comment>).
163              
164             =head2 elements_as_string
165              
166             This takes our list of L</elements> and call L<CSS:Object::Format/elements_as_string> to stringify them and return a formatted string.
167              
168             =head2 format
169              
170             This is a L<CSS::Object::Format> object or one of its child modules.
171              
172             =head2 get_property_by_name
173              
174             Provided with a property name, and this returns its matching L<CSS::Object::Property> objects.
175              
176             It returns a list of objects in list context or the first match found in scalar context.
177              
178             =head2 properties
179              
180             This sets or gets the L<Module::Generic::Array> object used to store all the L<CSS::Object::Property> objects.
181              
182             =head2 properties_as_string
183              
184             This returns the string value of all the properties objects currently held. It calls the method L<CSS::Object::Format/properties_as_string> to stringify those properties.
185              
186             =head2 selectors
187              
188             This sets or gets the L<Module::Generic::Array> object used to store all the L<CSS::Object::Selector> objects.
189              
190             =head2 selectors_as_string
191              
192             This returns the string value of all the selector objects currently held. It calls the method L<CSS::Object::Format/selectors_as_string> to stringify those selectors.
193              
194             head1 AUTHOR
195              
196             Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
197              
198             =head1 SEE ALSO
199              
200             L<CSS::Object>
201              
202             =head1 COPYRIGHT & LICENSE
203              
204             Copyright (c) 2020 DEGUEST Pte. Ltd.
205              
206             You can use, copy, modify and redistribute this package and associated
207             files under the same terms as Perl itself.
208              
209             =cut