File Coverage

blib/lib/Spreadsheet/WriteExcel/Styler.pm
Criterion Covered Total %
statement 43 50 86.0
branch 11 16 68.7
condition 4 6 66.6
subroutine 8 11 72.7
pod 6 6 100.0
total 72 89 80.9


line stmt bran cond sub pod time code
1             package Spreadsheet::WriteExcel::Styler;
2 2     2   28648 use warnings;
  2         5  
  2         73  
3 2     2   11 use strict;
  2         4  
  2         69  
4 2     2   10 use Carp;
  2         9  
  2         315  
5            
6             our $VERSION = '1.02';
7            
8             # shorthand notation: $styler->(..) instead of $styler->format(..)
9             use overload
10 2     2   3588 '&{}' => sub {my $self = shift; return sub {$self->format(@_)}};
  2     6   2188  
  2         21  
  6         1944  
  6         28  
  6         17  
11            
12            
13             sub new {
14 1     1 1 24 my $class = shift;
15 1         4 my $workbook = shift;
16            
17 1 50 33     20 ref $workbook && $workbook->can('add_format')
18             or croak "improper 'workbook' arg (has no 'add_format' method)";
19            
20 1         6 my $self = { workbook => $workbook, # ref to workbook
21             style => {}, # hash of styles and features
22             format => {}, # cache of created formats
23             };
24 1         5 bless $self, $class;
25             }
26            
27             sub add_styles {
28 1     1 1 19 my $self = shift;
29            
30 1         9 while (my ($style_name, $features) = splice(@_, 0, 2)) {
31 4 50       54 carp "overriding style $style_name" if $self->{style}{$style_name};
32 4 50       13 croak "features for style $style_name should be expressed as a hashref"
33             if ref $features ne 'HASH';
34 4         23 $self->{style}{$style_name} = $features;
35             }
36             }
37            
38            
39             sub format {
40 6     6 1 8 my $self = shift;
41            
42             # get styles from args (can be array, single arrayref or single hashref)
43 6         8 my @styles;
44 6         7 my $first_arg = shift;
45 6         12 my $ref = ref $first_arg;
46 6 100       12 if ($ref) {
47 3 50       10 not @_ or croak "too many args to format() when first arg is a ref";
48 7         16 @styles = $ref eq 'ARRAY' ? @$first_arg
49 3 50       16 : $ref eq 'HASH' ? (grep {$first_arg->{$_}} keys %$first_arg)
    100          
50             : croak "unexpected arg to format() ($ref)";
51             }
52             else {
53 3         9 @styles = ($first_arg, @_);
54             }
55            
56             # retrieve format for that combination of styles ... or create a new one
57 6         50 my $format_name = join $;, sort @styles;
58 6   100     25 $self->{format}{$format_name} ||= do {
59 3         6 my @unknown = grep {!$self->{style}{$_}} @styles;
  7         24  
60             !@unknown
61 3 100       220 or croak "unknown style : " . join ", ", @unknown;
62 2         6 $self->{workbook}->add_format(map {%{$self->{style}{$_}}} @styles);
  4         9  
  4         30  
63             };
64 5         46 return $self->{format}{$format_name};
65             }
66            
67             sub workbook {
68 0     0 1   my $self = shift;
69 0           return $self->{workbook};
70             }
71            
72             sub styles {
73 0     0 1   my $self = shift;
74 0           return keys %{$self->{style}};
  0            
75             }
76            
77             sub style {
78 0     0 1   my ($self, $style_name) = @_;
79 0           return $self->{style}{style_name};
80             }
81            
82             1; # End of Spreadsheet::WriteExcel::Styler
83            
84             __END__