Skip to content

Instantly share code, notes, and snippets.

@joshlin
Created April 15, 2012 03:29
Show Gist options
  • Select an option

  • Save joshlin/2389737 to your computer and use it in GitHub Desktop.

Select an option

Save joshlin/2389737 to your computer and use it in GitHub Desktop.
require 'yaml'
require 'json'
# require 'active_support/core_ext'
#HashWithIndifferentAccess
# default config
class Configuration
attr_accessor :name, :host
def initialize(parameters = {})
parameters.each do |key,value|
send("#{key}=",value) if respond_to? "#{key}="
end
end
def merge(configurations = [])
Array(configurations).each do |config|
@name = config.name
@host = config.host
end
end
end
class JSONConfig
def self.parse(filename)
json_file_contents = File.read filename
json_config_data = JSON.parse json_file_contents
# convert the keys to normalized keys
normalized_hash = {}
normalized_hash[:name] = json_config_data['FullName']
Configuration.new json_config_data
end
end
configuration_root_dir = File.join(File.dirname(__FILE__),"..","fixtures")
jconfig = JSONConfig.parse "#{configuration_root_dir}/configuration.json"
yconfig
jconfig.merge yconfig
yml_file_contents = File.read "#{configuration_root_dir}/configuration.yml"
yml_config_data = YAML::load file_contents
# decide a default config. if default config is nil then apply others.
# conflict: the new one override the default config and warn
# users that conflict
class Configuration
def host
json_config_data['host'] || yml_config_data['host']
end
def title
json_config_data['title'] || yml_config_data['title']
end
def method_missing(name, args, &block)
define_method name do
#json_config_data[name] || yml_config_data[name]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment