File Coverage

blib/lib/CSS/Object/Rule/Keyframes.pm
Criterion Covered Total %
statement 41 42 97.6
branch 5 8 62.5
condition 2 5 40.0
subroutine 10 11 90.9
pod 4 5 80.0
total 62 71 87.3


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