X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FExcel%2FTemplate%2FFactory.pm;h=477605ac90a6150a45d51a8b06d30931d919c0fd;hb=HEAD;hp=1fc9cd32baea27242e4cfbe15c172525fa626d46;hpb=c09684ff93144b943ad8786931bc51752e29bec3;p=p5sagit%2FExcel-Template.git diff --git a/lib/Excel/Template/Factory.pm b/lib/Excel/Template/Factory.pm index 1fc9cd3..477605a 100644 --- a/lib/Excel/Template/Factory.pm +++ b/lib/Excel/Template/Factory.pm @@ -2,11 +2,7 @@ package Excel::Template::Factory; use strict; -BEGIN { - use vars qw(%Manifest %isBuildable); -} - -%Manifest = ( +my %Manifest = ( # These are the instantiable nodes 'IF' => 'Excel::Template::Container::Conditional', @@ -16,13 +12,19 @@ BEGIN { 'WORKBOOK' => 'Excel::Template::Container::Workbook', 'WORKSHEET' => 'Excel::Template::Container::Worksheet', - 'CELL' => 'Excel::Template::Element::Cell', - 'FORMULA' => 'Excel::Template::Element::Formula', - 'VAR' => 'Excel::Template::Element::Var', + 'BACKREF' => 'Excel::Template::Element::Backref', + 'CELL' => 'Excel::Template::Element::Cell', + 'FORMULA' => 'Excel::Template::Element::Formula', + 'FREEZEPANES' => 'Excel::Template::Element::FreezePanes', + 'MERGE_RANGE' => 'Excel::Template::Element::MergeRange', + 'IMAGE' => 'Excel::Template::Element::Image', + 'RANGE' => 'Excel::Template::Element::Range', + 'VAR' => 'Excel::Template::Element::Var', 'FORMAT' => 'Excel::Template::Container::Format', # These are all the Format short-cut objects +# They are also instantiable 'BOLD' => 'Excel::Template::Container::Bold', 'HIDDEN' => 'Excel::Template::Container::Hidden', 'ITALIC' => 'Excel::Template::Container::Italic', @@ -31,8 +33,10 @@ BEGIN { 'SHADOW' => 'Excel::Template::Container::Shadow', 'STRIKEOUT' => 'Excel::Template::Container::Strikeout', -# These are the helper objects + 'KEEP_LEADING_ZEROS' => 'Excel::Template::Container::KeepLeadingZeros', +# These are the helper objects +# They are also in here to make E::T::Factory::isa() work. 'CONTEXT' => 'Excel::Template::Context', 'ITERATOR' => 'Excel::Template::Iterator', 'TEXTOBJECT' => 'Excel::Template::TextObject', @@ -43,88 +47,102 @@ BEGIN { 'BASE' => 'Excel::Template::Base', ); -%isBuildable = map { $_ => 1 } qw( - BOLD - CELL - FORMAT - FORMULA - IF - ITALIC - OUTLINE - LOOP - ROW - SHADOW - STRIKEOUT - VAR - WORKBOOK - WORKSHEET +my %isBuildable = map { $_ => ~~1 } qw( + WORKBOOK WORKSHEET + FORMAT BOLD HIDDEN ITALIC LOCKED OUTLINE SHADOW STRIKEOUT + IF ROW LOOP SCOPE KEEP_LEADING_ZEROS + CELL FORMULA FREEZEPANES IMAGE MERGE_RANGE + VAR BACKREF RANGE ); -sub register { - my %params = @_; - - my @param_names = qw(name class isa); - for (@param_names) + my %Loaded; + sub _load_class { - unless ($params{$_}) + my $self = shift; + my ($class) = @_; + + unless ( exists $Loaded{$class} ) { - warn "$_ was not supplied to register()\n"; - return 0; + (my $filename = $class) =~ s!::!/!g; + eval { + require "$filename.pm"; + }; if ($@) { + die "Cannot find or compile PM file for '$class' ($filename) because $@\n"; + } + + $Loaded{$class} = ~~1; } - } - my $name = uc $params{name}; - if (exists $Manifest{$name}) - { - warn "$params{name} already exists in the manifest.\n"; - return 0; + return ~~1; } +} - my $isa = uc $params{isa}; - unless (exists $Manifest{$isa}) +{ + my @param_names = qw(name class isa); + sub register { - warn "$params{isa} does not exist in the manifest.\n"; - return 0; - } + my $self = shift; + my %params = @_; - $Manifest{$name} = $params{class}; - $isBuildable{$name} = 1; + for (@param_names) + { + unless ($params{$_}) + { + warn "$_ was not supplied to register()\n" if $^W; + return; + } + } - { - no strict 'refs'; - unshift @{"$params{class}::ISA"}, $Manifest{$isa}; - } + my $name = uc $params{name}; + if (exists $Manifest{$name}) + { + warn "$params{name} already exists in the manifest.\n" if $^W; + return; + } - return 1; + my $isa = uc $params{isa}; + unless (exists $Manifest{$isa}) + { + warn "$params{isa} does not exist in the manifest.\n" if $^W; + return; + } + + { + no strict 'refs'; + unshift @{"$params{class}::ISA"}, $Manifest{$isa}; + } + + $self->_load_class( $Manifest{$isa} ); + $self->_load_class( $params{class} ); + + $Manifest{$name} = $params{class}; + $isBuildable{$name} = ~~1; + + return ~~1; + } } -sub create +sub _create { - my $class = shift; + my $self = shift; my $name = uc shift; return unless exists $Manifest{$name}; - (my $filename = $Manifest{$name}) =~ s!::!/!g; - - eval { - require "$filename.pm"; - }; if ($@) { - die "Cannot find or compile PM file for '$name' ($filename)\n"; - } + $self->_load_class( $Manifest{$name} ); return $Manifest{$name}->new(@_); } -sub create_node +sub _create_node { - my $class = shift; + my $self = shift; my $name = uc shift; return unless exists $isBuildable{$name}; - return $class->create($name, @_); + return $self->_create($name, @_); } sub isa @@ -135,28 +153,40 @@ sub isa : UNIVERSAL::isa(@_) } +sub is_embedded +{ + return unless @_ >= 1; + + isa( $_[0], $_ ) && return ~~1 for qw( VAR BACKREF RANGE ); + return; +} + 1; __END__ =head1 NAME -Excel::Template::Factory +Excel::Template::Factory - Excel::Template::Factory =head1 PURPOSE -=head1 NODE NAME +To provide a common way to instantiate Excel::Template nodes -=head1 INHERITANCE +=head1 USAGE -=head1 ATTRIBUTES +=head2 register() -=head1 CHILDREN +Use this to register your own nodes. -=head1 AFFECTS +Example forthcoming. -=head1 DEPENDENCIES +=head1 METHODS -=head1 USAGE +=head2 isa + +This is a customized isa() wrapper for syntactic sugar + +=head2 is_embedded =head1 AUTHOR