File Coverage

lib/Class/Meta/Declare.pm
Criterion Covered Total %
statement 21 146 14.3
branch 0 54 0.0
condition 0 15 0.0
subroutine 7 23 30.4
pod 4 4 100.0
total 32 242 13.2


line stmt bran cond sub pod time code
1             package Class::Meta::Declare;
2              
3 2     2   53553 use warnings;
  2         5  
  2         90  
4 2     2   10 use strict;
  2         6  
  2         65  
5 2     2   2549 use Class::Meta;
  2         40134  
  2         90  
6 2         17 use Class::BuildMethods qw/
7             accessors
8             cm
9 2     2   2951 /;
  2         7678  
10              
11             =head1 NAME
12              
13             Class::Meta::Declare - Deprecated in favor of Class::Meta::Express
14              
15             =head1 VERSION
16              
17             Version 0.04
18              
19             =cut
20              
21             our $VERSION = '0.04';
22              
23             =head1 SYNOPSIS
24              
25             This was a first attempt at making a saner interface for
26             L. It I nicer, but
27             L is nicer still. Go use that one.
28              
29             package MyApp::Thingy;
30             use Class::Meta::Declare ':all';
31             use Data::UUID;
32              
33             Class::Meta::Declare->new(
34             meta => [
35             key => 'thingy',
36             accessors => $ACC_SEMI_AFFORDANCE,
37             ],
38             attributes => [
39             pi => {
40             context => $CTXT_CLASS,
41             authz => $AUTHZ_READ,
42             default => 3.1415927,
43             },
44             id => {
45             authz => $AUTHZ_READ,
46             type => $TYPE_STRING,
47             default => sub { Data::UUID->new->create_str },
48             },
49             name => {
50             required => 1,
51             type => $TYPE_STRING,
52             default => 'No Name Supplied',
53             },
54             age => { type => $TYPE_INTEGER, },
55             ],
56             methods => [
57             some_method => {
58             view => $VIEW_PUBLIC,
59             code => sub {
60             my $self = shift;
61             return [ reverse @_ ];
62             },
63             }
64             ]
65             );
66              
67             my $object = MyApp::Thingy->new;
68             print MyApp::Thingy->pi; # prints 3.1415927
69             print $object->name; # prints "No Name Supplied';
70             $object->set_name("bob");
71             print $object->name; # prints "bob"
72              
73             =head1 DESCRIPTION
74              
75             This class provides an alternate interface for C.
76              
77             C is a useful module which allows one to create Perl classes
78             which support I (also known as I). Typically Perl
79             classes, when created, don't supply a lot of metadata. Imported helper
80             functions show up when you call C<< $object->can($method) >>. Private,
81             protected and trusted methods are not readily supported. Fetching a list of
82             attributes or methods is a haphazard affair. C overcomes these
83             shortcomings by building the classes for you and allowing you to fetch a class
84             object:
85              
86             my $class_object = $object->my_class;
87              
88             foreach my $attribute ( $class_object->attributes ) {
89             print $attribute->name, "\n";
90             }
91             foreach my $method ( $class_object->methods ) {
92             print $method->name, "\n";
93             }
94              
95             If you've set up your class correctly, these properties are now easy to
96             discover.
97              
98             Unfortunately, many find the C interface to be a bit clumsy. As
99             an alternative, C allows you to declare your entire
100             class in a single argument list to the constructor and have the class built
101             for you automatically. Further, reasonable defaults are provided for just
102             about everything.
103              
104             B: You want this class or C if you need an
105             introspection API for your classes. If you do not need introspection or
106             dynamic class generation, these modules are overkill.
107              
108             =head1 COMPARISON TO CLASS::META
109              
110             Consider the C example from the C:
111              
112             package MyApp::Thingy;
113             use Class::Meta::Declare ':all';
114             use Data::UUID;
115              
116             Class::Meta::Declare->new(
117             meta => [
118             key => 'thingy',
119             accessors => $ACC_SEMI_AFFORDANCE,
120             ],
121             attributes => [
122             pi => {
123             context => $CTXT_CLASS,
124             authz => $AUTHZ_READ,
125             default => 3.1415927,
126             },
127             id => {
128             authz => $AUTHZ_READ,
129             type => $TYPE_INTEGER,
130             default => sub { Data::UUID->new->create_str },
131             },
132             name => {
133             required => 1,
134             type => $TYPE_STRING,
135             default => 'No Name Supplied',
136             },
137             age => { type => $TYPE_INTEGER, },
138             ],
139             methods => [
140             some_method => {
141             view => $VIEW_PUBLIC,
142             code => sub {
143             my $self = shift;
144             return [ reverse @_ ];
145             },
146             }
147             ]
148             );
149              
150             Here's the equivalent C code:
151              
152             package MyApp::Thingy;
153             use Class::Meta;
154             use Class::Meta::Types::String 'semi-affordance';
155             use Class::Meta::Types::Numeric 'semi-affordance';
156             use Data::UUID;
157              
158             my $cm = Class::Meta->new( key => 'thingy' );
159              
160             $cm->add_constructor(
161             name => 'new',
162             create => 1,
163             );
164              
165             $cm->add_attribute(
166             name => 'pi',
167             context => Class::Meta::CLASS,
168             authz => Class::Meta::READ,
169             type => 'whole',
170             default => 3.1415927,
171             );
172              
173             $cm->add_attribute(
174             name => 'id',
175             authz => Class::Meta::READ,
176             type => 'integer',
177             default => sub { Data::UUID->new->create_str },
178             );
179              
180             $cm->add_attribute(
181             name => 'name',
182             required => 1,
183             type => 'string',
184             default => 'No Name Supplied',
185             );
186              
187             $cm->add_attribute(
188             name => 'age',
189             type => 'integer',
190             );
191              
192             sub some_method {
193             my $self = shift;
194             return [ reverse @_ ];
195             }
196              
197             $cm->add_method(
198             name => 'some_method',
199             view => Class::Meta::PUBLIC,
200             );
201              
202             $cm->build;
203              
204             As you can see, the C code is longer. The larger and more
205             complicated the class, the longer it gets. C offers the
206             following advantages:
207              
208             =over 4
209              
210             =item * Shorter code
211              
212             =item * Compile-time failures for many mistyped attribute values
213              
214             =item * Less duplication of information (for example, see C)
215              
216             =item * Helper classes for included C types are autoloaded
217              
218             =item * Sensible defaults for many entries
219              
220             =back
221              
222             =cut
223              
224 2     2   4046 use Readonly;
  2         8459  
  2         2225  
