Sha256: 53bb698ad54d6a64c8bd8807df79c1dc00bfaabdfe84c990d5a11094fd111bb9

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

require 'rails'
require 'yaml'
require 'erb'
require "couchbase_settings/version"

module CouchbaseSettings
  class UndefinedCouchbaseSettings < StandardError; end
  class << self
    def setup!
      find_ymls.each do |yml|
        create_cbsettings_class(yml)
      end
    end
  
    def find_ymls
      main_file = "#{Rails.root.to_s}/config/couchbase.yml"
      couchbase_main_file = File.exists?(main_file) ? [main_file] : []
    end
  
    def create_cbsettings_class(yml_file)
      hash = load_yml(yml_file)
      #klass_name = File.basename(yml_file).gsub(".yml","").camelize
      klass_name = "CouchbaseSetting"
      #klass_name = "#{klass_name}CouchbaseSetting" unless klass_name=="CouchbaseSetting"
      klass = Object.const_set(klass_name,Class.new)
      hash.each do |key,value|
        #Rails.logger.debug "hash: key = #{key}, value = #{value}"
        klass.define_singleton_method(key){ value }
      end
      klass.class_eval do
        def self.method_missing(method_id,*args)
          raise UndefinedCouchbaseSettings, "#{method_id} is not defined in #{self.to_s}"
        end
      end
    end
  
    def load_yml(yml_file)
      erb = ERB.new(File.read(yml_file)).result
      erb.present? ? YAML.load(erb).to_hash[Rails.env] : {}
    end
  end # class << self
end

# Include Railtie after defining the module
COUCHBASE_SETTINGS_PATH = "#{File.dirname(__FILE__)}/couchbase_settings"
require "#{COUCHBASE_SETTINGS_PATH}/railtie.rb"

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
couchbase-settings-0.1.1 lib/couchbase_settings.rb