
package Text::Wigwam::Stash::Base::Metachar::Magic;

use strict;
use warnings;
no warnings q/uninitialized/;

use base 'Text::Wigwam::Stash::Base::Metachar';

our $VERSION = '0.01';

sub get {
    my ($self, $mgc, $val, $key, $ref) = @_;
    $key =~ s/\\(.)/$1/g;
#    return ( $val, $ref )
#      unless length $key;
    return $self->_ref_rref (
        $mgc->conjure($val, $key, $ref),
    );
}

sub set {
    my ($self, $mgc, $val, $key, $ref, $type, $def) = @_;
    my $v;
    $key =~ s/\\(.)/$1/g;
    return ($type, $self->_condense($val->$key))
      if blessed $val;
    if( ref $val eq 'HASH' ){
        return ( $type, $v )
          if defined($v = $val->{$key});
        return ( $type, $val->{$key} = $def );
    }
    if( ref $val eq 'ARRAY' ){
        return ( $type, $v )
          if defined($v = $val->[$key]);
        return ( $type, $val->[$key] = $def );
    }
    return( $type, $self->_condense($val->($key)) )
      if ref $val eq 'CODE';
    die "Encountered an unsupported data type ".ref($val);
}

sub def {
    my ($self, $mgc, $val, $key, $ref) = @_;
    $key =~ s/\\(.)/$1/g;
    return $self->_ref_rref (
        $mgc->conjure($val, $key, $ref),
    );
}

1;
