[interchange-cvs] interchange - heins modified 2 files

interchange-core@icdevgroup.org interchange-core@icdevgroup.org
Sat Aug 3 00:21:00 2002


User:      heins
Date:      2002-08-03 04:20:30 GMT
Modified:  lib/Vend Config.pm Interpolate.pm
Log:
* Add ability to override some routines in the list iterators on
  a global, per-catalog, or per-loop basis.

  Routines that can be overridden:

	PREFIX-accessories
	PREFIX-common
	PREFIX-description
	PREFIX-field
	PREFIX-last
	PREFIX-next
	PREFIX-options

  Each is separately overridden for Array and Hash routines. None
  of the pricing routines are done because they are available many
  other ways and the interdependencies are too great.

  To override global, with global code permissions, in interchange.cfg:

  CodeDef options HashCode <<EOR
  sub {
  	my (@args) =3D @_;
	return &somestuff;
  }
  EOR

  That will override only for [item-list ...] and [tree ...] and
  [query list=3D1 hashref=3D1 ...].

  To override per-catalog, same thing except subject to Safe.

  To override on a per-loop basis, you map a Sub or GlobalSub to
  the tag.

  GlobalSub sub my_description { return "Uh " . shift }
  GlobalSub sub my_common      { return join (" ", reverse(@_)) }

	and

  [loop list=3D"1 2 3"
  		maproutine=3D|
			description=3Dmy_description
			common=3Dmy_common
		|
		][loop-description] [loop-common potato]<br>
  [/loop]

  	or

  [loop list=3D"1 2 3"
  		maproutine.description=3D"globalsub_description"
  		maproutine.common=3D"globalsub_common"
		][loop-description] | [loop-common potato]<br>
  [/loop]

   Will return:

       Uh 1 | 1 potato
       Uh 2 | 2 potato
       Uh 3 | 3 potato

   As a reminder, there is another powerful tag-mapping routine that
   has existed for quite a while. In the above code, you could do:

   	   [loop-tag-time adjust=3D"[loop-code] min"]%H:%M[/loop-tag-time]

	That will be called just like

	   [time adjust=3D"[loop-code] min"]%H%M[/time]

	but not incur the additional parse overhead and copying.

Revision  Changes    Path
2.60      +24 -13    interchange/lib/Vend/Config.pm


rev 2.60, prev_rev 2.59
Index: Config.pm
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /anon_cvs/repository/interchange/lib/Vend/Config.pm,v
retrieving revision 2.59
retrieving revision 2.60
diff -u -r2.59 -r2.60
--- Config.pm	2 Aug 2002 13:09:34 -0000	2.59
+++ Config.pm	3 Aug 2002 04:20:30 -0000	2.60
@@ -1,6 +1,6 @@
 # Vend::Config - Configure Interchange
 #
-# $Id: Config.pm,v 2.59 2002/08/02 13:09:34 racke Exp $
+# $Id: Config.pm,v 2.60 2002/08/03 04:20:30 mheins Exp $
 #
 # Copyright (C) 1996-2002 Red Hat, Inc. <interchange@redhat.com>
 #
@@ -44,7 +44,7 @@
 use Vend::Parse;
 use Vend::Util;
=20
-$VERSION =3D substr(q$Revision: 2.59 $, 10);
+$VERSION =3D substr(q$Revision: 2.60 $, 10);
=20
 my %CDname;
=20
@@ -3108,6 +3108,8 @@
=20
 	group			Group
 	actionmap		ActionMap
+	arraycode		ArrayCode
+	hashcode		HashCode
 	coretag  		CoreTag
 	filter			Filter
 	formaction		FormAction
@@ -3163,14 +3165,16 @@
=20
 my %current_dest;
 my %valid_dest =3D qw/
-                    actionmap  ActionMap
-                    coretag    UserTag
-                    filter     Filter
-                    formaction FormAction
-                    itemaction ItemAction
-                    ordercheck OrderCheck
-                    usertag    UserTag
-                    widget     Widget
+					actionmap        ActionMap
+					coretag          UserTag
+					filter           Filter
+					formaction       FormAction
+					itemaction       ItemAction
+					ordercheck       OrderCheck
+					usertag          UserTag
+					hashcode         HashCode
+					arraycode        ArrayCode
+					widget           Widget
 				/;
=20
 sub finalize_mapped_code {
@@ -3205,6 +3209,13 @@
 	}
 }
