Class: Hopsoft::Fig
- Inherits:
-
Object
- Object
- Hopsoft::Fig
- Defined in:
- lib/hopsoft/fig.rb
Instance Method Summary collapse
-
#get_setting(key) ⇒ Object
The safest way to get a config setting.
-
#initialize(file_path) ⇒ Fig
constructor
Constructor…
-
#load ⇒ Object
Loads the config file and builds the internal Fig objects.
-
#settings ⇒ Object
Returns an OpenStruct object representation of the config file.
-
#yaml ⇒ Object
Returns the config file file as a YAML Hash.
Constructor Details
#initialize(file_path) ⇒ Fig
Constructor…
Params
-
file_path
- Path to the config file that should be loaded.
13 14 15 16 17 |
# File 'lib/hopsoft/fig.rb', line 13 def initialize(file_path) @lock = Mutex.new @file_path = file_path load 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.
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.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/hopsoft/fig.rb', line 48 def get_setting(key) setting = nil @lock.synchronize do setting = @settings keys = key.to_s.downcase.split(/\./) keys.each do |k| item = eval("setting.#{k}") if setting.is_a?(OpenStruct) return nil unless item setting = item end end setting end |
#load ⇒ Object
Loads the config file and builds the internal Fig objects. Can be used to reload the file when changes have been made.
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/hopsoft/fig.rb', line 67 def load yaml = YAML.load_file(@file_path) yaml.each {|k, v| interpolate_setting(yaml, v)} settings = OpenStruct.new add_hash(settings, yaml) @lock.synchronize do @yaml = yaml @settings = settings end rescue puts "Failed to load file: #{@file_path}\n#{$!}" end |
#settings ⇒ Object
Returns an OpenStruct object representation of the config file. This allows access to config settings via dot notation.
31 32 33 34 35 36 37 |
# File 'lib/hopsoft/fig.rb', line 31 def settings copy = OpenStruct.new @lock.synchronize do copy.marshal_load(@settings.marshal_dump) end copy end |
#yaml ⇒ Object
Returns the config file file as a YAML Hash.
20 21 22 23 24 25 26 |
# File 'lib/hopsoft/fig.rb', line 20 def yaml copy = {} @lock.synchronize do copy.merge!(@yaml) end copy end |