# Copyright 2002-2007 Interchange Development Group and others
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.  See the LICENSE file for details.

UserTag db-hash Order      table column key
UserTag db-hash PosNumber  3
UserTag db-hash addAttr
UserTag db-hash Version    1.5
UserTag db-hash Routine    <<EOR
sub {
	my($table, $col, $key, $opt) = @_;
	$col =~ s/:+(.*)//s;
	my $out;
	my $rest = $1;
	my $val = ::tag_data($table,$col,$key);

	my $ref;
	if ($val !~ /\S/) {
		$ref = {};
	}
	else {
		$ref = $Vend::Interpolate::ready_safe->reval($val);
		if (! ref $ref) {
			$ref = {};
		}
	}
	if (! $rest) {
		return $val unless defined $opt->{value};
	}
	my @extra;
	@extra = split /:+/, $rest;
	my $final = pop @extra;
	my $curr = $ref;
	$out .= "Original key request: $rest\n";
	$out .= "\nFinal key: $final\n";
	for(@extra) {
		$out .= "key --> $_\n";
		$curr = $curr->{$_};
		if (! ref $curr) {
			return "BAD HASH: $out" if $opt->{show_error};
			return;
		}
	}

	if($opt->{keys}) {
		return join get_joiner($opt->{joiner}), sort keys %$curr;
	}
	elsif(! defined $opt->{value}) {
		return $curr->{$final};
	}
	else {
		$curr->{$final} = $opt->{value};
		tag_data($table, $col, $key, { value => uneval_it($ref) });
		return $curr->{$final};
	}
}
EOR
