Sha256: fcb12d2a8d515b51c3999a6137007cce23d2c8accf593a1fdd31e8ae66b6d786

Contents?: true

Size: 1.25 KB

Versions: 5

Compression:

Stored size: 1.25 KB

Contents

require 'ostruct'
require 'yaml'
require 'user_config'

module SearchKit
  module Configuration

    def configure
      yield(config) if block_given?
    end

    def config
      return @config if @config
      root   = UserConfig.new(".search-kit")
      yaml   = root['config.yml']
      config = OpenStruct.new

      yaml.each { |key, value| config.send("#{key}=", value) }
      @config = config
    end

    def set_config(key, value)
      root = UserConfig.new(".search-kit")
      yaml = root['config.yml']

      yaml[key] = value
      yaml.save
    end

    def show_config(key)
      root = UserConfig.new(".search-kit")
      root['config.yml'][key]
    end

    def fetch(key)
      ENV.fetch(key, show_config(key.downcase) || default(key.to_sym))
    end

    private

    def default(key)
      default_table = {
        APP_URI:     "http://localhost:8080",
        APP_ENV:     "development",
        APP_DIR:     default_app_dir,
        APP_VERBOSE: true,
        LOG_DIR:     default_log_dir
      }.fetch(key, nil)
    end

    def default_app_dir
      File.expand_path("../../", __dir__)
    end

    def default_log_dir
      log_dir = File.join(default_app_dir, 'log')
      Dir.mkdir(log_dir) unless Dir.exist?(log_dir)

      log_dir
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
search-kit-0.0.8 lib/search_kit/configuration.rb
search-kit-0.0.7 lib/search_kit/configuration.rb
search-kit-0.0.6 lib/search_kit/configuration.rb
search-kit-0.0.5 lib/search_kit/configuration.rb
search-kit-0.0.4 lib/search_kit/configuration.rb