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   1094 use strict;
  1         3  
  1         51  
22 1     1   8 use XAO::Utils qw(:args fround);
  1         2  
  1         179  
23 1     1   7 use XAO::Objects;
  1         2  
  1         44  
24 1     1   6 use base XAO::Objects->load(objname => 'Web::Page');
  1         2  
  1         6  
25              
26             our $VERSION='2.003';
27              
28             ###############################################################################
29              
30             sub separate_thousands ($);
31              
32             ###############################################################################
33              
34             sub display ($;%) {
35 15     15 1 31 my $self=shift;
36 15         71 my $args=get_args(\@_);
37              
38             ##
39             # Special formatting for special fields.
40             #
41             # number => 1,234,456,789
42             #
43 15 100       201 my $template="<%NUMBER%>" if defined($args->{number});
44 15   100     60 my $number=int($args->{number} || 0);
45 15 100       103 $number=separate_thousands($args->{'format'} ? sprintf($args->{'format'},$number) : $number);
46              
47             ##
48             # dollars => $1'234.78
49             #
50 15 100 66     81 $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     117 : sprintf('%.2f',fround($args->{dollars} || $args->{dollar} || 0,100));
      50        
54 15         180 $dollars='$' . separate_thousands($dollars);
55              
56             ##
57             # real => 1'234.78
58             #
59 15 100       76 $template='<$REAL$>' if defined($args->{real});
60             my $real=$args->{format}
61             ? sprintf($args->{format},$args->{real} || 0)
62 15 100 100     138 : sprintf("%.2f",$args->{real} || 0);
      100        
63 15         30 $real=separate_thousands($real);
64              
65             ##
66             # Percents
67             #
68 15         28 my $percent=0;
69 15 50       37 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         25 my $path=$args->{style};
83 15         28 my $text=$args->{text};
84 15 50       29 if($path)
85 0         0 { $path="/bits/styler/$path";
86 0         0 $template=undef;
87             }
88             else
89 15 50       32 { $template=$text unless $template;
90 15         59 $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     58 PERCENT => sprintf($args->{format} || '%.2f%%', $percent*100),
100             });
101             }
102              
103             ############################## PRIVATE ########################################
104              
105             sub separate_thousands ($) {
106 45     45 0 77 my $value=shift;
107              
108 45 100       259 return $value unless $value =~ /^(\d+)(\.\d+)?$/;
109              
110 39         156 my ($i,$f)=($1,$2);
111              
112 39         203 1 while $i=~s/(\d)(\d{3}($|,))/$1,$2/;
113              
114 39 100       86 $i.=$f if defined $f;
115              
116 39         99 return $i;
117             }
118              
119             ###############################################################################
120             1;
121             __END__