Class: Fig

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ Fig

Constructor…

Params

  • file_path - Path to the config file that should be loaded.



11
12
13
14
# File 'lib/fig.rb', line 11

def initialize(file_path)
  @file_path = file_path
  load
end

Instance Attribute Details

#settingsObject (readonly)

Returns an OpenStruct object representation of the config file. This allows you to access config settings with dot notation.



22
23
24
# File 'lib/fig.rb', line 22

def settings
  @settings
end

#yamlObject (readonly)

Returns the config file file as a YAML Hash.



17
18
19
# File 'lib/fig.rb', line 17

def yaml
  @yaml
end

Instance Method Details

#get_setting(key) ⇒ Object

The safest way to get a config setting. Requesting a non-exsisting key, will simply return a nil value instead of raising an error.

Examples: Fig.get_setting(‘some.nested.setting’)

Params

  • key - A case insensivie config key

Returns The value of the config setting requested.

This may be the value itself or an OpenStruct containing child args


35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fig.rb', line 35

def get_setting(key)
  setting = settings
  keys = key.to_s.downcase.split(/\./)

  keys.each do |k|
    item = eval("setting.#{k}")
    return nil unless item
    setting = item
  end

  setting
end

#loadObject

Loads the config file and builds the internal Fig objects. Can be used to reload the file when changes have been made.



50
51
52
53
54
55
# File 'lib/fig.rb', line 50

def load
  @yaml = YAML.load_file(@file_path)
  @yaml.each {|k, v| interpolate_setting(v)}
  @settings = OpenStruct.new
  add_hash(@settings, @yaml)
end