blib/lib/Oryx/MetaClass.pm untested

filter: Statementstmt 0.0% 0 / 21 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 53 CC 5 SCAR 13.6
CC
5
Cov
0%
meta
6.0
getMetaAttribute
6.0
_carp
6.0
CRAP 3.9
1 package Oryx::MetaClass;
2  
3 # okay, so it's not really a meta-class in the true sense since it
4 # doesn't get instantiated. Instead, it gets subclassed, but we use
5 # inheritable class data to achieve the same effect. This is
6 # because we're not trying to create our own meta-model so much as
7 # trying to squeeze a relational data model into Perl's in-built
8 # meta-model. Subclassing this via Perl's inheritance mechanism
9 # preserves class state as if the sub class were a Class instance of a
10 # MetaClass (which it is, just not this one... Perl's). There is
11 # meta-data associated with the class which is in the form of a DOM
12 # Node which was used to define the schema in the first place (and
13 # which is passed into the constructor of whichever entity derives
14 # from this class).
15  
16 use Carp qw(carp croak cluck);
17 use base qw(Class::Data::Inheritable);
18  
19 __PACKAGE__->mk_classdata("storage");
20 __PACKAGE__->mk_classdata("schema");
21  
22 =head1 NAME
23  
24 Oryx::MetaClass - abstract base class for all Oryx meta types
25  
26 =head1 INTERFACE
27  
28 All Oryx components implement the interface defined herein. This
29 is the basis for all Oryx components to share a common interface.
30 All this really means is that when an object is created, retrieved,
31 deleted etc. then each meta-instance (L<Oryx::Attribute>, L<Oryx::Association> etc.)
32 associated with the class or instance can decide what it wants to do
33 during each call. So when we say:
34  
35 CMS::Page->create({ ... });
36  
37 then in the C<create()> method inherited from L<Oryx::Class> we
38 do something similar to:
39  
40 sub create {
41 my ($class, $params) = @_;
42
43 # do a few things with $params, exactly what would depend
44 # on whether we're using DBI or DBM back-end
45
46 $_->create($query, $params, ...) foreach $class->members;
47
48 # return a newly created instance
49 }
50  
51 Here the C<members> method (defined in L<Oryx::Class> returns
52 all meta-instances hanging off the class, and to each on is the
53 C<create> method delegated; hence the common interface.
54  
55 =over
56  
57 =item create
58  
59 meta object's C<create()> hook
60  
61 =item retrieve
62  
63 meta object's C<retrieve()> hook
64  
65 =item update
66  
67 meta object's C<update()> hook
68  
69 =item delete
70  
71 meta object's C<delete()> hook
72  
73 =item search
74  
75 meta object's C<search()> hook
76  
77 =item construct
78  
79 meta object's C<construct()> hook
80  
81 =head1 META-DATA ACCESS
82  
83 Each meta-type (with the exception of L<Oryx::Value> types) has
84 meta-data associated with it which is usually defined in the
85 C<$schema> class variable used in your persistent classes.
86  
87 The following are accessors for this meta-data:
88  
89 =item meta
90  
91 usually returns a hash reference which corresponds to the meta-data
92 described in C<$schema>.
93  
94 =item getMetaAttribute( $name )
95  
96 get a value from the meta-data hash ref keyed by C<$name>
97  
98 =item setMetaAttribute( $name, $value )
99  
100 set a value from the meta-data hash ref keyed by C<$name>
101  
102 =back
103  
104 =cut
105  
106 sub create { }
107 sub retrieve { }
108 sub update { }
109 sub delete { }
110 sub search { }
111 sub construct { }
112  
113 sub meta {
114 my $class = shift;
115 $class->{meta} = shift if @_;
116 $class->{meta};
117 }
118  
119 sub setMetaAttribute {
120 my ($class, $key, $value) = @_;
121 $class->meta->{$key} = $value;
122 }
123  
124 sub getMetaAttribute {
125 my ($class, $key) = @_;
126 unless ($class->meta) {
127 cluck("$class has no meta");
128 }
129 return $class->meta->{$key};
130 }
131  
132 sub _carp {
133 my $thing = ref($_[0]) ? ref($_[0]) : $_[0];
134 carp("[$thing] $_[1]");
135 }
136  
137 sub _croak {
138 my $thing = ref($_[0]) ? ref($_[0]) : $_[0];
139 croak("[$thing] $_[1]");
140 }
141  
142 1;
143  
144 =head1 SEE ALSO
145  
146 L<Oryx>, L<Oryx::Class>
147  
148 =head1 AUTHOR
149  
150 Copyright (C) 2005 Richard Hundt <richard NO SPAM AT protea-systems.com>
151  
152 =head1 LICENSE
153  
154 This library is free software and may be used under the same terms as Perl itself.
155  
156 =cut