| 1 | package Oryx::Schema; | ||
| 2 | |||
| 3 | use base qw(Class::Data::Inheritable); | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Oryx::Schema - Schema class for Oryx | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | package CMS::Schema; | ||
| 12 | |||
| 13 | # enable auto deploy for all classes | ||
| 14 | use Oryx::Class(auto_deploy => 1); | ||
| 15 | |||
| 16 | # useful if you want to say $storage->deploySchema('CMS::Schema'); | ||
| 17 | use CMS::Page; | ||
| 18 | use CMS::Paragraph; | ||
| 19 | use CMS::Image; | ||
| 20 | use CMS::Author; | ||
| 21 | |||
| 22 | sub prefix { 'cms' } | ||
| 23 | |||
| 24 | 1; | ||
| 25 | |||
| 26 | #================================================================== | ||
| 27 | # ALTERNATIVE - With XML::DOM::Lite installed | ||
| 28 | #================================================================== | ||
| 29 | package CMS::Schema; | ||
| 30 | use base qw(Oryx::Schema); | ||
| 31 | 1; | ||
| 32 | __DATA__ | ||
| 33 | <Schema> | ||
| 34 | <Class name="CMS::Page"> | ||
| 35 | <Attribute name="title" type="String"/> | ||
| 36 | <Attribute name="num" type="Integer"/> | ||
| 37 | <Association role="author" class="CMS::Author"/> | ||
| 38 | </Class> | ||
| 39 | <Class name="CMS::Author"> | ||
| 40 | <Attribute name="first_name" type="String"/> | ||
| 41 | <Attribute name="last_name" type="String"/> | ||
| 42 | </Class> | ||
| 43 | </Schema> | ||
| 44 | use CMS::Schema; | ||
| 45 | |||
| 46 | my $cms_storage = Oryx->connect(\@conn, 'CMS::Schema'); | ||
| 47 | CMS::Schema->addClass('CMS::Revision'); | ||
| 48 | my @cms_classes = CMS::Schema->classes; | ||
| 49 | $cms_storage->deploySchema(); # deploys only classes seen by CMS::Schema | ||
| 50 | $cms_storage->deploySchema('CMS::Schema') # same thing, but `use's CMS::Schema first | ||
| 51 | my $name = CMS::Schema->name; # returns CMS_Schema | ||
| 52 | CMS::Schema->hasClass($classname); # true if seen $classname | ||
| 53 | |||
| 54 | |||
| 55 | =head1 DESCRIPTION | ||
| 56 | |||
| 57 | Schema class for Oryx. | ||
| 58 | |||
| 59 | The use of this class is optional. | ||
| 60 | |||
| 61 | The intention is to allow arbitrary grouping of classes | ||
| 62 | into different namespaces to support simultaneous use of | ||
| 63 | different storage backends, or for having logically separate | ||
| 64 | groups of classes in the same database, but having table | ||
| 65 | names prefixed to provide namespace separation. | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | __PACKAGE__->mk_classdata('_classes'); | ||
| 70 | __PACKAGE__->mk_classdata('_name'); | ||
| 71 | |||
| 72 | sub new { | ||
| 73 | my $class = shift; | ||
| 74 | $class->_classes({ }) unless defined $class->_classes; | ||
| 75 | return bless { }, $class; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub name { | ||
| 79 | my $self = shift; | ||
| 80 | if (@_) { | ||
| 81 | $_[0] =~ s/::/_/g; | ||
| 82 | $self->_name($_[0]); | ||
| 83 | } | ||
| 84 | unless ($self->_name) { | ||
| 85 | my $name = ref($self) || $self; | ||
| 86 | $name =~ s/::/_/g; | ||
| 87 | $self->_name($name); | ||
| 88 | } | ||
| 89 | return $self->_name; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub prefix { | ||
| 93 | my $self = shift; | ||
| 94 | if (@_) { | ||
| 95 | $self->{prefix} = shift; | ||
| 96 | } | ||
| 97 | unless (defined $self->{prefix}) { | ||
| 98 | $self->{prefix} = ''; | ||
| 99 | } | ||
| 100 | return $self->{prefix}; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub classes { | ||
| 104 | my @gens = grep { UNIVERSAL::isa($_, 'Oryx::Schema::Generator') } @INC; | ||
| 105 | foreach my $gen (@gens) { $gen->requireAll() } | ||
| 106 | keys %{$_[0]->_classes}; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub addClass { | ||
| 110 | my ($self, $class) = @_; | ||
| 111 | $self->_classes->{$class}++; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub hasClass { | ||
| 115 | return shift->class(@_); | ||
| 116 | } | ||
| 117 | |||
| 118 | sub class { | ||
| 119 | my $class = $_[0]->_classes->{$_[1]}; | ||
| 120 | return $class; | ||
| 121 | } | ||
| 122 | |||
| 123 | sub loadXML { | ||
| 124 | my $self = shift; | ||
| 125 | my $xstr = shift; | ||
| 126 | use XML::DOM::Lite::Parser; | ||
| 127 | use Oryx::Schema::Generator; | ||
| 128 | |||
| 129 | my $parser = XML::DOM::Lite::Parser->new( whitespace => 'strip' ); | ||
| 130 | my $doc = $parser->parse( $xstr ); | ||
| 131 | |||
| 132 | push @INC, Oryx::Schema::Generator->new( $doc ); | ||
| 133 | } | ||
| 134 | |||
| 135 | sub import { | ||
| 136 | my $class = shift; | ||
| 137 | my $fh = *{"$class\::DATA"}{IO}; | ||
| 138 | return undef unless $fh; | ||
| 139 | local $/ = undef; | ||
| 140 | my $data = <$fh>; | ||
| 141 | if ($data) { | ||
| 142 | $class->loadXML($data); | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | 1; | ||
| 147 | |||
| 148 | =head1 SEE ALSO | ||
| 149 | |||
| 150 | L<Oryx>, L<Oryx::Class> | ||
| 151 | |||
| 152 | =head1 AUTHOR | ||
| 153 | |||
| 154 | Copyright (C) 2005 Richard Hundt <richard NO SPAM AT protea-systems.com> | ||
| 155 | |||
| 156 | =head1 LICENSE | ||
| 157 | |||
| 158 | This library is free software and may be used under the same terms as Perl itself. | ||
| 159 | |||
| 160 | =cut |