# Copyright (c) 2008 bivio Software, Inc. All Rights Reserved. # # Visit http://www.bivio.biz for more info. # # This library is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; If not, you may get a copy from: # http://www.opensource.org/licenses/lgpl-license.html # # $Id: TupleSlotType.pm,v 1.4 2009/11/20 17:50:22 nagler Exp $ package Bivio::Type::TupleSlotType; use strict; use Bivio::Base 'Type.TupleSlot'; our($VERSION) = sprintf('%d.%02d', q$Revision: 1.4 $ =~ /\d+/g); my($_IDI) = __PACKAGE__->instance_data_index; sub DEFAULT_CLASS { return 'String'; } sub as_string { my($self) = @_; return shift->SUPER::as_string(@_) unless ref($self); my($fields) = $self->[$_IDI]; my($s) = $fields->{choices}->to_literal($fields->{choices}); my($c) = $fields->{class}->simple_package_name; $c = $self->DEFAULT_CLASS if $c eq 'TupleSlot'; return $self->simple_package_name . '[' . $c . (length($s) ? ";$s" : '') . ']'; } sub get { return shift->[$_IDI]->{shift(@_)} || die; } sub from_literal { my($self) = shift; return $self->SUPER::from_literal(@_) unless ref($self); my($fields) = $self->[$_IDI]; my($v, $e) = $fields->{class}->from_literal(@_); return ($v, $e) unless defined($v); my($c) = $self->[$_IDI]->{choices}; return $v unless defined($c) && $c->is_specified; my($found); $c->do_iterate( sub {($found = $fields->{class}->is_equal(shift, $v)) ? 0 : 1}); return $found ? $v : (undef, Bivio::TypeError->NOT_FOUND); } sub new { my($self) = shift->SUPER::new; my($class, $choices) = @_; $self->[$_IDI] = { class => Bivio::Type->get_instance( $class eq $self->DEFAULT_CLASS ? 'TupleSlot' : $class), choices => $choices, }; return $self; } 1;