Sha256: 37933d3b367efbbb8ce542c20a0a92480197ca8a2f9041659c0b95d77a281f58

Contents?: true

Size: 1.19 KB

Versions: 7

Compression:

Stored size: 1.19 KB

Contents

require 'erb'
require 'mkit/config/config'
module MKIt
  module Utils
    module_function

    def me
      'mkit'
    end

    def log
      Console.logger
    end

    def root
      File.expand_path("../../..", __FILE__)
    end

    def set_config_dir(config_dir)
      @config_dir = config_dir
    end

    def config_dir
      @config_dir.nil? ? "#{self.root}/config" : @config_dir
    end

    def load_db_config(db_config_dir = self.config_dir)
      self.log.info "loading database configurations from '#{config_dir}'..."
      YAML::load(ERB.new(IO.read("#{db_config_dir}/database.yml")).result)
    end

    def db_config_to_uri(env = MKIt::Config.mkit.database.env)
      config = self.load_db_config[env]

      if config["username"] || config["password"]
        user_info = [ config["username"], config["password"] ].join(":")
      else
        user_info = nil
      end
      URI::Generic.new(config["adapter"],user_info,
                       config["hostname"] || "localhost",
                       config["port"],
                       nil,
                       "/#{config["database"]}",
                       nil,
                       nil,
                       nil).to_s
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
mkit-0.5.0 lib/mkit/utils.rb
mkit-0.4.3 lib/mkit/utils.rb
mkit-0.4.2 lib/mkit/utils.rb
mkit-0.4.1 lib/mkit/utils.rb
mkit-0.4.0 lib/mkit/utils.rb
mkit-0.3.0 lib/mkit/utils.rb
mkit-0.2.0 lib/mkit/utils.rb