df966d94408c8f225127102664a4ee7eac3269e7
[p5sagit/Excel-Template.git] / lib / Excel / Template / Container.pm
1 package Excel::Template::Container;
2
3 use strict;
4
5 BEGIN {
6     use vars qw(@ISA);
7     @ISA = qw(Excel::Template::Base);
8
9     use Excel::Template::Base;
10 }
11
12 # Containers are objects that can contain arbitrary elements, such as
13 # PageDefs or Loops.
14
15 sub new
16 {
17     my $class = shift;
18     my $self = $class->SUPER::new(@_);
19
20     $self->{ELEMENTS} = []
21         unless exists $self->{ELEMENTS} &&
22             UNIVERSAL::isa($self->{ELEMENTS}, 'ARRAY');
23
24     return $self;
25 }
26
27 sub _do_page
28 {
29     my $self = shift;
30     my ($method, $context) = @_;
31
32     for my $e (@{$self->{ELEMENTS}})
33     {
34         $e->enter_scope($context);
35         $e->$method($context);
36         $e->exit_scope($context, 1);
37     }
38
39     return 1;
40 }
41
42 sub begin_page { _do_page 'begin_page', @_ }
43 sub end_page   { _do_page 'end_page', @_   }
44
45 sub iterate_over_children
46 {
47     my $self = shift;
48     my ($context) = @_;
49
50     my $continue = 1;
51
52     for my $e (
53         @{$self->{ELEMENTS}})
54     {
55         $e->enter_scope($context);
56
57         my $rc = $e->render($context);
58         $continue = $rc if $continue;
59
60         $e->exit_scope($context);
61     }
62
63     return $continue;
64 }
65
66 sub render { $_[0]->iterate_over_children($_[1]) }
67 #{
68 #    my $self = shift;
69 #    my ($context) = @_;
70 #
71 #    return $self->iterate_over_children($context);
72 #}
73
74 sub max_of
75 {
76     my $self = shift;
77     my ($context, $attr) = @_;
78
79     my $max = $context->get($self, $attr);
80
81     ELEMENT:
82     foreach my $e (@{$self->{ELEMENTS}})
83     {
84         $e->enter_scope($context);
85
86         my $v = $e->isa('CONTAINER')
87             ? $e->max_of($context, $attr)
88             : $e->calculate($context, $attr);
89
90         $max = $v if $max < $v;
91
92         $e->exit_scope($context, 1);
93     }
94
95     return $max;
96 }
97
98 sub total_of
99 {
100     my $self = shift;
101     my ($context, $attr) = @_;
102
103     my $total = 0;
104
105     ELEMENT:
106     foreach my $e (@{$self->{ELEMENTS}})
107     {
108         $e->enter_scope($context);
109
110         $total += $e->isa('CONTAINER')
111             ? $e->total_of($context, $attr)
112             : $e->calculate($context, $attr);
113
114         $e->exit_scope($context, 1);
115     }
116
117     return $total;
118 }
119
120 1;
121 __END__
122
123 =head1 NAME
124
125 Excel::Template::Container
126
127 =head1 PURPOSE
128
129 =head1 NODE NAME
130
131 =head1 INHERITANCE
132
133 =head1 ATTRIBUTES
134
135 =head1 CHILDREN
136
137 =head1 AFFECTS
138
139 =head1 DEPENDENCIES
140
141 =head1 USAGE
142
143 =head1 AUTHOR
144
145 Rob Kinyon (rob.kinyon@gmail.com)
146
147 =head1 SEE ALSO
148
149 =cut