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;

View file

@ -0,0 +1,91 @@
package WebGUI::Definition::Meta::Asset;
=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
-------------------------------------------------------------------
=cut
use 5.010;
use Moose;
use namespace::autoclean;
use WebGUI::Definition::Meta::Property::Asset;
no warnings qw(uninitialized);
extends 'WebGUI::Definition::Meta::Class';
our $VERSION = '0.0.1';
=head1 NAME
Package WebGUI::Definition::Meta::Property::Asset
=head1 DESCRIPTION
Extends WebGUI::Definition::Meta::Class to provide
=head1 SYNOPSIS
Extends 'WebGUI::Definition::Meta::Class' to provide attributes specific to Assets.
=head1 METHODS
These methods are available from this class:
=cut
#-------------------------------------------------------------------
=head2 property_meta ( )
Asset Definitions use WebGUI::Definition::Meta::Property::Asset as the base class
for properties.
=cut
sub property_meta {
return 'WebGUI::Definition::Meta::Property::Asset';
}
has [ qw{tableName icon assetName} ] => (
is => 'rw',
);
#-------------------------------------------------------------------
=head2 tableName ( )
The table that this asset stores its properties in.
=cut
#-------------------------------------------------------------------
=head2 icon ( )
The filename of the icon for this Asset. Icons are stored in
www/extras/assets and are 48 x 48 pixels in size. A smaller version of
the icon, 16x16, is found in www/extras/assets/small.
=cut
#-------------------------------------------------------------------
=head2 assetName ( )
An array reference containing two items. The first is the i18n key for the asset's name.
The second is the i18n namespace to find the asset's name.
=cut
1;

View file

@ -0,0 +1,140 @@
package WebGUI::Definition::Meta::Class;
=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
-------------------------------------------------------------------
=cut
use 5.010;
use Moose;
use namespace::autoclean;
use WebGUI::Definition::Meta::Property;
no warnings qw(uninitialized);
extends 'Moose::Meta::Class';
our $VERSION = '0.0.1';
=head1 NAME
Package WebGUI::Definition::Meta::Class
=head1 DESCRIPTION
Moose-based meta class for all 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
These methods are available from this class:
=cut
#-------------------------------------------------------------------
=head2 get_all_properties ( )
Returns an array of all Properties, in all classes, in the order they were
created in the Definition.
=cut
sub get_all_properties {
my $self = shift;
my @properties = ();
CLASS: foreach my $className (reverse $self->linearized_isa()) {
my $meta = $self->initialize($className);
next CLASS unless $meta->isa('WebGUI::Definition::Meta::Class');
push @properties,
sort { $a->insertion_order <=> $b->insertion_order } # In insertion order
grep { $_->isa('WebGUI::Definition::Meta::Property') } # that are Meta::Properties
$meta->get_attributes # All attributes
;
}
return @properties;
}
#-------------------------------------------------------------------
=head2 get_attributes ( )
Returns an array of all attributes, but only for this class. This is the
API-safe way of doing $self->_attribute_map;
=cut
sub get_attributes {
my $self = shift;
return map { $self->find_attribute_by_name($_) } $self->get_attribute_list;
}
#-------------------------------------------------------------------
=head2 get_property_list ( )
Returns an array of the names of all Properties, in all classes, in the
order they were created in the Definition. Duplicate names are filtered
out.
=cut
sub get_property_list {
my $self = shift;
my @properties = ();
my %seen = ();
push @properties,
grep { ! $seen{$_}++ } # Uniqueness check
map { $_->name } # Just the name
$self->get_all_properties
;
return @properties;
}
#-------------------------------------------------------------------
=head2 get_tables ( )
Returns an array of the names of all tables in every class used by
this Class.
=cut
sub get_tables {
my $self = shift;
my @properties = ();
my %seen = ();
push @properties,
grep { ! $seen{$_}++ }
map { $_->tableName }
$self->get_all_properties
;
return @properties;
}
#-------------------------------------------------------------------
=head2 property_meta ( )
Returns the name of the class for properties.
=cut
sub property_meta {
return 'WebGUI::Definition::Meta::Property';
}
1;

View file

@ -0,0 +1,54 @@
package WebGUI::Definition::Meta::Property;
=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
-------------------------------------------------------------------
=cut
use 5.010;
use Moose;
use namespace::autoclean;
no warnings qw(uninitialized);
our $VERSION = '0.0.1';
=head1 NAME
Package WebGUI::Definition::Meta::Property
=head1 DESCRIPTION
Moose-based meta class for all properties in WebGUI::Definition.
=head1 SYNOPSIS
WebGUI::Definition::Meta::Property extends Moose::Meta::Attribute to include
a read-only form method, that provides the form properties for the attribute.
=cut
extends 'Moose::Meta::Attribute';
has 'form' => (
is => 'ro',
);
#-------------------------------------------------------------------
=head2 form ( )
Returns a hashref of propertes that are specific to WebGUI::Forms.
=cut
1;

