Sha256: 865cedadd2b7bf40e5abb8bfbdf545c44bdfcde028f228769803a04f2deec9c3

Contents?: true

Size: 1.58 KB

Versions: 7

Compression:

Stored size: 1.58 KB

Contents

#encoding: UTF-8

require 'yaml'

module Solis
class ConfigFile
  @config = {}
  @config_file_path = ''
  @config_file_name = 'config.yml'

  def self.version
    "0.0.4"
  end

  def self.name
    @config_file_name
  end

  def self.name=(config_file_name)
    @config_file_name = config_file_name
  end

  def self.path
    @config_file_path
  end

  def self.path=(config_file_path)
    @config_file_path = File.absolute_path(config_file_path)
  end

  def self.[](key)
    init
    @config[key]
  end

  def self.[]=(key,value)
    init
    @config[key] = value
    File.open("#{path}/#{name}", 'w') do |f|
      f.puts @config.to_yaml
    end
  end

  def self.include?(key)
    init
    @config.include?(key)
  end

  def self.config
    init
    @config
  end

  private

  def self.init
    discover_config_file_path
    if @config.empty?
      config = YAML::load_file("#{path}/#{name}")
      @config = process(config)
    end
  end

  def self.discover_config_file_path
    @config_file_path = ENV['CONFIG_FILE_PATH'] || '' if path.nil? || path.empty?
    if @config_file_path.nil? || @config_file_path.empty?
      if File.exist?(@config_file_name)
        @config_file_path = '.'
      elsif File.exist?("config/#{@config_file_name}")
        @config_file_path = 'config'
      end
    end

    #puts "#{@config_file_name} found at #{@config_file_path}"
  end

  def self.process(config)
    new_config = {}
    config.each do |k,v|

      if config[k].is_a?(Hash)
        v = process(v)
      end
#      config.delete(k)      
      new_config.store(k.to_sym, v)
    end

    new_config
  end
end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
solis-0.70.0 lib/solis/config_file.rb
solis-0.69.0 lib/solis/config_file.rb
solis-0.68.0 lib/solis/config_file.rb
solis-0.67.0 lib/solis/config_file.rb
solis-0.66.0 lib/solis/config_file.rb
solis-0.65.0 lib/solis/config_file.rb
solis-0.64.0 lib/solis/config_file.rb