| 1 | package XAS::Model::Database; | ||
| 2 | |||
| 3 | our $VERSION = '0.01'; | ||
| 4 | |||
| 5 | 1 | ▶ | use Class::Inspector; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 0 | ▶ | use XAS::Model::Schema; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use Params::Validate qw/SCALAR ARRAYREF/; | ||
| 8 | |||
| 9 | use XAS::Class | ||
| 10 | debug => 0, | ||
| 11 | version => $VERSION, | ||
| 12 | base => 'XAS::Base', | ||
| 13 | constants => 'DELIMITER PKG REFS ARRAY', | ||
| 14 | filesystem => 'File', | ||
| 15 | utils => ':validation', | ||
| 16 | exports => { | ||
| 17 | hooks => { | ||
| 18 | schema => [ \&_schema, 1 ], | ||
| 19 | table => [ \&_tables, 1 ], | ||
| 20 | tables => [ \&_tables, 1 ], | ||
| 21 | } | ||
| 22 | }, | ||
| 23 | ; | ||
| 24 | |||
| 25 | our $KEYS; | ||
| 26 | |||
| 27 | #use Data::Dumper; | ||
| 28 | |||
| 29 | # --------------------------------------------------------------------- | ||
| 30 | # Hooks | ||
| 31 | # --------------------------------------------------------------------- | ||
| 32 | |||
| 33 | sub _tables { | ||
| 34 | my $self = shift; | ||
| 35 | my $target = shift; | ||
| 36 | my $symbol = shift; | ||
| 37 | my $tables = shift; | ||
| 38 | |||
| 39 | $self->tables($tables, 4); | ||
| 40 | |||
| 41 | return $self; | ||
| 42 | |||
| 43 | } | ||
| 44 | |||
| 45 | sub _schema { | ||
| 46 | my $self = shift; | ||
| 47 | my $target = shift; | ||
| 48 | my $symbol = shift; | ||
| 49 | my $schemas = shift; | ||
| 50 | |||
| 51 | $self->schemas($schemas); | ||
| 52 | |||
| 53 | return $self; | ||
| 54 | |||
| 55 | } | ||
| 56 | |||
| 57 | # --------------------------------------------------------------------- | ||
| 58 | # Public Methods | ||
| 59 | # --------------------------------------------------------------------- | ||
| 60 | |||
| 61 | sub table { | ||
| 62 | my $self = shift; | ||
| 63 | |||
| 64 | $self->tables(@_); | ||
| 65 | |||
| 66 | } | ||
| 67 | |||
| 68 | sub tables { | ||
| 69 | my $self = shift; | ||
| 70 | my ($tables, $depth) = validate_params(\@_, [ | ||
| 71 | { type => SCALAR | ARRAYREF }, | ||
| 72 | { optional => 1, default => 3 }, | ||
| 73 | ]); | ||
| 74 | |||
| 75 | $tables = [ split(DELIMITER, $tables) ] unless (ref($tables) eq ARRAY); | ||
| 76 | |||
| 77 | my ($pkg) = caller($depth); # presummed caller | ||
| 78 | |||
| 79 | no strict REFS; # to register new methods in package | ||
| 80 | no warnings; # turn off warnings | ||
| 81 | |||
| 82 | foreach my $table (@$tables) { | ||
| 83 | |||
| 84 | # building constants in the calling package. | ||
| 85 | |||
| 86 | if ($table ne ':all') { | ||
| 87 | |||
| 88 | *{$pkg.PKG.$table} = sub { $KEYS->{$table}; }; | ||
| 89 | |||
| 90 | } else { | ||
| 91 | |||
| 92 | while (my ($key, $value) = each(%$KEYS)) { | ||
| 93 | |||
| 94 | *{$pkg.PKG.$key} = sub { $value; }; | ||
| 95 | |||
| 96 | } | ||
| 97 | |||
| 98 | last; | ||
| 99 | |||
| 100 | } | ||
| 101 | |||
| 102 | } | ||
| 103 | |||
| 104 | } | ||
| 105 | |||
| 106 | sub schemas { | ||
| 107 | my $self = shift; | ||
| 108 | my ($schemas) = validate_params(\@_, [ | ||
| 109 | { type => SCALAR | ARRAYREF }, | ||
| 110 | ]); | ||
| 111 | |||
| 112 | $schemas = [ split(DELIMITER, $schemas) ] unless (ref($schemas) eq ARRAY); | ||
| 113 | |||
| 114 | foreach my $schema (@$schemas) { | ||
| 115 | |||
| 116 | # loading our schema | ||
| 117 | |||
| 118 | XAS::Model::Schema->load_namespaces( | ||
| 119 | result_namespace => "+$schema" . "::Result", | ||
| 120 | resultset_namespace => "+$schema" . "::ResultSet", | ||
| 121 | ); | ||
| 122 | |||
| 123 | # building our keys | ||
| 124 | |||
| 125 | my $pattern = $schema . '::'; | ||
| 126 | my $modules = Class::Inspector->subclasses('UNIVERSAL'); | ||
| 127 | |||
| 128 | foreach my $module (@$modules) { | ||
| 129 | |||
| 130 | next if ($module =~ /ResultSet/); | ||
| 131 | |||
| 132 | if ($module =~ m/$pattern/) { | ||
| 133 | |||
| 134 | my @parts = split('::', $module); | ||
| 135 | my $begin = scalar(@parts) - 1; | ||
| 136 | my $name = join('', splice(@parts, $begin, $#parts)); | ||
| 137 | |||
| 138 | $KEYS->{$name} = $module; | ||
| 139 | |||
| 140 | } | ||
| 141 | |||
| 142 | } | ||
| 143 | |||
| 144 | } | ||
| 145 | |||
| 146 | } | ||
| 147 | |||
| 148 | # --------------------------------------------------------------------- | ||
| 149 | # Private Methods | ||
| 150 | # --------------------------------------------------------------------- | ||
| 151 | |||
| 152 | 1; | ||
| 153 | |||
| 154 | __END__ | ||