Sha256: 89905a472ab22dfc88575a8d716cb67d54fafccedf13c4c7a1c953d752a434cc

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

require 'active_support/core_ext/module/attribute_accessors'

module KcConfig
  # ensures the setup only gets run once
  @@_ran_once = false

  mattr_accessor :const_name
  @@const_name = "KcSettings"

  class << self
    
    def setup
      yield self if @@_ran_once == false
      @@_ran_once = true
    end
  
    def load!(settings = {})
      Kernel.send(:remove_const, KcConfig.const_name) if Kernel.const_defined?(KcConfig.const_name)
      kc_wrapper = KcWrapper.new
      kc_wrapper.load!(settings)
      Kernel.const_set(KcConfig.const_name, kc_wrapper)
    end
  end
end

class KcWrapper

  def initialize
    @settings = {}
  end

  def load!(settings = {})
    settings.each do |key, value|
      set(key, value)
    end
  end

  def set(key, value)
    unset(key) if set?(key)
    define_accessor(key) { value }
    @settings[key] = value
  end

  def set?(key)
    @settings.key?(key)
  end
  
  def get(key)
    @settings[key]
  end

  private

  def define_accessor(name, &block)
    singleton_class.class_eval { define_method(name, &block) } if !respond_to?(name) || exists?(name)
  end

  def singleton_class
    class << self
      self
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
korben-0.0.1 lib/korben/kc_config.rb