Missing descriptions from POD NAME sections. #75994
[p5sagit/Excel-Template.git] / lib / Excel / Template / Context.pm
index 79c5415..77cd06b 100644 (file)
@@ -12,10 +12,10 @@ BEGIN {
 use Excel::Template::Format;
 
 # This is a helper object. It is not instantiated by the user, nor does it
-# represent an XML object. Rather, every container will use this object to
+# represent an XML node. Rather, every container will use this object to
 # maintain the context for its children.
 
-my %isAbsolute = map { $_ => 1 } qw(
+my %isAbsolute = map { $_ => ~~1 } qw(
     ROW
     COL
 );
@@ -26,16 +26,26 @@ sub new
     my $self = $class->SUPER::new(@_);
 
     $self->{ACTIVE_WORKSHEET} = undef;
-    $self->{ACTIVE_FORMAT}    = Excel::Template::Format->blank_format($self);
+    $self->{FORMAT_OBJECT}    = Excel::Template::Format->new;
+    $self->{ACTIVE_FORMAT}    = $self->{FORMAT_OBJECT}->blank_format($self);
+    $self->{WORKSHEET_NAMES}  = undef;
 
-    UNIVERSAL::isa($self->{$_}, 'ARRAY') || ($self->{$_} = [])
-        for qw( STACK PARAM_MAP NAME_MAP );
+    $self->{__MARKS} = {};
+
+    # Removed NAME_MAP until I figure out what the heck it's for
+    for (qw( STACK PARAM_MAP ))
+    {
+        next if defined $self->{$_} && ref $self->{$_} eq 'ARRAY';
+        $self->{$_} = [];
+    }
 
     $self->{$_} = 0 for keys %isAbsolute;
 
     return $self;
 }
 
+sub use_unicode { $_[0]->{UNICODE} && 1 }
+
 sub _find_param_in_map
 {
     my $self = shift;
@@ -43,15 +53,13 @@ sub _find_param_in_map
     $param = uc $param;
     $depth ||= 0;
 
-    my $val = undef;
-    my $found = 0;
-
+    my ($val, $found);
     for my $map (reverse @{$self->{$map}})
     {
         next unless exists $map->{$param};
         $depth--, next if $depth;
 
-        $found = 1;
+        $found = ~~1;
         $val = $map->{$param};
         last;
     }
@@ -71,14 +79,14 @@ sub param
     );
 }
 
-sub named_param
-{
-    my $self = shift;
-    $self->_find_param_in_map(
-        'NAME_MAP',
-        @_,
-    );
-}
+#sub named_param
+#{
+#    my $self = shift;
+#    $self->_find_param_in_map(
+#        'NAME_MAP',
+#        @_,
+#    );
+#}
 
 sub resolve
 {
@@ -102,7 +110,7 @@ sub resolve
     #    2) A decimal number
 
 #GGG Convert this to use //x
-    my ($op, $val) = $obj_val =~ m!^\s*([\+\*\/\-])?\s*([\d.]*\d)\s*$!oi;
+    my ($op, $val) = $obj_val =~ m/^\s*([\+\*\/\-])?\s*([\d.]*\d)\s*$/oi;
 
     # Unless it's a relative value, we have what we came for.
     return $obj_val unless $op;
@@ -115,7 +123,7 @@ sub resolve
     return $prev_val unless defined $obj_val;
 
     # Prevent divide-by-zero issues.
-    return $val if $op eq '/' and $val == 0;
+    return $prev_val if $op eq '/' and $val == 0;
 
     my $new_val;
     for ($op)
@@ -145,7 +153,7 @@ sub enter_scope
         $self->{$key} = $self->resolve($obj, $key);
     }
 
-    return 1;
+    return ~~1;
 }
 
 sub exit_scope
@@ -161,7 +169,7 @@ sub exit_scope
 
     pop @{$self->{STACK}};
 
-    return 1;
+    return ~~1;
 }
 
 sub get
@@ -209,17 +217,48 @@ sub active_format
 sub new_worksheet
 {
     my $self = shift;
-    my ($name) = @_;
+    my ($worksheet) = @_;
 
     $self->{ROW} = $self->{COL} = 0;
+    $self->{REFERENCES} = {};
+
+    my $name = $self->get( $worksheet, 'NAME' );
 
-    $self->active_worksheet(
-        $self->{XLS}->add_worksheet(
-            $name || '',
-        ),
+    if ( defined $name && length $name )
+    {
+        if ( exists $self->{WORKSHEET_NAMES}{$name} )
+        {
+            $name = '';
+        }
+        else
+        {
+            $self->{WORKSHEET_NAMES}{$name} = undef;
+        }
+    }
+    else
+    {
+        $name = '';
+    }
+
+    return $self->active_worksheet(
+        $self->{XLS}->add_worksheet( $name ),
     );
 }
 
+sub mark
+{
+    my $self = shift;
+
+    if ( @_ > 1 )
+    {
+        my %args = @_;
+
+        @{$self->{__MARKS}}{keys %args} = values %args;
+    }
+
+    return $self->{__MARKS}{$_[0]}
+}
+
 sub active_worksheet
 {
     my $self = shift;
@@ -230,32 +269,104 @@ sub active_worksheet
     $self->{ACTIVE_WORKSHEET};
 }
 
+sub add_reference
+{
+    my $self = shift;
+    my ($ref, $row, $col) = @_;
+
+    $self->{REFERENCES}{$ref} ||= [];
+
+    push @{$self->{REFERENCES}{$ref}}, [ $row, $col ];
+
+    return ~~1;
+}
+
+sub get_all_references
+{
+    my $self = shift;
+    my $ref = uc shift;
+
+    $self->{REFERENCES}{$ref} ||= [];
+
+    return @{ $self->{REFERENCES}{$ref} };
+}
+
+sub get_last_reference
+{
+    my $self = shift;
+    my $ref = uc shift;
+
+    $self->{REFERENCES}{$ref} ||= [];
+
+    return @{ $self->{REFERENCES}{$ref}[-1] };
+}
+
+sub format_object { $_[0]{FORMAT_OBJECT} }
+
 1;
 __END__
 
 =head1 NAME
 
-Excel::Template::Context
+Excel::Template::Context - Excel::Template::Context
 
 =head1 PURPOSE
 
+This is a helper node that provides the global context for the nodes do their processing within. It provides attribute scoping, parameter resolution, and other very nice things.
+
+Documentation is provided for if you wish to subclass another node.
+
 =head1 NODE NAME
 
+None
+
 =head1 INHERITANCE
 
+None
+
 =head1 ATTRIBUTES
 
+None
+
 =head1 CHILDREN
 
+None
+
 =head1 AFFECTS
 
+Everything
+
 =head1 DEPENDENCIES
 
-=head1 USAGE
+None
+
+=head1 METHODS
+
+=head2 active_format
+
+=head2 active_worksheet
+
+=head2 add_reference
+
+=head2 format_object
+
+=head2 get
+
+=head2 get_all_references
+
+=head2 get_last_reference
+
+=head2 mark
+
+=head2 new_worksheet
+
+=head2 param
+
+=head2 use_unicode
 
 =head1 AUTHOR
 
-Rob Kinyon (rkinyon@columbus.rr.com)
+Rob Kinyon (rob.kinyon@gmail.com)
 
 =head1 SEE ALSO