Module: ConfigureMe::Loading

Included in:
Base
Defined in:
lib/configure_me/loading.rb

Instance Method Summary collapse

Instance Method Details

#from_hash(root, config) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/configure_me/loading.rb', line 5

def from_hash(root, config)

  # Determine how many root keys there are.
  #
  # If more than one, create a new nested ConfigureMe::Base
  # subclass for each key.
  #
  # If only one, just assign the values to the root config
  if config.keys.length > 1
    if root.nil?
      root = define_custom_class('RootConfig')
    end
    config.each_pair do |key, value|
      if value.is_a?(Hash)
        klass_name = "#{key}_config".camelize
        c = define_custom_class("#{key}_config".camelize)
        from_hash(c, value)
        c.send :nest_me, root
      else
        root.send :setting, key, :default => value
      end
    end
    root
  else
    root = define_custom_class(config.keys.first.to_s.camelize)
    from_hash(root, config.values.first)
  end
end

#load(*args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/configure_me/loading.rb', line 34

def load(*args)
  case args.first
  when Hash
    from_hash(nil, args.first)
  when String
    if File.exists?(args.first)
      yml = YAML::load(File.open(args.first))
      from_hash(nil, yml)
    else
      raise ::ArgumentError, "ConfigureMe: Invalid file: #{args.first}"
    end
  else
    raise ::ArgumentError, "ConfigureMe: Not sure how to load type [#{args.class}]"
  end
end