1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
#========================================================================
#
# Badger::Data
#
# DESCRIPTION
# Base class module for objects representing various different data
# types.
#
# AUTHOR
# Andy Wardley <abw@wardley.org>
#
#========================================================================
package Badger::Data;
use Badger::Class
version => 0.01,
debug => 0,
base => 'Badger::Prototype',
import => 'class CLASS',
utils => 'md5_hex self_params refaddr',
auto_can => 'method', # anything in $METHODS can become a method
constants => 'HASH TRUE FALSE',
constant => {
# tell Badger what class to strip off to generate id()
base_id => __PACKAGE__,
id => 'type',
},
alias => {
# alias type() to id() method provided by Badger::Base
type => 'id',
};
# Define a lexical scope with a $METADATA table for storing any out-of-band
# information about text objects. The methods defined below can be called
# as subroutines (as part of the vmethod mechanism), so the first argument
# can be a non-reference text string in place of the usual $self object
# reference (which itself is just a blessed reference to a scalar text
# string). To handle this case, we use an md5_hex encoding of the text
# to determine a unique handle for it (or close enough to unique for
# practical purposes)
{
my $METADATA = { };
sub metadata {
my $meta = $METADATA->{
$_[0] && ref $_[0]
? refaddr $_[0]
: md5_hex $_[0]
} ||= { };
# short-cut: return metadata hash when called without arguments
return $meta if @_ == 1;
# short-cut: return item in the metadata hash when called with a
# single (non-hashref) item
return $meta->{ $_[1] } if @_ == 2 && ref $_[1] ne HASH;
# add metadata items when called with a HASH ref or multiple args
my ($self, $params) = self_params(@_);
@$meta{ keys %$params } = values %$params;
return $meta;
}
}
# This is a throwback to the Template::TT3::Type object on which this is
# based... these methods probably won't be staying here - they should be
# in Badger::Data::Type
our $METHODS = {
method => \&method, # TODO: can() as alias to method()?
methods => \&methods,
type => \&type,
ref => \&ref,
def => \&defined,
undef => \&undefined,
defined => \&defined,
undefined => \&undefined,
true => \&true,
false => \&false,
};
sub init {
my ($self, $config) = @_;
# merge everything in $config into $self for now
@$self{ keys %$config } = values %$config;
# merge all config methods with class $METHODS
$self->{ methods } = $self->class->hash_vars(
METHODS => $config->{ methods }
);
return $self;
}
sub method {
my $self = shift->prototype;
# return item from hash or the hash itself when called without arguments
return @_
? $self->{ methods }->{ $_[0] }
: $self->{ methods };
}
sub methods {
my $self = shift->prototype;
# return hash ref when called without argument
return $self->{ methods } unless @_;
# add items to hash when called with hash ref or multiple args
my $items = @_ == 1 && ref $_[0] eq HASH ? shift : { @_ };
my $hash = $self->{ methods };
@$hash{ keys %$items } = values %$items;
return $hash;
}
sub ref {
return CORE::ref($_[0]);
}
sub defined {
CORE::defined $_[0] ? TRUE : FALSE;
}
sub undefined {
CORE::defined $_[0] ? FALSE : TRUE;
}
sub true {
$_[0] ? $_[0] : FALSE;
}
sub false {
$_[0] ? FALSE : TRUE;
}
1;
__END__
=head1 NOTE
This is being merged in from Template::TT3::Type. The documentation still
refers to the old name and relates to TT-specific use.
=head1 NAME
Badger::Data - base class for data object
=head1 SYNOPSIS
# defining a subclass data type
package Badger::Data::Thing;
use base 'Badger::Data';
our $METHODS = {
wibble => \&wibble,
wobble => \&wobble,
};
sub wibble {
my $self = shift;
# some wibble code...
}
sub wobble {
my $self = shift;
# some wobble code...
}
=head1 PLEASE NOTE
This module is being merged in from the prototype C<Template-TT3> code. The
implementation is subject to change and the documentation may be incomplete or
incorrect in places.
=head1 DESCRIPTION
The C<Badger::Data> module implements a base class for the
L<Badger::Data::Text>, L<Badger::Data::List> and L<Badger::Data::Hash> data
objects.
=head1 METHODS
The following methods are defined in addition to those inherited from
L<Badger::Prototype> and L<Badger::Base>.
=head2 init(\%config)
Initialialisation method to handle any per-object initialisation. This is
called by the L<new()|Badger::Base/new()> method inherited from
L<Badger::Base> . In this base class, the method simply copies all items in
the C<$config> hash array into the C<$self> object.
=head2 clone()
Create a copy of the current object.
my $clone = $object->clone();
Additional named parameters can be provided. These are merged with
the items defined in the parent object and passed to the cloned
object's L<init()> method.
my $clone = $object->clone( g => 0.577 );
=head2 methods()
Returns a reference to a hash array containing the content of the
C<$METHODS> package variable in the current class and any base classes.
my $methods = $object->methods;
=head2 method($name)
Returns a reference to a particular method from the hash reference
returned by the L<methods()> method.
my $method = $object->method('ref');
When called without any arguments, it returns a reference to the
entire hash reference, as per L<methods()>.
my $method = $object->method->{ foo };
=head2 metadata($name,$value)
This method provides access to an out-of-band (i.e. stored separately from the
data itself) hash array of metadata for the data item. It returns a reference
to a hash array when called without arguments.
# fetch metadata hash and add an entry
my $metadata = $data->metadata;
$metadata->{ author } = 'Arthur Dent';
# later... print the metadata
print $data->metadata->{ author };
It returns the value of an item in the metadata hash when called with a single
argument.
print $data->metadata('author');
It sets the value of an item when called with two arguments.
$data->metadata( author => 'Ford Prefect' );
=head2 ref()
Returns the name of the object type, e.g. C<Template::TT3::Type>,
C<Template::TT3::Type::Text>, L<Template::TT3::Type::List>, etc., exactly as
Perl's C<ref()> function does.
=head2 defined()
Returns a true/false (C<1>/C<0>) value to indicate if the target data is
defined.
=head2 undefined()
Returns a true/false (C<1>/C<0>) value to indicate if the target data is
undefined.
=head2 true()
Returns a true/false (C<1>/C<0>) value to indicate if the target data has
a true value (using by Perl's definition of what constitutes truth).
=head2 false()
Returns a true/false (C<1>/C<0>) value to indicate if the target data has
a false value (using by Perl's definition of what constitutes truth).
=head1 AUTHOR
Andy Wardley L<http://wardley.org/>
=head1 COPYRIGHT
Copyright (C) 1996-2008 Andy Wardley. All Rights Reserved.
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=head1 SEE ALSO.
L<Template::TT3::Type::Text>, L<Template::TT3::Type::List> and L<Template::TT3::Type::Hash>.
=cut
# Local Variables:
# mode: perl
# perl-indent-level: 4
# indent-tabs-mode: nil
# End:
#
# vim: expandtab shiftwidth=4:
|