# Copyright (c) 1999-2007 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: Boolean.pm,v 2.5 2010/08/25 17:02:54 moeller Exp $ package Bivio::Type::Boolean; use strict; use Bivio::Base 'Bivio::Type::Integer'; our($VERSION) = sprintf('%d.%02d', q$Revision: 2.5 $ =~ /\d+/g); sub FALSE { return 0; } sub TRUE { return 1; } sub can_be_negative { return 0; } sub can_be_positive { return 1; } sub can_be_zero { return 1; } sub from_literal { my($res, $err) = shift->SUPER::from_literal(@_); if ($err) { my($v) = @_; if ($v =~ /^\s*(?:y|yes|t|true|on)\s*$/i) { $res = 1; } elsif ($v =~ /^\s*(?:n|no|f|false|off)\s*$/i) { $res = 0; } } # Booleans are never non-null. Always returns 0 or 1 or error. return defined($res) ? $res : $err ? ($res, $err) : 0; } sub get_decimals { return 0; } sub get_default { return 0; } sub get_max { return 1; } sub get_min { return 0; } sub get_precision { return 1; } sub get_width { return 1; } sub to_sql_param { shift; my($v) = shift; return !defined($v) ? undef : $v ? '1' : '0'; } sub to_xml { my($proto, $value) = @_; return !defined($value) ? '' : $value ? 'true' : 'false'; } 1;