=20
+my %Compiled =3D qw/
+					Routine     Routine
+					PosRoutine  PosRoutine
+					HashCode    Routine
+					ArrayCode   Routine
+				/;
+
 sub parse_mapped_code {
 	my ($var, $value) =3D @_;
=20
@@ -3234,9 +3245,9 @@
=20
 	my $c =3D $repos->{$dest};
=20
-	if($p eq 'Routine') {
-		$c->{Routine} ||=3D {};
-		parse_action($var, "$tag $val", $c->{Routine});
+	if($Compiled{$p}) {
+		$c->{$Compiled{$p}} ||=3D {};
+		parse_action($var, "$tag $val", $c->{$Compiled{$p}} ||=3D {});
 	}
 	elsif(defined $tagAry{$p}) {
 		my(@v) =3D Text::ParseWords::shellwords($val);



2.99      +91 -14    interchange/lib/Vend/Interpolate.pm


rev 2.99, prev_rev 2.98
Index: Interpolate.pm
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /anon_cvs/repository/interchange/lib/Vend/Interpolate.pm,v
retrieving revision 2.98
retrieving revision 2.99
diff -u -r2.98 -r2.99
--- Interpolate.pm	2 Aug 2002 18:52:55 -0000	2.98
+++ Interpolate.pm	3 Aug 2002 04:20:30 -0000	2.99
@@ -1,6 +1,6 @@
 # Vend::Interpolate - Interpret Interchange tags
 #=20
-# $Id: Interpolate.pm,v 2.98 2002/08/02 18:52:55 mheins Exp $
+# $Id: Interpolate.pm,v 2.99 2002/08/03 04:20:30 mheins Exp $
 #
 # Copyright (C) 1996-2002 Red Hat, Inc. <interchange@redhat.com>
 #
@@ -27,7 +27,7 @@
 require Exporter;
 @ISA =3D qw(Exporter);
=20
-$VERSION =3D substr(q$Revision: 2.98 $, 10);
+$VERSION =3D substr(q$Revision: 2.99 $, 10);
=20
 @EXPORT =3D qw (
=20
@@ -4307,6 +4307,74 @@
 	return $where;
 }
=20
+use vars qw/%Ary_code/;
+%Ary_code =3D (
+	accessories =3D> \&tag_accessories,
+	common =3D> \&Vend::Data::product_common,
+	description =3D> \&Vend::Data::product_description,
+	field =3D> \&Vend::Data::product_field,
+	last =3D> \&interpolate_html,
+	next =3D> \&interpolate_html,
+	options =3D> \&tag_options,
+	tag =3D> \&tag_dispatch,
+);
+
+use vars qw/%Hash_code/;
+%Hash_code =3D (
+	accessories =3D> \&tag_accessories,
+	common =3D> \&Vend::Data::item_common,
+	description =3D> \&Vend::Data::item_description,
+	field =3D> \&Vend::Data::item_field,
+	last =3D> \&interpolate_html,
+	next =3D> \&interpolate_html,
+	options =3D> \&tag_options,
+	tag =3D> \&tag_dispatch,
+);
+
+sub map_list_routines {
+	my($type, $opt) =3D @_;
+
+	### This allows mapping of new routines to=20
+	##    PREFIX-options
+	##    PREFIX-accessories
+	##    PREFIX-description
+	##    PREFIX-common
+	##    PREFIX-field
+	##    PREFIX-price
+	##    PREFIX-tag
+	##    PREFIX-last
+	##    PREFIX-next
+
+	my $nc;
+
+	my $ac;=20
+	for $ac ($Global::CodeDef->{$type}, $Vend::Cfg->{CodeDef}{$type}) {
+		next unless $ac and $ac->{Routine};
+		$nc ||=3D {};
+		for(keys %{$ac->{Routine}}) {
+			$nc->{$_} =3D $ac->{Routine}{$_};
+		}
+	}
+
+	if($ac =3D $opt->{maproutine}) {
+		$nc ||=3D {};
+		if(! ref($ac) ) {
+			$ac =3D~ s/\s+$//;
+			$ac =3D~ s/^\s+//;
+			$ac =3D { split /[\s,=3D\0]/, $ac };
+		}
+		$ac =3D {} if ref($ac) ne 'HASH';
+		for(keys %$ac) {
+			$nc->{$_} =3D $Vend::Cfg->{Sub}{$_} || $Global::GlobalSub->{$_}
+			  or do {
+				  logError("%s: non-existent mapped routine %s.", $type, $_);
+					delete $nc->{$_};
+			  };
+		}
+	}
+	return $nc;
+}
+
 sub iterate_array_list {
 	my ($i, $end, $count, $text, $ary, $opt_select, $fh, $opt) =3D @_;
=20
@@ -4321,6 +4389,10 @@
 		return $r;
 	}
=20
+	my $nc =3D map_list_routines('ArrayCode', $opt);
+
+	$nc and local(@Ary_code{keys %$nc}) =3D values %$nc;
+
 	my ($run, $row, $code, $return);
 my $once =3D 0;
 #::logDebug("iterating array $i to $end. count=3D$count opt_select=3D$opt_=
select ary=3D" . ::uneval($ary));
@@ -4416,13 +4488,13 @@
 		$run =3D~ s:$B$QR{_line}:join "\t", @{$row}[ ($1 || 0) .. $#$row]:ige;
 	    $run =3D~ s:$B$QR{_increment}:$count:ig;
 		$run =3D~ s:$B$QR{_accessories}:
-						tag_accessories($code,$1,{}):ige;
+						$Ary_code{accessories}->($code,$1,{}):ige;
 		$run =3D~ s:$B$QR{_options}:
-						tag_options($code,$1):ige;
+						$Ary_code{options}->($code,$1):ige;
 		$run =3D~ s:$B$QR{_code}:$code:ig;
-		$run =3D~ s:$B$QR{_description}:ed(product_description($code)):ige;
-		$run =3D~ s:$B$QR{_field}:ed(product_field($1, $code)):ige;
-		$run =3D~ s:$B$QR{_common}:ed(product_common($1, $code)):ige;
+		$run =3D~ s:$B$QR{_description}:ed($Ary_code{description}->($code)):ige;
+		$run =3D~ s:$B$QR{_field}:ed($Ary_code{field}->($1, $code)):ige;
+		$run =3D~ s:$B$QR{_common}:ed($Ary_code{common}->($1, $code)):ige;
 		tag_labeled_data_row($code, \$run);
 		$run =3D~ s!$B$QR{_price}!
 					currency(product_price($code,$1), $2)!ige;
@@ -4444,7 +4516,7 @@
 				#ige;
 		$run =3D~ s#$B$QR{_filter}$E$QR{'/_filter'}#filter_value($1,$2)#ige;
 		$run =3D~ s#$B$QR{_last}$E$QR{'/_last'}#
-                    my $tmp =3D interpolate_html($1);
+                    my $tmp =3D $Ary_code{last}->($1);
 					$tmp =3D~ s/^\s+//;
 					$tmp =3D~ s/\s+$//;
                     if($tmp && $tmp < 0) {
@@ -4455,7 +4527,7 @@
                     }
                     '' #ixge;
 		$run =3D~ s#$B$QR{_next}$E$QR{'/_next'}#
-                    interpolate_html($1) !=3D 0 ? next : '' #ixge;
+                    $Ary_code{next}->($1) !=3D 0 ? next : '' #ixge;
 		$run =3D~ s/<option\s*/<OPTION SELECTED /i
 			if $opt_select and $opt_select->($code);
=20
@@ -4482,6 +4554,10 @@
 	my $code_field =3D $opt->{code_field} || 'mv_sku';
 	my ($run, $code, $return, $item);
=20
+	my $nc =3D map_list_routines('HashCode', $opt);
+
+	$nc and local(@Hash_code{keys %$nc}) =3D values %$nc;
+
 #::logDebug("iterating hash $i to $end. count=3D$count opt_select=3D$opt_s=
elect hash=3D" . ::uneval($hash));
 	while($text =3D~ s#$B$QR{_sub}$E$QR{'/_sub'}##i) {
 		my $name =3D $1;
@@ -4567,10 +4643,11 @@
 				  $item->{$3}	?	pull_if($5,$2,$4,$item->{$3})
 								:	pull_else($5,$2,$4,$item->{$3})#ge;
 		$run =3D~ s:$B$QR{_increment}:$i + 1:ge;
+=09=09
 		$run =3D~ s:$B$QR{_accessories}:
-						tag_accessories($code,$1,{},$item):ge;
+						$Hash_code{accessories}->($code,$1,{},$item):ge;
 		$run =3D~ s:$B$QR{_options}:
-						tag_options($item,$1):ige;
+						$Hash_code{options}->($item,$1):ige;
 		$run =3D~ s:$B$QR{_sku}:$code:ig;
 		$run =3D~ s:$B$QR{_code}:$item->{code}:ig;
 		$run =3D~ s:$B$QR{_quantity}:$item->{quantity}:g;
@@ -4587,10 +4664,10 @@
 								$1
 								)!ge;
 		$run =3D~ s:$B$QR{_code}:$code:g;
-		$run =3D~ s:$B$QR{_field}:ed(item_field($item, $1) || $item->{$1}):ge;
-		$run =3D~ s:$B$QR{_common}:ed(item_common($item, $1) || $item->{$1}):ge;
+		$run =3D~ s:$B$QR{_field}:ed($Hash_code{field}->($item, $1) || $item->{$=
1}):ge;
+		$run =3D~ s:$B$QR{_common}:ed($Hash_code{common}->($item, $1) || $item->=
{$1}):ge;
 		$run =3D~ s:$B$QR{_description}:
-							ed(item_description($item) || $item->{description})
+							ed($Hash_code{description}->($item) || $item->{description})
 							:ge;
 		$run =3D~ s!$B$QR{_price}!currency(item_price($item,$1), $2)!ge;
 		$run =3D~ s!$B$QR{_discount_price}!