View file

@ -0,0 +1,85 @@
package WebGUI::Definition::Meta::Property::Asset;
=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
-------------------------------------------------------------------
=cut
use 5.010;
use Moose;
use namespace::autoclean;
no warnings qw(uninitialized);
our $VERSION = '0.0.1';
=head1 NAME
Package WebGUI::Definition::Meta::Property::Asset
=head1 DESCRIPTION
Extends WebGUI::Definition::Meta::Property to provide Asset properties with
specific methods. The tableName and fieldType class properties must be defined.
=head1 METHODS
The following methods are added.
=cut
extends 'WebGUI::Definition::Meta::Property';
has 'tableName' => (
is => 'ro',
required => 1,
);
has 'fieldType' => (
is => 'ro',
required => 1,
);
has 'noFormPost' => (
is => 'ro',
);
#-------------------------------------------------------------------
=head2 tableName ( )
Previously, properties were storied in arrays of definitions, with each definition
providing its own attributes like table. This Moose based implementation stores
the properties flat, so the tableName attribute is copied into the property so we
know where to store it.
=cut
#-------------------------------------------------------------------
=head2 fieldType ( )
The type of HTML form field that this property should use to generate its UI
and validate its data.
=cut
#-------------------------------------------------------------------
=head2 noFormPost ( )
This is boolean which indicates that no data from HTML forms should be validated
and stored for this property.
=cut
1;

View file

@ -0,0 +1,27 @@
package WebGUI::Definition::Role::Asset;
=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
-------------------------------------------------------------------
=cut
use 5.010;
use Moose::Role;
use namespace::autoclean;
no warnings qw(uninitialized);
with 'WebGUI::Definition::Role::Asset';
our $VERSION = '0.0.1';
1;

View file

@ -0,0 +1,149 @@
package WebGUI::Definition::Role::Object;
=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
-------------------------------------------------------------------
=cut
use 5.010;
use Moose::Role;
use namespace::autoclean;
no warnings qw(uninitialized);
our $VERSION = '0.0.1';
=head1 NAME
Package WebGUI::Role::Object
=head1 DESCRIPTION
Moose-based role for providing classic WebGUI get/set style methods for objects.
This role is automatically included in all Definition objects.
=head1 SYNOPSIS
$obj->get('someProperty');
$obj->set({ someProperty => 'someValue' });
=head1 METHODS
These methods are available from this class:
=cut
#-------------------------------------------------------------------
=head2 get ( [ $name ] )
Generic accessor for this object's properties.
=head3 $name
If $name is defined, and is an attribute of the object, it returns the
value of the attribute. If $name is not an attribute, then it returns
undef.
If $name is not defined, it returns a hashref of all attributes.
=cut
sub get {
my $self = shift;
if (@_) {
my $property = shift;
if ($self->meta->find_attribute_by_name($property)) {
return $self->$property;
}
return undef;
}
my %properties = map { $_ => scalar $self->$_ } $self->meta->get_property_list;
return \%properties;
}
#-------------------------------------------------------------------
=head2 set ( dataSpec )
Generic setter for this object's properties.
=head3 dataSpec
Accepts either a hash, or a hash reference, of data to set in the object. If the key
is not an attribute of the object, then it is silently ignored.
=cut
sub set {
my $self = shift;
my $properties = @_ % 2 ? shift : { @_ };
KEY: for my $key ( keys %$properties ) {
next KEY unless $self->meta->find_attribute_by_name($key);
$self->$key($properties->{$key});
}
return 1;
}
#-------------------------------------------------------------------
=head2 update ( dataSpec )
Combines the actions of setting data in the object and writing the data.
=head3 dataSpec
See L<set>.
=cut
sub update {
my $self = shift;
$self->set(@_);
if ($self->can('write')) {
$self->write;
}
return 1;
}
#-------------------------------------------------------------------
=head2 getProperty ( $name )
Returns the requested property, which will be a subclass of Moose::Meta::Attribute.
=head3 $name
The name of the property to return.
=cut
sub getProperty {
my $self = shift;
return $self->meta->find_attribute_by_name(@_);
}
#-------------------------------------------------------------------
=head2 getProperties ( )
Returns a list of the names of all properties of the object, as set by the Definition.
=cut
sub getProperties {
my $self = shift;
return $self->meta->get_property_list;
}
1;