Sha256: c12735fc6ea712b8a907d5d5e56b4f36a86efeb13add3a29f0633d8d1b8ef372

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

warn_off { require 'sequel' }

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

2 entries across 2 versions & 1 rubygems

Version Path
kamerling-0.0.2 lib/kamerling/repos.rb
kamerling-0.0.1 lib/kamerling/repos.rb