lib/CrashTest/Model/Storage.pm
author Vincent Tondellier <tonton+hg@team1664.org>
Wed, 04 Nov 2015 17:43:00 +0100
changeset 78 0ebef32c34af
child 83 eaf6d0ea4e65
permissions -rw-r--r--
Refactor everything - change db access method, use Mojo::{Pg,SQLite,...} instead of DBIx::Class - use Minion as job queue - refactor into a non-Lite Mojolicious app

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

package CrashTest::Model::Storage;
use Mojo::Base -base;

has [ qw/app config/ ];
has instances => sub { return [] };

sub register {
    my ($self, $storage_instance) = @_;

    push @{$self->instances}, $storage_instance;

    #$self->app->log->debug("Loaded $storage_instance");
}

sub load_plugins {
    my $self = shift;

    for my $storage(@{$self->config->{Storage}}) {
        $self->app->plugin("CrashTest::Plugin::Storage::" . $storage->{Type}, $storage);
    }
}


sub each {
    my ($self, $proc, @args) = @_;

    my $result = 1;
    foreach my $storage(@{$self->instances}) {
        if(defined(my $model = $storage->models->{CrashReport})) {
            if($model->can($proc)) {
                $result = $result && $model->$proc(@args);
            }
        }
    }
    return $result;
}

sub first {
    my ($self, $proc, @args) = @_;

    my @result;
    foreach my $storage(@{$self->instances}) {

        if(defined(my $model = $storage->models->{CrashReport})) {
            if($model->can($proc)) {
                @result = $model->$proc(@args);
                if(@result) {
                    last;
                }
            }
        }
    }

    return wantarray ? @result : shift @result;
}

1;