Class: ConfigFileLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/config_file_loader.rb,
lib/config_file_loader/version.rb

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.baseObject

base directory for configuration typically in rails root / config



10
11
12
13
14
15
16
17
18
# File 'lib/config_file_loader.rb', line 10

def self.base
  if defined?(@@base)
    @@base
  elsif defined?(Rails)
    @@base||="#{Rails.root}/config/"
  else
    @@base||=File.expand_path(File.dirname(__FILE__)) + "/config/"
  end
end

.base=(val) ⇒ Object



20
21
22
# File 'lib/config_file_loader.rb', line 20

def self.base=(val)
  @@base=val
end

.contents(file_name) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/config_file_loader.rb', line 72

def self.contents(file_name)
  if file_name.nil?
    nil
  elsif (file_name.is_a?(Hash))
    file_name
  else
    file_name=fix_name(file_name)
    load_erb_yaml(file_name) if File.exist?(file_name)
  end
end

.deep_merge(target, extra) ⇒ Object

snippets.dzone.com/posts/show/4706 www.francisfish.com/deep_merge_a_ruby_hash_the_joys_of_recursion.htm rails already defines this return target.deep_merge(extra) if target.respond_to?(:deep_merge)



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/config_file_loader.rb', line 98

def self.deep_merge(target, extra)
  # return something if either is nil
  return target || extra if target.nil? || extra.nil?

  extra.each_key do |k1|
    # if the key exists in both sides and they are both "hashes", merge
    # could move this block to the first line, but may want special logic for lists
    if target.key?(k1) && target[k1].respond_to?(:each_key) && extra[k1].respond_to?(:each_key)
      target[k1] = deep_merge(target[k1],extra[k1])
    else
      target[k1] = extra[k1] #dup?
    end
  end
  target    
end

.deep_symbolize_keys(hash) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/config_file_loader.rb', line 114

def self.deep_symbolize_keys(hash)
  return hash unless hash.is_a?(Hash)

  #hash.symbolize_keys
  hash.inject({}) do |options, (key, value)|
    value=deep_symbolize_keys(value) if value.is_a?(Hash)
    options[(key.to_sym rescue key) || key] = value
    options
  end
  #ret inject value
end

.envObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/config_file_loader.rb', line 25

def self.env
  @@env ||=
    if defined?(Rails)
      Rails.env
    elsif defined?(RAILS_ENV)
      RAILS_ENV
    else
      ENV['RACK_ENV'] || ENV['RAILS_ENV'] || ENV['ENVIRONMENT']
    end
end

.env=(val) ⇒ Object



36
37
38
# File 'lib/config_file_loader.rb', line 36

def self.env=(val)
  @@env=val
end

.fix_name(name) ⇒ Object

add .yml or rails config root if necessary



64
65
66
67
68
69
70
# File 'lib/config_file_loader.rb', line 64

def self.fix_name(name)
  return unless name.is_a?(String)
  #if it is not relative ('./file') or absolute ('/a/b/file') - tack on config directory
  name = "#{self.base}/#{name}" unless ['.','/','~'].include?(name[0,1])
  name+=".yml" unless name.include?('yml') || name.include?('cfg')
  name
end

.load(*files, &block) ⇒ Object



48
49
50
51
# File 'lib/config_file_loader.rb', line 48

def self.load(*files, &block)    
  block=lambda() { |hash| self.deep_symbolize_keys(hash) } unless block_given?
  load_files(*files,&block)
end

.load_as_struct(*files) ⇒ Object



44
45
46
# File 'lib/config_file_loader.rb', line 44

def self.load_as_struct(*files)
  OpenStruct.new(load_files(*files))
end

.load_erb_yaml(file_name) ⇒ Object



83
84
85
86
87
88
# File 'lib/config_file_loader.rb', line 83

def self.load_erb_yaml(file_name)
  raw_config = ERB.new(File.read(file_name)).result
  yaml_config = YAML.load(raw_config)
  yaml_config = yaml_config[env] || yaml_config[env.to_s] if env
  yaml_config
end

.load_files(*files, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/config_file_loader.rb', line 53

def self.load_files(*files, &block)
  raw_hash = nil
  files.each do |file_name|
    second_hash = contents(file_name)
    second_hash = yield second_hash if block_given?
    raw_hash=deep_merge(raw_hash,second_hash)
  end
  raw_hash
end

.load_no_symbolize_keys(*files) ⇒ Object



40
41
42
# File 'lib/config_file_loader.rb', line 40

def self.load_no_symbolize_keys(*files)
  load_files(*files)
end