Sha256: 8aa806160c1a1c6d797f07c8220fbde0e07487bc354bdb196eee5717d05a783d

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

warn_off { require 'sequel' }
require_relative 'client'
require_relative 'project'
require_relative 'registration'
require_relative 'repo'
require_relative 'task'

Sequel.extension :migration

module Kamerling
  class Repos
    class << self
      attr_writer :repos

      def <<(object)
        repos[object.class] << object
        self
      end

      def [](klass)
        repos[klass]
      end

      def clients
        repos[Client].all
      end

      def clients_for(project)
        repos[Registration].related_to(project).map(&:client)
      end

      def db=(db)
        warn_off { Sequel::Migrator.run db, "#{__dir__}/migrations" }
        @repos = nil
        @db    = db
      end

      def free_clients_for(project)
        clients_for(project).reject(&:busy)
      end

      def next_task_for(project)
        repos[Task].related_to(project).reject(&:done).first
      end

      def project(project_uuid)
        repos[Project][project_uuid]
      end

      def projects
        repos[Project].all
      end

      def tasks_for(project)
        repos[Task].related_to(project)
      end

      private

      def db
        @db ||= self.db = Sequel.sqlite
      end

      def repos
        @repos ||= Hash.new do |repos, klass|
          table = "#{klass.name.split('::').last.downcase}s".to_sym
          repos[klass] = Repo.new(klass, warn_off { db[table] })
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kamerling-0.0.3 lib/kamerling/repos.rb