diff --git a/lib/WebGUI/Definition/Role/Object.pm b/lib/WebGUI/Definition/Role/Object.pm index 70a2aeb72..a921ba31a 100644 --- a/lib/WebGUI/Definition/Role/Object.pm +++ b/lib/WebGUI/Definition/Role/Object.pm @@ -21,30 +21,91 @@ 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->can($property)) { + if ($self->meta->find_attribute_by_name($property)) { return $self->$property; } return undef; } - my %properties = map { $_ => scalar $self->$_ } $self->meta->get_all_properties; + 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 : { @_ }; - for my $key ( keys %$properties ) { - return undef - unless $self->can($key); + 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. + +=cut + + sub update { my $self = shift; $self->set(@_); @@ -54,14 +115,34 @@ sub update { return 1; } +#------------------------------------------------------------------- + +=head2 getProperty ( dataSpec ) + +Returns a list of all properties of the object, as set by the Definition. + +=head3 dataSpec + +See L. + +=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_all_properties; + return $self->meta->get_property_list; } 1;