Sha256: a4b43a1c58baab8d7bf75a81ab0c4a41b66e112c26c85f7c0d494ac2f2fe5ea4

Contents?: true

Size: 1.74 KB

Versions: 23

Compression:

Stored size: 1.74 KB

Contents

# typed: false
require 'yaml'

module Kuby
  module Plugins
    module RailsApp
      class UnsupportedDatabaseError < StandardError; end

      class Database
        ADAPTER_MAP = {
          sqlite3: Sqlite,
          mysql2: MySQL,
          postgresql: Postgres
        }.freeze

        def self.get(rails_app)
          if rails_app.manage_database?
            new(rails_app)
          end
        end

        def self.get_adapter(adapter_name)
          ADAPTER_MAP.fetch(adapter_name) do
            raise UnsupportedDatabaseError, "Kuby does not support the '#{adapter}' "\
              'database adapter'
          end
        end

        attr_reader :rails_app

        def initialize(rails_app)
          @rails_app = rails_app
        end

        def plugin
          @plugin ||= self.class
            .get_adapter(adapter_name)
            .new(rails_app.environment, db_configs)
        end

        def adapter_name
          @adapter_name ||= db_config['adapter'].to_sym
        end

        alias_method :plugin_name, :adapter_name

        private

        def db_config
          @db_config ||= db_configs[rails_app.environment.name]
        end

        def db_configs
          @db_configs ||= YAML.load(File.read(db_config_path))
        end

        def db_config_path
          @db_config_path ||= begin
            db_config_paths.first or
              raise "Couldn't find database config in #{db_config_pattern}"
          end
        end

        def db_config_paths
          @db_config_paths ||= Dir.glob(db_config_pattern)
        end

        def db_config_pattern
          @db_config_pattern ||= File.join(
            rails_app.root, 'config', 'database.{yml,erb,yml.erb,yaml,yaml.erb}'
          )
        end
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
kuby-core-0.17.0 lib/kuby/plugins/rails_app/database.rb
kuby-core-0.16.1 lib/kuby/plugins/rails_app/database.rb
kuby-core-0.16.0 lib/kuby/plugins/rails_app/database.rb
kuby-core-0.15.0 lib/kuby/plugins/rails_app/database.rb
kuby-core-0.14.0 lib/kuby/plugins/rails_app/database.rb
kuby-core-0.13.0 lib/kuby/plugins/rails_app/database.rb
kuby-core-0.12.0 lib/kuby/plugins/rails_app/database.rb
kuby-core-0.11.16 lib/kuby/plugins/rails_app/database.rb
kuby-core-0.11.15 lib/kuby/plugins/rails_app/database.rb
kuby-core-0.11.14 lib/kuby/plugins/rails_app/database.rb
kuby-core-0.11.13 lib/kuby/plugins/rails_app/database.rb
kuby-core-0.11.12 lib/kuby/plugins/rails_app/database.rb
kuby-core-0.11.11 lib/kuby/plugins/rails_app/database.rb
kuby-core-0.11.10 lib/kuby/plugins/rails_app/database.rb
kuby-core-0.11.9 lib/kuby/plugins/rails_app/database.rb
kuby-core-0.11.8 lib/kuby/plugins/rails_app/database.rb
kuby-core-0.11.7 lib/kuby/plugins/rails_app/database.rb
kuby-core-0.11.6 lib/kuby/plugins/rails_app/database.rb
kuby-core-0.11.4 lib/kuby/plugins/rails_app/database.rb
kuby-core-0.11.3 lib/kuby/plugins/rails_app/database.rb