File Coverage

blib/lib/XAO/DO/Web/Styler.pm
Criterion Covered Total %
statement 37 43 86.0
branch 19 26 73.0
condition 12 15 80.0
subroutine 6 6 100.0
pod 1 2 50.0
total 75 92 81.5


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             XAO::DO::Web::Styler - Simple styler object
4              
5             =head1 SYNOPSIS
6              
7             Currently is only useful in XAO::Web site context.
8              
9             =head1 DESCRIPTION
10              
11             Styler allows to define style templates and then use them in the web
12             pages. It also includes formatting capabilities for various commonly
13             used values.
14              
15             XXX - make real documentation!!
16              
17             =cut
18              
19             ###############################################################################
20             package XAO::DO::Web::Styler;
21 1     1   877 use strict;
  1         2  
  1         29  
22 1     1   5 use XAO::Utils qw(:args fround);
  1         2  
  1         86  
23 1     1   5 use XAO::Objects;
  1         2  
  1         37  
24 1     1   4 use base XAO::Objects->load(objname => 'Web::Page');
  1         1  
  1         4  
25              
26             our $VERSION='2.003';
27              
28             ###############################################################################
29              
30             sub separate_thousands ($);
31              
32             ###############################################################################
33              
34             sub display ($;%) {
35 15     15 1 17 my $self=shift;
36 15         27 my $args=get_args(\@_);
37              
38             ##
39             # Special formatting for special fields.
40             #
41             # number => 1,234,456,789
42             #
43 15 100       156 my $template="<%NUMBER%>" if defined($args->{number});
44 15   100     40 my $number=int($args->{number} || 0);
45 15 100       63 $number=separate_thousands($args->{'format'} ? sprintf($args->{'format'},$number) : $number);
46              
47             ##
48             # dollars => $1'234.78
49             #
50 15 100 66     43 $template="<%DOLLARS%>" if defined($args->{dollars}) || defined($args->{dollar});
51             my $dollars=$args->{format}
52             ? sprintf($args->{format},$args->{dollars} || $args->{dollar} || 0)
53 15 100 50     75 : sprintf('%.2f',fround($args->{dollars} || $args->{dollar} || 0,100));
      50        
54 15         121 $dollars='$' . separate_thousands($dollars);
55              
56             ##
57             # real => 1'234.78
58             #
59 15 100       33 $template='<$REAL$>' if defined($args->{real});
60             my $real=$args->{format}
61             ? sprintf($args->{format},$args->{real} || 0)
62 15 100 100     77 : sprintf("%.2f",$args->{real} || 0);
      100        
63 15         20 $real=separate_thousands($real);
64              
65             ##
66             # Percents
67             #
68 15         19 my $percent=0;
69 15 50       27 if(defined($args->{percent})) {
70 0         0 $template='<$PERCENT$>';
71 0 0       0 if(defined($args->{total})) {
72 0 0       0 $percent=$args->{total} ? $args->{percent}/$args->{total} : 0;
73             }
74             else {
75 0         0 $percent=$args->{percent};
76             }
77             }
78              
79             ##
80             # Displaying what we've got and any additional arguments
81             #
82 15         18 my $path=$args->{style};
83 15         17 my $text=$args->{text};
84 15 50       19 if($path)
85 0         0 { $path="/bits/styler/$path";
86 0         0 $template=undef;
87             }
88             else
89 15 50       23 { $template=$text unless $template;
90 15         17 $path=undef;
91             }
92             $self->object->display($args,{
93             path => $path,
94             template => $template,
95             TEXT => $text,
96             NUMBER => $number,
97             DOLLARS => $dollars,
98             REAL => $real,
99 15   100     36 PERCENT => sprintf($args->{format} || '%.2f%%', $percent*100),
100             });
101             }
102              
103             ############################## PRIVATE ########################################
104              
105             sub separate_thousands ($) {
106 45     45 0 50 my $value=shift;
107              
108 45 100       165 return $value unless $value =~ /^(\d+)(\.\d+)?$/;
109              
110 39         84 my ($i,$f)=($1,$2);
111              
112 39         119 1 while $i=~s/(\d)(\d{3}($|,))/$1,$2/;
113              
114 39 100       65 $i.=$f if defined $f;
115              
116 39         62 return $i;
117             }
118              
119             ###############################################################################
120             1;
121             __END__