Merge branch 'moose-definition' into static_definition. Moved Asset.pm over to the new Definition.

This commit is contained in:
Colin Kuskie 2009-12-18 11:40:33 -08:00
commit ed0eeb9bc5
22 changed files with 2426 additions and 651 deletions

View file

@ -14,127 +14,78 @@ package WebGUI::Definition::Asset;
=cut
use strict;
use warnings;
use 5.010;
use base qw(WebGUI::Definition);
use WebGUI::International;
use WebGUI::Exception;
use Moose;
use Moose::Exporter;
use WebGUI::Definition ();
use WebGUI::Definition::Meta::Asset;
use namespace::autoclean;
no warnings qw(uninitialized);
our $VERSION = '0.0.1';
#-------------------------------------------------------------------
sub import {
my $class = shift;
if (! @_) {
return;
}
my $definition = (@_ == 1 && ref $_[0]) ? $_[0] : { @_ };
my $table = $definition->{tableName}
|| WebGUI::Error::InvalidParam->throw(param => 'tableName');
if ( my $properties = $definition->{properties} ) {
for ( my $i = 0; $i < $#{ $properties }; $i += 2) {
my ($name, $value) = @{ $properties }[$i, $i + 1];
$value->{tableName} ||= $table;
if ( ! $value->{tableName}|| ref $value->{tableName}) {
WebGUI::Error::InvalidParam->throw(param => 'tableName');
}
elsif ( ! $value->{fieldType} || ref $value->{fieldType}) {
WebGUI::Error::InvalidParam->throw(param => 'fieldType');
}
elsif ( ( ! $value->{noFormPost} || ref $value->{noFormPost} ) && ! $value->{label}) {
WebGUI::Error::InvalidParam->throw(param => 'label');
}
}
}
# WebGUI::Definition->import uses caller, so avoid the extra entry in the call stack
my $next = $class->next::can;
@_ = ($class, $definition);
goto $next;
}
#-------------------------------------------------------------------
sub _build {
my ($class, $super, $caller, $definition) = @_;
$class->next::method($super, $caller, $definition);
$class->_install($super, 'getTables', $class->_gen_getTables());
}
#-------------------------------------------------------------------
sub _gen_getTables {
my $class = shift;
return sub {
my $self = shift;
my %found;
my @tables;
foreach my $property ($self->getProperties) {
my $definition = $self->getProperty($property);
unless ($found{$definition->{tableName}}) {
push @tables, $definition->{tableName};
}
$found{$definition->{tableName}} = 1;
}
return @tables;
};
}
#-------------------------------------------------------------------
sub _gen_getProperty {
my $class = shift;
my $superGetProperty = $class->next::method(@_);
return sub {
my $self = shift;
my $property = $self->$superGetProperty(@_);
for my $element (qw(label hoverHelp)) {
if ($property->{$element} && ref $property->{$element} eq 'ARRAY') {
$property->{$element}
= WebGUI::International->new($self->session)->get(@{$property->{$element}});
}
}
return $property;
};
}
1;
__END__
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2009 Plain Black Corporation.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
http://www.plainblack.com info@plainblack.com
-------------------------------------------------------------------
=head1 NAME
WebGUI::Definition::Asset
Package WebGUI::Definition::Asset
=head1 DESCRIPTION
Extends WebGUI::Definition with asset specific methods and convienences. It automatically inserts the tableName attribute as an element of each property if the property doesn't explicitly set it.
Moose-based meta class for all Asset definitions in WebGUI.
=head1 SYNOPSIS
A definition contains all the information needed to build an object.
Information required to build forms are added as optional roles and
sub metaclasses. Database persistance is handled similarly.
=head1 METHODS
The following methods are exposed through this class.
These methods are available from this class:
=head2 getProperty ( property )
=cut
Extends getProperty() method generated by WebGUI::Definition by automatically converting the label and hoverHelp elements into strings.
my ($import, $unimport, $init_meta) = Moose::Exporter->build_import_methods(
install => [ 'unimport' ],
also => 'WebGUI::Definition',
with_meta => [ 'property' ],
);
=head2 getTables ( )
sub import {
my $class = shift;
my $caller = caller;
$class->$import({ into_level => 1 });
warnings->unimport('uninitialized');
namespace::autoclean->import( -cleanee => $caller );
return 1;
}
Returns a list of the tables that this asset's properties are stored in.
sub init_meta {
my $class = shift;
my %options = @_;
$options{metaclass} //= 'WebGUI::Definition::Meta::Asset';
my $meta = WebGUI::Definition->init_meta(%options);
Moose::Util::apply_all_roles($meta, 'WebGUI::Definition::Role::Asset');
return $meta;
}
#-------------------------------------------------------------------
=head2 property ( $name, %options )
Extends WebGUI::Definition::property to copy the tableName attribute from the
meta class into the options for each property.
=head3 $name
=head3 %options
=cut
sub property {
my ($meta, $name, %options) = @_;
$options{tableName} //= $meta->tableName;
return WebGUI::Definition::property($meta, $name, %options);
}
1;