« blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Validator/HashRef.pm
↑ Summary
blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Error.pm »
| 1 | package Dwarf::Module::DSL; | ||
| 2 | use Dwarf::Pragma; | ||
| 3 | use Dwarf::Data::Validator; | ||
| 4 | use Dwarf::SQLBuilder; | ||
| 5 | use Dwarf::Util qw/load_class dwarf_log/; | ||
| 6 | use Carp qw/croak/; | ||
| 7 | use Scalar::Util qw/weaken/; | ||
| 8 | |||
| 9 | use Dwarf::Accessor { | ||
| 10 | ro => [qw/context module/], | ||
| 11 | rw => [qw/prefix/] | ||
| 12 | }; | ||
| 13 | |||
| 14 | our @FUNC = qw/ | ||
| 15 | self app c model m | ||
| 16 | conf db error e env log debug | ||
| 17 | session param parameters | ||
| 18 | request req method | ||
| 19 | response res status type header headers body | ||
| 20 | not_found unauthorized finish redirect | ||
| 21 | is_cli is_production | ||
| 22 | load_plugin load_plugins | ||
| 23 | render dump new_query args | ||
| 24 | /; | ||
| 25 | |||
| 26 | sub new { | ||
| 27 | my $class = shift; | ||
| 28 | my $self = bless { @_ }, $class; | ||
| 29 | dwarf_log 'new DSL'; | ||
| 30 | return $self; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub DESTROY { | ||
| 34 | my $self = shift; | ||
| 35 | dwarf_log 'DESTROY DSL'; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub init {} | ||
| 39 | |||
| 40 | sub _build_prefix { | ||
| 41 | my $self = shift; | ||
| 42 | $self->{prefix} ||= $self->c->namespace . '::Model'; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub app { shift->context } | ||
| 46 | sub c { shift->context } | ||
| 47 | sub m { shift->model(@_) } | ||
| 48 | sub conf { shift->c->conf(@_) } | ||
| 49 | sub db { shift->c->db(@_) } | ||
| 50 | sub error { shift->c->error(@_) } | ||
| 51 | sub e { shift->c->error(@_) } | ||
| 52 | sub env { shift->c->env } | ||
| 53 | sub log { shift->c->log(@_) } | ||
| 54 | sub debug { shift->c->debug(@_) } | ||
| 55 | |||
| 56 | sub session { shift->c->session(@_) } | ||
| 57 | sub param { shift->c->request->param(@_) } | ||
| 58 | sub parameters { shift->c->request->parameters(@_) } | ||
| 59 | sub request { shift->c->request(@_) } | ||
| 60 | sub req { shift->c->request(@_) } | ||
| 61 | sub method { shift->c->method(@_) } | ||
| 62 | sub response { shift->c->response(@_) } | ||
| 63 | sub res { shift->c->response(@_) } | ||
| 64 | sub status { shift->c->status(@_) } | ||
| 65 | sub type { shift->c->type(@_) } | ||
| 66 | sub header { shift->c->header(@_) } | ||
| 67 | sub headers { shift->c->headers(@_) } | ||
| 68 | sub body { shift->c->body(@_) } | ||
| 69 | |||
| 70 | sub not_found { shift->c->not_found(@_) } | ||
| 71 | sub unauthorized { shift->c->unauthorized(@_) } | ||
| 72 | sub finish { shift->c->finish(@_) } | ||
| 73 | sub redirect { shift->c->redirect(@_) } | ||
| 74 | sub is_cli { shift->c->is_cli(@_) } | ||
| 75 | sub is_production { shift->c->is_production(@_) } | ||
| 76 | sub load_plugin { shift->c->load_plugin(@_) } | ||
| 77 | sub load_plugins { shift->c->load_plugins(@_) } | ||
| 78 | sub render { shift->c->render(@_) } | ||
| 79 | sub dump { shift->c->dump(@_) } | ||
| 80 | |||
| 81 | sub new_query { Dwarf::SQLBuilder->new_query } | ||
| 82 | |||
| 83 | sub args { | ||
| 84 | my ($self, $rules, $module, $args) = @_; | ||
| 85 | croak 'rules must be HashRef' unless ref $rules eq 'HASH'; | ||
| 86 | my @ret = Dwarf::Data::Validator->validate($rules, $args); | ||
| 87 | return wantarray ? ($self, $ret[0]) : $ret[0]; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub model { | ||
| 91 | my $self = shift; | ||
| 92 | my $package = shift; | ||
| 93 | |||
| 94 | my $prefix = $self->prefix; | ||
| 95 | unless ($package =~ m/^$prefix/) { | ||
| 96 | $package = $prefix . '::' . $package; | ||
| 97 | } | ||
| 98 | |||
| 99 | $self->context->models->{$package} //= $self->create_model($package, @_); | ||
| 100 | } | ||
| 101 | |||
| 102 | sub create_model { | ||
| 103 | my $self = shift; | ||
| 104 | my $package = shift; | ||
| 105 | |||
| 106 | croak "package name must be specified to create model." | ||
| 107 | unless defined $package; | ||
| 108 | |||
| 109 | my $prefix = $self->prefix; | ||
| 110 | unless ($package =~ m/^$prefix/) { | ||
| 111 | $package = $prefix . '::' . $package; | ||
| 112 | } | ||
| 113 | |||
| 114 | dwarf_log "create model: $package"; | ||
| 115 | |||
| 116 | load_class($package); | ||
| 117 | my $model = $package->new(context => $self->context, @_); | ||
| 118 | weaken $model->{context}; | ||
| 119 | $model->init($self->context); | ||
| 120 | return $model; | ||
| 121 | } | ||
| 122 | |||
| 123 | sub export_symbols { | ||
| 124 | my ($self, $to) = @_; | ||
| 125 | |||
| 126 | no strict 'refs'; | ||
| 127 | no warnings 'redefine'; | ||
| 128 | my $super = *{"${to}::ISA"}{ARRAY}; | ||
| 129 | if ($super && $super->[0]) { | ||
| 130 | $self->export_symbols($super->[0]); | ||
| 131 | } | ||
| 132 | |||
| 133 | for my $f (@FUNC) { | ||
| 134 | *{"${to}::${f}"} = sub { | ||
| 135 | # OO インターフェース で呼ばれた時対策 | ||
| 136 | shift if defined $_[0] and $_[0] eq $self->module; | ||
| 137 | return $self->module if $f eq 'self'; | ||
| 138 | $self->$f(@_) | ||
| 139 | }; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | sub delete_symbols { | ||
| 144 | my ($self, $from) = @_; | ||
| 145 | |||
| 146 | no strict 'refs'; | ||
| 147 | no warnings 'redefine'; | ||
| 148 | my $super = *{"${from}::ISA"}{ARRAY}; | ||
| 149 | if ($super && $super->[0]) { | ||
| 150 | $self->delete_symbols($super->[0]); | ||
| 151 | } | ||
| 152 | |||
| 153 | for my $f (@FUNC) { | ||
| 154 | *{"${from}::${f}"} = sub {}; | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | 1; |