Class: ConfigModule::ConfigHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/config_module/config_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#config_fileObject

Returns the value of attribute config_file.


6
7
8
# File 'lib/config_module/config_helper.rb', line 6

def config_file
  @config_file
end

#namespacesObject


58
59
60
# File 'lib/config_module/config_helper.rb', line 58

def namespaces
  @namespaces ||= []
end

#raw_configObject (readonly)

Returns the value of attribute raw_config.


5
6
7
# File 'lib/config_module/config_helper.rb', line 5

def raw_config
  @raw_config
end

Instance Method Details

#configObject

[View source]

9
10
11
# File 'lib/config_module/config_helper.rb', line 9

def config
  @config ||= ConfigOption.wrap load_config
end

#field_lookup_handler(name, _source, *_args, &_block) ⇒ Object

[View source]

28
29
30
# File 'lib/config_module/config_helper.rb', line 28

def field_lookup_handler name, _source, *_args, &_block
  config[name]
end

#load_configObject

[View source]

32
33
34
35
36
37
38
# File 'lib/config_module/config_helper.rb', line 32

def load_config
  raise ConfigModule::ConfigFileNotSpecified, config_file unless config_file
  raise ConfigModule::ConfigFileNotFound, config_file unless File.exist? config_file

  yaml_load
  load_namespaces_from raw_config
end

#load_namespaces_from(tree) ⇒ Object

[View source]

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/config_module/config_helper.rb', line 40

def load_namespaces_from tree
  namespaces.inject(ConfigOption.wrap(tree)) do |subtree, ns|
    if ConfigOption === subtree && ns.respond_to?(:to_sym) && subtree.has_key?(ns)
      ConfigOption.wrap subtree[ns]
    else
      raise(
        InvalidNamespaceError.new(ns, subtree, caller),
        "No subkey with name: #{ns.inspect}", caller(6)
      )
    end
  end
rescue TypeError
  raise(
    InvalidNamespaceError.new(namespaces.first, self, caller),
    "Namespace must be a string or symbol, instead it was: #{namespaces.first.class}", caller(6)
  )
end

#method_missing_handler(name, source, *args, &block) ⇒ Object

[View source]

13
14
15
16
17
18
19
20
21
22
# File 'lib/config_module/config_helper.rb', line 13

def method_missing_handler name, source, *args, &block
  ConfigOption.wrap config.send(name, *args, &block)
rescue NoMethodError => error
  raise unless error.name == name

  raise(
    ConfigOption::NotFoundError.new(name, self, error),
    error.message, source
  )
end

#respond_to_missing_handler(name, include_all) ⇒ Object

[View source]

24
25
26
# File 'lib/config_module/config_helper.rb', line 24

def respond_to_missing_handler name, include_all
  config.send(:respond_to_missing?, name, include_all)
end