225              
226             # ACC
227              
228             Readonly our $ACC_PERL => '';
229             Readonly our $ACC_AFFORDANCE => 'affordance';
230             Readonly our $ACC_SEMI_AFFORDANCE => 'semi-affordance';
231              
232             # AUTHZ
233              
234             Readonly our $AUTHZ_READ => Class::Meta::READ;
235             Readonly our $AUTHZ_WRITE => Class::Meta::WRITE;
236             Readonly our $AUTHZ_RDWR => Class::Meta::RDWR;
237             Readonly our $AUTHZ_NONE => Class::Meta::NONE;
238              
239             # CREATE
240              
241             Readonly our $CREATE_GET => Class::Meta::GET;
242             Readonly our $CREATE_SET => Class::Meta::SET;
243             Readonly our $CREATE_GETSET => Class::Meta::GETSET;
244             Readonly our $CREATE_NONE => Class::Meta::NONE;
245              
246             # CTXT
247              
248             Readonly our $CTXT_CLASS => Class::Meta::CLASS;
249             Readonly our $CTXT_OBJECT => Class::Meta::OBJECT;
250              
251             # TYPE
252              
253             Readonly our $TYPE_SCALAR => 'scalar';
254             Readonly our $TYPE_SCALARREF => 'scalarref';
255             Readonly our $TYPE_ARRAY => 'array';
256             Readonly our $TYPE_ARRAYREF => 'arrayref';
257             Readonly our $TYPE_HASH => 'hash';
258             Readonly our $TYPE_HASHREF => 'hashref';
259             Readonly our $TYPE_CODE => 'code';
260             Readonly our $TYPE_CODEREF => 'coderef';
261             Readonly our $TYPE_CLOSURE => 'closure';
262             Readonly our $TYPE_STRING => 'string';
263             Readonly our $TYPE_BOOLEAN => 'boolean';
264             Readonly our $TYPE_BOOL => 'bool';
265             Readonly our $TYPE_WHOLE => 'whole';
266             Readonly our $TYPE_INTEGER => 'integer';
267             Readonly our $TYPE_DECIMAL => 'decimal';
268             Readonly our $TYPE_REAL => 'real';
269             Readonly our $TYPE_FLOAT => 'float';
270              
271             # VIEW
272              
273             Readonly our $VIEW_PUBLIC => Class::Meta::PUBLIC;
274             Readonly our $VIEW_PRIVATE => Class::Meta::PRIVATE;
275             Readonly our $VIEW_TRUSTED => Class::Meta::TRUSTED;
276             Readonly our $VIEW_PROTECTED => Class::Meta::PROTECTED;
277              
278             # start type lookup
279              
280             my %TYPE_CLASS_FOR = map { $_ => 'Class::Meta::Types::Perl' } (
281             $TYPE_SCALAR, $TYPE_SCALARREF, $TYPE_ARRAY,
282             $TYPE_ARRAYREF, $TYPE_HASH, $TYPE_HASHREF,
283             $TYPE_CODE, $TYPE_CODEREF, $TYPE_CLOSURE,
284             );
285             $TYPE_CLASS_FOR{$TYPE_STRING} = 'Class::Meta::Types::String';
286             $TYPE_CLASS_FOR{$TYPE_BOOL} = 'Class::Meta::Types::Boolean';
287             $TYPE_CLASS_FOR{$TYPE_BOOLEAN} = 'Class::Meta::Types::Boolean';
288              
289             foreach my $type (
290             $TYPE_WHOLE, $TYPE_INTEGER, $TYPE_DECIMAL, $TYPE_REAL,
291             $TYPE_FLOAT
292             )
293             {
294             $TYPE_CLASS_FOR{$type} = 'Class::Meta::Types::Numeric';
295             }
296              
297             # end type lookup
298              
299 2         24 use Exporter::Tidy acc => [
300             qw/
301             $ACC_PERL
302             $ACC_AFFORDANCE
303             $ACC_SEMI_AFFORDANCE
304             /
305             ],
306             authz => [
307             qw/
308             $AUTHZ_READ
309             $AUTHZ_WRITE
310             $AUTHZ_RDWR
311             $AUTHZ_NONE
312             /
313             ],
314             create => [
315             qw/
316             $CREATE_GET
317             $CREATE_SET
318             $CREATE_GETSET
319             $CREATE_NONE
320             /
321             ],
322             ctxt => [
323             qw/
324             $CTXT_CLASS
325             $CTXT_OBJECT
326             /
327             ],
328             type => [
329             qw/
330             $TYPE_SCALAR
331             $TYPE_SCALARREF
332             $TYPE_ARRAY
333             $TYPE_ARRAYREF
334             $TYPE_HASH
335             $TYPE_HASHREF
336             $TYPE_CODE
337             $TYPE_CODEREF
338             $TYPE_CLOSURE
339             $TYPE_STRING
340             $TYPE_BOOLEAN
341             $TYPE_BOOL
342             $TYPE_WHOLE
343             $TYPE_INTEGER
344             $TYPE_DECIMAL
345             $TYPE_REAL
346             $TYPE_FLOAT
347             /
348             ],
349             view => [
350             qw/
351             $VIEW_PUBLIC
352             $VIEW_PRIVATE
353             $VIEW_TRUSTED
354             $VIEW_PROTECTED
355             /
356 2     2   2949 ];
  2         22  
