class KonfigYaml
KonfigYaml
main class
Constants
- VERSION
Public Class Methods
create_hash(name, opts)
click to toggle source
# File lib/konfig_yaml/main.rb, line 60 def create_hash(name, opts) name ||= 'app' path = File.expand_path(opts[:path] || 'config', Dir.pwd) env = environment(opts) expand_erb = opts.fetch(:erb, false) h = load_config(name, env, path, expand_erb) dup_hash_expand_envs(h) end
new(name = 'app', opts = nil)
click to toggle source
Create an configuration from YAML file
@param [String] name ('app') the basename of YAML file @param [Hash] opts the options to intialize @option opts [String] :env (ENV || ENV || ENV || 'development') execution environment @option opts [Boolean] :erb (false) whether evaluate ERB or not @option opts [String] :path ('config') directory path that contains the YAML file
# File lib/konfig_yaml/main.rb, line 16 def initialize(name = 'app', opts = nil) if name.is_a?(Hash) && opts.nil? opts = name name = 'app' elsif opts.nil? opts = {} end hash = self.class.create_hash(name, opts) set_inner_hash(hash) end
setup() { |cfg| ... }
click to toggle source
Setup global configuration class from YAML file If this is called without block, this defines `Settings` class by loading `config/app.yml` @yield [config] configuration to the block @yieldparam config [OpenStruct] support `const_name`, `name`, `env`, `path` and `use_cahe` attribute
# File lib/konfig_yaml/main.rb, line 33 def setup(&block) const_name = 'Settings' name = nil opts = {} if block_given? cfg = OpenStruct.new yield(cfg) cfg.each_pair do |key, val| if key == :const_name const_name = val elsif key == :name name = val else opts[key] = val end end end hash = create_hash(name, opts) cls = Class.new do extend NeoHash::Support set_inner_hash(hash) end Object.const_set(const_name, cls) end
Private Class Methods
convert_data_to_hash(data, env)
click to toggle source
# File lib/konfig_yaml/main.rb, line 91 def convert_data_to_hash(data, env) if data.include?(env) deep_merge(data[env] || {}, data['default'] || {}) elsif data.include?('default') data['default'] else raise ArgumentError.new("The configuration for #{env} is not defined in #{name}") end end
deep_merge(target, default)
click to toggle source
# File lib/konfig_yaml/main.rb, line 101 def deep_merge(target, default) target.merge(default) do |key, target_val, default_val| if target_val.is_a?(Hash) && default_val.is_a?(Hash) deep_merge(target_val, default_val) else target_val end end end
dup_array_expand_envs(root)
click to toggle source
# File lib/konfig_yaml/main.rb, line 125 def dup_array_expand_envs(root) root.map do |val| if val.is_a?(Hash) dup_hash_expand_envs(val) elsif val.is_a?(Array) dup_array_expand_envs(val) elsif val.is_a?(String) expand_envs(val) else val end end end
dup_hash_expand_envs(root)
click to toggle source
# File lib/konfig_yaml/main.rb, line 111 def dup_hash_expand_envs(root) root.map do |name, val| if val.is_a?(Hash) [name, dup_hash_expand_envs(val)] elsif val.is_a?(Array) [name, dup_array_expand_envs(val)] elsif val.is_a?(String) [name, expand_envs(val)] else [name, val] end end.to_h end
environment(opts)
click to toggle source
# File lib/konfig_yaml/main.rb, line 72 def environment(opts) env = opts[:env] return env.to_s if env ENV['RUBY_ENV'] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development' end
expand_envs(str)
click to toggle source
# File lib/konfig_yaml/main.rb, line 139 def expand_envs(str) str.gsub(/\$\{(.+?)\}/) do |m| env_key, default = $1.split(/:\-?/) ENV[env_key] || default || '' end end
load_config(name, env, path, expand_erb)
click to toggle source
# File lib/konfig_yaml/main.rb, line 78 def load_config(name, env, path, expand_erb) data = load_yaml(name, path, expand_erb) convert_data_to_hash(data, env) end
load_yaml(name, dir, expand_erb)
click to toggle source
# File lib/konfig_yaml/main.rb, line 83 def load_yaml(name, dir, expand_erb) file_path = Dir.glob("#{dir}/#{name}.{yml,yaml}").first raise ArgumentError.new("Not found configuration yaml file") unless file_path str = File.read(file_path) str = ERB.new(str).result if expand_erb YAML.load(str) end