51ed783e2fb3b6caf233254064b3607f2732603e
[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} = [] unless UNIVERSAL::isa($self->{ELEMENTS}, 'ARRAY');
21
22     return $self;
23 }
24
25 sub _do_page
26 {
27     my $self = shift;
28     my ($method, $context) = @_;
29
30     for my $e (@{$self->{ELEMENTS}})
31     {
32         $e->enter_scope($context);
33         $e->$method($context);
34         $e->exit_scope($context, 1);
35     }
36
37     return 1;
38 }
39
40 sub begin_page { _do_page 'begin_page', @_ }
41 sub end_page   { _do_page 'end_page', @_   }
42
43 sub iterate_over_children
44 {
45     my $self = shift;
46     my ($context) = @_;
47
48     my $continue = 1;
49
50     for my $e (
51         @{$self->{ELEMENTS}})
52     {
53         $e->enter_scope($context);
54
55         my $rc = $e->render($context);
56         $continue = $rc if $continue;
57
58         $e->exit_scope($context);
59     }
60
61     return $continue;
62 }
63
64 sub render { $_[0]->iterate_over_children($_[1]) }
65 #{
66 #    my $self = shift;
67 #    my ($context) = @_;
68 #
69 #    return $self->iterate_over_children($context);
70 #}
71
72 sub max_of
73 {
74     my $self = shift;
75     my ($context, $attr) = @_;
76
77     my $max = $context->get($self, $attr);
78
79     ELEMENT:
80     foreach my $e (@{$self->{ELEMENTS}})
81     {
82         $e->enter_scope($context);
83
84         my $v = $e->isa('CONTAINER')
85             ? $e->max_of($context, $attr)
86             : $e->calculate($context, $attr);
87
88         $max = $v if $max < $v;
89
90         $e->exit_scope($context, 1);
91     }
92
93     return $max;
94 }
95
96 sub total_of
97 {
98     my $self = shift;
99     my ($context, $attr) = @_;
100
101     my $total = 0;
102
103     ELEMENT:
104     foreach my $e (@{$self->{ELEMENTS}})
105     {
106         $e->enter_scope($context);
107
108         $total += $e->isa('CONTAINER')
109             ? $e->total_of($context, $attr)
110             : $e->calculate($context, $attr);
111
112         $e->exit_scope($context, 1);
113     }
114
115     return $total;
116 }
117
118 1;
119 __END__
120
121 =head1 NAME
122
123 Excel::Template::Container
124
125 =head1 PURPOSE
126
127 =head1 NODE NAME
128
129 =head1 INHERITANCE
130
131 =head1 ATTRIBUTES
132
133 =head1 CHILDREN
134
135 =head1 AFFECTS
136
137 =head1 DEPENDENCIES
138
139 =head1 USAGE
140
141 =head1 AUTHOR
142
143 Rob Kinyon (rob.kinyon@gmail.com)
144
145 =head1 SEE ALSO
146
147 =cut