Sha256: d37154d4299126d9bb17d182f603bef40ecec212d89d820f28f8228d4ea8f19f

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

module ActiveTenant
  class SQLiteAdapter
    delegate :connection_config, :establish_connection, :connection, to: ::ActiveRecord::Base

    def initialize
      self.global = ActiveTenant.configuration.global if ActiveTenant.configuration.global
    end

    def all
      path = database_path
      Dir.glob("#{path}/*.sqlite3").map { |f| File.basename(f, '.sqlite3') } - [global]
    end

    def create(name)
      unless all.include? name
        current_config = connection_config
        establish_connection current_config.merge(database: file_name(name))
        connection
        establish_connection current_config
      end
    end

    def remove(name)
      file = file_name name
      FileUtils.rm(file) if File.exist?(file)
    end

    def with(name)
      return yield if name == self.name

      current_config = connection_config
      ex = nil
      begin
        establish_connection current_config.merge(database: file_name(name))
        result = yield
      rescue => e
        ex = e
      ensure
        establish_connection current_config
        raise ex unless ex.nil?
        result
      end
    end

    def name
      File.basename(connection_config[:database], '.sqlite3')
    end

    def global
      @global
    end

    private

    def global=(name)
      @global = name
    end

    def database_path
      File.dirname(connection_config[:database])
    end

    def file_name(name)
      "#{database_path}/#{name}.sqlite3"
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_tenant-0.0.1 lib/active_tenant/adapters/sqlite_adapter.rb