Class: Nac::Config
- Inherits:
-
Object
- Object
- Nac::Config
- Defined in:
- lib/nac/config.rb
Overview
The main configuration manager class
Instance Attribute Summary collapse
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Instance Method Summary collapse
- #get(keys = nil, default = nil) ⇒ Object
- #init! ⇒ Object
-
#initialize(source, options = {}) ⇒ Config
constructor
A new instance of Config.
- #set(keys, value) ⇒ Object
Constructor Details
#initialize(source, options = {}) ⇒ Config
Returns a new instance of Config.
8 9 10 11 12 13 14 |
# File 'lib/nac/config.rb', line 8 def initialize(source, = {}) @source = source @template = ([:template] || {}).to_yaml [:init!] ? init! : init cache end |
Instance Attribute Details
#source ⇒ Object (readonly)
Returns the value of attribute source.
6 7 8 |
# File 'lib/nac/config.rb', line 6 def source @source end |
#template ⇒ Object (readonly)
Returns the value of attribute template.
6 7 8 |
# File 'lib/nac/config.rb', line 6 def template @template end |
Instance Method Details
#get(keys = nil, default = nil) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/nac/config.rb', line 24 def get(keys = nil, default = nil) return @cache unless keys val = @cache [keys].flatten.each do |key| val = val[key] end val rescue NoMethodError default end |
#init! ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/nac/config.rb', line 16 def init! FileUtils.mkdir_p(File.dirname(@source)) File.open(@source, 'w+') do |file| file.write(@template) end end |
#set(keys, value) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/nac/config.rb', line 38 def set(keys, value) @cache.merge!(bury(keys, value)) File.write(@source, @cache.to_yaml) cache! end |