357              
358             ##############################################################################
359              
360             =head1 CLASS METHODS
361              
362             =head2 new
363              
364             Class::Meta::Declare->new(%options);
365              
366             The C method allows you to build an entire class, with reflective
367             capabilities, just like C. However, the syntax is shorter,
368             hopefully clearer, and it builds everything in one go.
369              
370             See C for details on C<%options>.
371              
372             =cut
373              
374             sub new {
375 0     0 1   my $class = shift;
376 0           my $self = $class->_init(@_);
377 0           $self->cm->build;
378 0           return $self;
379             }
380              
381             ##############################################################################
382              
383             =head2 create
384              
385             my $declare = Class::Meta::Declare->create(\%options);
386             my $class_meta = $declare->cm;
387             # more Class::Meta stuff
388             $class_meta->build;
389              
390             This constructor is exactly the same as C except it does not call
391             C's C method. Use this constructor if you have more stuff
392             you need to do prior to C being called.
393              
394             =cut
395              
396             sub create {
397 0     0 1   my $class = shift;
398 0           my $self = $class->_init(@_);
399 0           return $self;
400             }
401              
402             sub _init {
403 0     0     my $class = shift;
404 0           my $self = bless {}, $class;
405 0           my %declaration_for = @_;
406 0           foreach my $type (qw/meta constructors attributes methods/) {
407 0   0       $declaration_for{$type} ||= [];
408             }
409 0 0 0       if ( exists $declaration_for{constructors}
  0            
410             && @{ $declaration_for{constructors} } )
411             {
412 0           push @{ $declaration_for{meta} }, _no_constructor => 1;
  0            
413             }
414 0           $self->_set_cm( delete $declaration_for{meta} );
415 0           $self->_add_constructors( delete $declaration_for{constructors} );
416 0           $self->_add_attributes( delete $declaration_for{attributes} );
417 0           $self->_add_methods( delete $declaration_for{methods} );
418 0           return $self;
419             }
420              
421             ##############################################################################
422              
423             =head1 INSTANCE METHODS
424              
425             =head2 cm
426              
427             my $cm = $declare->cm;
428              
429             Returns the C object used to build the the class.
430              
431             =head2 installed_types
432              
433             my @types = $declare->installed_types;
434             if ($declare->installed_types('Class::Meta::Type::Numeric')) { ... }
435              
436             Returns a list of data types used. If passed a data type, returns a boolean
437             value indicating whether or not that type was used.
438              
439             =cut
440              
441             {
442             my %is_installed;
443              
444             sub installed_types {
445 0     0 1   my $self = shift;
446 0   0       $is_installed{ $self->accessors } ||= {};
447 0 0         return sort keys %{ $is_installed{ $self->accessors } } unless @_;
  0            
448 0           return $is_installed{ $self->accessors }{ +shift };
449             }
450              
451             sub _set_installed {
452 0     0     my ( $self, $package ) = @_;
453 0           $is_installed{ $self->accessors }{$package} = 1;
454             }
455             }
456              
457             ##############################################################################
458              
459             =head2 package
460              
461             my $package = $declare->package;
462              
463             Returns the package for which the C code was declared.
464              
465             =cut
466              
467 0     0 1   sub package { shift->cm->class->package }
468              
469             sub _set_cm {
470 0     0     my ( $self, $meta ) = @_;
471 0           my %value_for = @$meta;
472              
473 0   0       $value_for{package} ||= $self->_get_call_pack;
474 0   0       $self->accessors( delete $value_for{accessors} || $ACC_PERL );
475 0   0       my $build = delete $value_for{use} || 'Class::Meta';
476 0           eval "use $build";
477 0 0         if ( my $error = $@ ) {
478 0           $self->_croak("Cannot use $build as building class: $error");
479             }
480              
481 0           my $cm = $build->new(%value_for);
482              
483             # If they've defined their own constructors, we had better not build a
484             # default.
485 0 0         unless ( $value_for{_no_constructor} ) {
486 0           $cm->add_constructor(
487             name => 'new',
488             create => 1
489             );
490             }
491 0           $self->cm($cm);
492 0           return $self;
493             }
494              
495             sub _get_call_pack {
496 0     0     my $self = shift;
497              
498 0           my $call_level = 1;
499 0           my $call_pack;
500 0           while ( !$call_pack ) {
501 0           ($call_pack) = caller($call_level);
502 0 0         last unless $call_pack;
503 0           $call_level++;
504 0 0         undef $call_pack if $call_pack->isa(__PACKAGE__);
505             }
506 0 0         return $call_pack
507             or $self->_croak("Could not determine package");
508             }
509              
510             sub _add_constructors {
511 0     0     my ( $self, $constructors ) = @_;
512 0           while ( my $constructor = shift @$constructors ) {
513 0           my $definition_for = shift @$constructors;
514 0           $definition_for->{name} = $constructor;
515 0 0         $definition_for->{create} = exists $definition_for->{code} ? 0 : 1;
516 0 0         if ( my $code = delete $definition_for->{code} ) {
517 0           $self->_install_method( $constructor, $code );
518             }
519 0           $self->cm->add_constructor(%$definition_for);
520             }
521 0           return $self;
522             }
523              
524             sub _add_attributes {
525 0     0     my ( $self, $attributes ) = @_;
526              
527 0           while ( my $attribute = shift @$attributes ) {
528 0           my $definition_for = shift @$attributes;
529              
530             # set defaults
531 0           $definition_for->{name} = $attribute;
532 0 0         $definition_for->{type} = $TYPE_SCALAR
533             unless exists $definition_for->{type};
534              
535             # figure out the class for the type
536 0           my $type_class = $TYPE_CLASS_FOR{ $definition_for->{type} };
537 0 0         unless ($type_class) {
538 0           my $class = Class::Meta->for_key( $definition_for->{type} );
539 0 0         $type_class = $class->package if $class;
540             }
541 0 0         $self->_croak("Could not find type class for $definition_for->{type}")
542             unless defined $type_class;
543              
544             # set attribute interface type (e.g., 'affordance')
545 0 0         unless ( $self->installed_types($type_class) ) {
546 0           my $accessors = $self->accessors;
547 0 0         $accessors = "'$accessors'" if $accessors;
548 0           eval "use $type_class $accessors";
549 0 0         if ( my $error = $@ ) {
550 0           $self->_croak("Could not load $type_class: $error");
551             }
552 0           $self->_set_installed($type_class);
553             }
554              
555             # add the attributes
556 0 0         if ( exists $definition_for->{code} ) {
557 0           $self->_install_attribute_code(
558             $attribute,
559             delete $definition_for->{code}
560             );
561 0           $definition_for->{create} = $CREATE_NONE;
562             }
563              
564 0           eval { $self->cm->add_attribute(%$definition_for) };
  0            
565 0 0         if ( my $error = $@ ) {
566 0           $self->_croak("Setting attribute for $attribute failed: $error");
567             }
568             }
569 0           return $self;
570             }
571              
572             sub _add_methods {
573 0     0     my ( $self, $methods ) = @_;
574              
575 0           while ( my $name = shift @$methods ) {
576 0           my $definition_for = shift @$methods;
577 0 0         if ( exists $definition_for->{code} ) {
578              
579             # the "code" slot is not required as the sub may already exist via
580             # direct implementation or via "autoload".
581 0           $self->_install_method( $name, delete $definition_for->{code} );
582             }
583 0           $definition_for->{name} = $name;
584 0           eval { $self->cm->add_method(%$definition_for) };
  0            
585 0 0         if ( my $error = $@ ) {
586 0           $self->_croak("Adding method for $name failed: $error");
587             }
588             }
589 0           return $self;
590             }
591              
592             my %accessor_builder_for = (
593             $ACC_PERL => \&_install_perl_accessors,
594             $ACC_SEMI_AFFORDANCE => \&_install_affordance_accessors,
595             $ACC_AFFORDANCE => \&_install_affordance_accessors,
596             );
597              
598             sub _install_attribute_code {
599 0     0     my ( $self, $attribute, $code ) = @_;
600 0 0         my $code_installer = $accessor_builder_for{ $self->accessors }
601             or $self->_croak(
602 0           "I don't know how to install methods for @{[$self->accessors]}");
603 0           $self->$code_installer( $attribute, $code );
604 0           return $self;
605             }
606              
607             sub _install_method {
608 0     0     my ( $self, $method, $code ) = @_;
609 0 0         unless ( 'CODE' eq ref $code ) {
610 0           $self->_croak("Value for $method is not a coderef");
611             }
612 0           my $package = $self->package;
613 2     2   3580 no strict 'refs';
  2         5  
  2         1052  
614 0           *{"${package}::$method"} = $code;
  0            
615             }
616              
617             sub _install_perl_accessors {
618 0     0     my ( $self, $attribute, $code ) = @_;
619 0 0         unless ( 'CODE' eq ref $code ) {
620 0           $self->_croak(
621             "'code' value for Perl-style accessors must be a coderef");
622             }
623 0           $self->_install_method( $attribute, $code );
624 0           return $self;
625             }
626              
627             sub _install_affordance_accessors {
628 0     0     my ( $self, $attribute, $code ) = @_;
629 0           my $accessor_style = $self->accessors;
630 0 0         unless ( 'HASH' eq ref $code ) {
631 0           $self->_croak(
632             "'code' value for $accessor_style accessors must be a hashref");
633             }
634 0 0         my $get_prefix = $accessor_style =~ /^semi/ ? '' : 'get_';
635 0 0         my $get = $code->{get}
636             or $self->_croak("No 'get' method supplied for $attribute");
637 0           $self->_install_method( "$get_prefix$attribute", $get );
638 0 0         my $set = $code->{set}
639             or $self->_croak("No 'set' method supplied for $attribute");
640 0           $self->_install_method( "set_$attribute", $set );
641 0           return $self;
642             }
643              
644             sub _croak {
645 0     0     my ( $class, $message ) = @_;
646 0           require Carp;
647 0           Carp::croak $message;
648             }
649              
650             =head1 CONSTRUCTOR OPTIONS
651              
652             The constructor takes an even-sized list of name/value declarations. Each name
653             should be one of C, C, C or C. Each
654             declaration should be an array reference of with key/value pairs in it (in
655             other words, it's like a hashref but because it's in an array reference, we
656             preserve the element order). Each key is optional, but supplying no keys
657             pretty much means you have an empty class (though you will get a default
658             constructor).
659              
660             The following lists the key/value options for each declaration.
661              
662             =head2 meta
663              
664             Note that all keys for C are optional.
665              
666             =over 4
667              
668             =item * key
669              
670             This specifies the "class key" underwhich you may fetch a new instance of a
671             class object:
672              
673             my $class_object = Class::Meta->for_key($key);
674              
675             See L's "for_key".
676              
677             =item * package
678              
679             Building a class assumes the class is to be built in the current package. You
680             may override this with a package parameter.
681              
682             meta => [
683             key => 'foo',
684             package => 'Foo',
685             ]
686              
687             =item * accessors
688              
689             This key specifies the getter/setter style which will be built for attributes.
690             Perl-style getter/setters look like this:
691              
692             my $name = $object->name;
693             $object->name('Bob');
694              
695             You may also specify "semi-affordance" style accessors with
696             C<$ACC_SEMI_AFFORDANCE>:
697              
698             my $name = $object->name;
699             $object->set_name('Bob');
700              
701             You may also specify "affordance" style accessors with
702             C<$ACC_AFFORDANCE>:
703              
704             my $name = $object->get_name;
705             $object->set_name('Bob');
706              
707             This meta declaration thus might look like this:
708              
709             meta => [
710             accessors => $ACC_SEMI_AFFORDANCE
711             ]
712              
713             Note that the accessors parameter has no value on data types not supplied by
714             C unless they have been written to recognize them.
715              
716             =item * use
717              
718             By default, we assume that C is the build class. If you have
719             subclassed C (or done something really bizarre like creating an
720             alternative with an identical interface), you may specify that class with the
721             C key:
722              
723             meta => [
724             use => "Class::Meta::Subclass",
725             ]
726              
727             Note that C is an alternate interface, not a subclass of
728             C.
729              
730             =back
731              
732             =head3 C defaults
733              
734             =over 4
735              
736             =item * C
737              
738             C<$ACC_PERL>
739              
740             =item * C
741              
742             Defaults to value of C key.
743              
744             =item * C
745              
746             Calling package.
747              
748             =item * C
749              
750             C
751              
752             =back
753              
754             =head2 constructors
755              
756             By default, a C constructor is created for you. If you pass a
757             C declaration, the default constructor will not be built and all
758             constructor creation will be up to you.
759              
760             Each constructor must have a key which specifies the name of the constructor
761             and point to a hashref containing additional information about the
762             constructor. An empty hashref will simply create a constructor with the given
763             name, so the default constructor which is provided by C
764             in the absense of a C declaration is simply:
765              
766             constructors => [
767             new => {}
768             ]
769              
770             The values of the hashref should match the values identified in the
771             C "add_constructor" documentation. C is not required (and
772             will be ignored if supplied) as name is taken from the hashref key. C
773             should be on of the values listed in the C ":view" section of this
774             documentation.
775              
776             The actual body of the constructor, if supplied, should be provided with the
777             C key.
778              
779             So to create factory constructor, one might do this (the following example
780             assumes that the two factory classes listed are subclasses of the current
781             class):
782              
783             package MyClass;
784             use Class::Meta::Declare;
785             Class::Meta::Declare->new(
786             constructors => [
787             new => {}, # we can have multiple constructors
788             factory => {
789             view => $VIEW_PUBLIC, # optional as this is the default
790             code => sub {
791             my ($class, $target) = @_;
792             $class = $target eq 'foo'
793             ? 'Subclass::Foo'
794             : 'Subclass::Bar';
795             return bless {}, $class;
796             }
797             }
798             ]
799              
800             And later you'll be able to do this:
801              
802             my $object = MyClass->new;
803             print ref $object; # MyClass
804              
805             $object = MyClass->factory('foo');
806             print ref $object; # Subclass::Foo
807              
808             =head3 C defaults:
809              
810             =over 4
811              
812             =item * C
813              
814             C<$VIEW_PUBLIC>.
815              
816             =item * C
817              
818             If C is provided, false, otherwise true.
819              
820             Note that if you supply a C slot, its value will be ignored in favor
821             of the "default" create value.
822              
823             =back
824              
825             =head2 attributes
826              
827             Each attribute must have a key which specifies the name of the attribute
828             and point to a hashref containing additional information about the
829             attribute. An empty hashref will create a simple scalar attribute with the
830             given name, so a basic getter/setter with no validation is simply:
831              
832             attributes => [
833             some_attribute => {}
834             ]
835              
836             The values of the hashref should match the values identified in the
837             C "add_attribute" documentation. C is not required (and
838             will be ignored if supplied) as name is taken from the hashref key. C
839             should be on of the values listed in the C ":view" section of this
840             documentation.
841              
842             The C should be one of the datatypes specified in the C ":type"
843             section. Note that unlike C, you do not have to load the type
844             class. C will infer the type class from the type you
845             provide and handle this for you.
846              
847             The C and C values should be one of their corresponding values
848             in the C section of this document.
849              
850             The C key indicates whether this is a class or instance attribute.
851             It's value should be either C<$CTXT_CLASS> or C<$CTXT_OBJECT>.
852              
853             =head3 C defaults:
854              
855             =over 4
856              
857             =item * C
858              
859             Set to the value of the "key" for the attribute:
860              
861             rank => { # name will be set to 'rank'
862             default => 'private',
863             }
864              
865             =item * C
866              
867             C<$TYPE_SCALAR>.
868              
869             =item * C
870              
871             False.
872              
873             =item * C
874              
875             False.
876              
877             =item * C
878              
879             None.
880              
881             =item * C
882              
883             None.
884              
885             =item * C
886              
887             C<$VIEW_PUBLIC>.
888              
889             =item * C
890              
891             C<$AUTHZ_RDWR>.
892              
893             =item * C
894              
895             Value corresponding to value in C slot.
896              
897             =item * C
898              
899             C<$CTXT_OBJECT>.
900              
901             =item * C
902              
903             None (Ironic, eh?)
904              
905             =item * C
906              
907             False.
908              
909             =back
910              
911             =head3 Custom Accessors
912              
913             If you wish to provide custom attribute accessors, the actual body of the
914             accessor should be provided with the C key. If this is done, the
915             C value will automatically be set to C<$CREATE_NONE>. This tells
916             C to not create attribute accessor for you, but to use the code
917             you have supplied.
918              
919             There are two ways to create custom attribute code depending on the
920             accessor style you have chosen. If you are using regular "perl style"
921             accessors (the default), then C should point to a code reference or an
922             anonymous sub:
923              
924             password => { # insecure code for demonstration purposes only
925             code => sub {
926             my $self = shift;
927             return $self->{password} unless @_;
928             my $password = shift;
929             if (length $password < 5) {
930             croak "Password too short";
931             }
932             $self->{password} = $password;
933             return $self;
934             }
935             }
936              
937             However, if you are using C<$ACC_SEMI_AFFORDANCE> or C<$ACC_AFFORDANCE> style
938             accessors, then you'll have separate I and I methods. C
939             should then point to a hash reference with C and C as the keys and
940             their values pointing to their corresponding methods.
941              
942             meta => [
943             accessors => $ACC_SEMI_AFFORDANCE,
944             ],
945             attributes => [
946             password => { # insecure code for demonstration purposes only
947             code => {
948             get => sub { shift->{password} },
949             set => sub {
950             my ($self, $password) = @_;
951             if (length $password < 5) {
952             croak "Password too short";
953             }
954             $self->{password} = $password;
955             return $self;
956             }
957             }
958             }
959             ]
960              
961             For the code above, you may then access the attribute via
962             C<< $object->password >> and C<< $object->set_password($password) >>.
963              
964             =head3 Custom Types
965              
966             You may find the built-in list of types insufficient for your needs. For
967             example, you may wish to create an accessor which only accepts types of class
968             C. In this case, C should be a C or
969             C class and should be loaded prior to C being
970             called. C should then point to the C key.
971              
972             Class::Meta::Declare->new(
973             meta => [
974             key => 'customer',
975             package => 'Customer',
976             ],
977             @customer_attributes,
978             @customer_methods
979             );
980              
981             And later:
982              
983             Class::Meta::Declare->new(
984             meta => [
985             key => 'some_key',
986             package => 'Some::Package',
987             ],
988             attributes => [
989             cust => {
990             type => 'customer',
991             }
992             ]
993             );
994              
995             =head2 methods
996              
997             Each method must have a key which specifies the name of the method and point
998             to a hashref containing additional information about the method. Each hashref
999             should contain, at minimum, a C key which points to a subref of anonymous
1000             subroutine which defines the method:
1001              
1002             methods => [
1003             reverse_name => sub {
1004             my $self = shift;
1005             return scalar reverse $self->name;
1006             }
1007             ]
1008              
1009             The values of the hashref should match the values identified in the
1010             C "add_method" documentation. C is not required (and will
1011             be ignored if supplied) as name is taken from the hashref key. C should
1012             be on of the values listed in the C ":view" section of this
1013             documentation.
1014              
1015             The C key indicates whether this is a class or instance method. It's
1016             value should be either C<$CTXT_CLASS> or C<$CTXT_OBJECT>. The default is
1017             C<$CTXT_OBJECT>.
1018              
1019             The actual body of the method, if supplied, should be provided with the
1020             C key. If it's not supplied, it is assumed the the method will still
1021             be available at runtime. This is if the method is declared elsewhere or will
1022             be provided via C or similar functionality.
1023              
1024             =head3 C defaults:
1025              
1026             =over 4
1027              
1028             =item * C
1029              
1030             Set to the value of the "key" for the method:
1031              
1032             rank => { # name will be set to 'rank'
1033             code => \&some_method,
1034             }
1035              
1036             =item * C
1037              
1038             None.
1039              
1040             =item * C
1041              
1042             None.
1043              
1044             =item * C
1045              
1046             C<$VIEW_PUBLIC>.
1047              
1048             =item * C
1049              
1050             C<$CTXT_OBJECT>.
1051              
1052             =item * C
1053              
1054             None.
1055              
1056             =item * C
1057              
1058             None.
1059              
1060             =item * C
1061              
1062             None.
1063              
1064             =back
1065              
1066             =head1 EXPORT
1067              
1068             C exports a number of constants on demand. These
1069             constants are used to provide a simpler interface for C use.
1070              
1071             See L for details on where to use
1072             these.
1073              
1074             =head2 :acc
1075              
1076             Foreach each class, you can specify the type of attribute accessors created.
1077             Defaults to "perl-style" accessors.
1078              
1079             See the "Accessors" section for C.
1080              
1081             See also:
1082              
1083             L
1084              
1085             L
1086              
1087             L
1088              
1089             =over 4
1090              
1091             =item * $ACC_PERL
1092              
1093             =item * $ACC_AFFORDANCE
1094              
1095             =item * $ACC_SEMI_AFFORDANCE
1096              
1097             =back
1098              
1099             =head2 :authz
1100              
1101             Sets the authorization for each attribute, determining whether people can read
1102             or write to a given accessor. Defaults to C.
1103              
1104             See L.
1105              
1106             =over 4
1107              
1108             =item * $AUTHZ_READ
1109              
1110             =item * $AUTHZ_WRITE
1111              
1112             =item * $AUTHZ_RDWR
1113              
1114             =item * $AUTHZ_NONE
1115              
1116             =back
1117              
1118             =head2 :create
1119              
1120             Indicates what type of accessor or accessors are to be created for the
1121             attribute. Generally sets a sensible default based upon the C setting.
1122              
1123             See the "create" section under L.
1124              
1125             =over 4
1126              
1127             =item * $CREATE_GET
1128              
1129             =item * $CREATE_SET
1130              
1131             =item * $CREATE_GETSET
1132              
1133             =item * $CREATE_NONE
1134              
1135             =back
1136              
1137             =head2 :ctxt
1138              
1139             For each attribute, you may specify if it is a class or instance attribute.
1140              
1141             See the "context" section under L.
1142              
1143             =over 4
1144              
1145             =item * $CTXT_CLASS
1146              
1147             =item * $CTXT_OBJECT
1148              
1149             =back
1150              
1151             =head2 :type
1152              
1153             Sets the data type for each attribute. Setting an attribute to an illegal data
1154             type is a fatal error. This list of data types covers all that are supplied
1155             with C. If you use others, you'll have to specify them
1156             explicitly.
1157              
1158             See L.
1159              
1160             =over 4
1161              
1162             =item * $TYPE_SCALAR
1163              
1164             =item * $TYPE_SCALARREF
1165              
1166             =item * $TYPE_ARRAY
1167              
1168             =item * $TYPE_ARRAYREF
1169              
1170             =item * $TYPE_HASH
1171              
1172             =item * $TYPE_HASHREF
1173              
1174             =item * $TYPE_CODE
1175              
1176             =item * $TYPE_CODEREF
1177              
1178             =item * $TYPE_CLOSURE
1179              
1180             =item * $TYPE_STRING
1181              
1182             =item * $TYPE_BOOLEAN
1183              
1184             =item * $TYPE_BOOL
1185              
1186             =item * $TYPE_WHOLE
1187              
1188             =item * $TYPE_INTEGER
1189              
1190             =item * $TYPE_DECIMAL
1191              
1192             =item * $TYPE_REAL
1193              
1194             =item * $TYPE_FLOAT
1195              
1196             =back
1197              
1198             =head2 :view
1199              
1200             Sets the "visibility" of a constructor, attribute, or method.
1201              
1202             See the "view" section under L.
1203              
1204             =over 4
1205              
1206             =item * $VIEW_PUBLIC
1207              
1208             =item * $VIEW_PRIVATE
1209              
1210             =item * $VIEW_TRUSTED
1211              
1212             =item * $VIEW_PROTECTED
1213              
1214             =back
1215              
1216             =head1 AUTHOR
1217              
1218             Curtis "Ovid" Poe, C<< >>
1219              
1220             =head1 BUGS
1221              
1222             Please report any bugs or feature requests to
1223             C, or through the web interface at
1224             L.
1225             I will be notified, and then you'll automatically be notified of progress on
1226             your bug as I make changes.
1227              
1228             =head1 ACKNOWLEDGEMENTS
1229              
1230             Thanks to Kineticode, Inc, L for sponsoring this
1231             work.
1232              
1233             =head1 SEE ALSO
1234              
1235             L
1236              
1237             =head1 DEPENDENCIES
1238              
1239             L
1240              
1241             L
1242              
1243             L
1244              
1245             L
1246              
1247             =head1 COPYRIGHT & LICENSE
1248              
1249             Copyright 2005 Curtis "Ovid" Poe, all rights reserved.
1250              
1251             This program is free software; you can redistribute it and/or modify it
1252             under the same terms as Perl itself.
1253              
1254             =cut
1255              
1256             1;