Class: Resedit::Config
- Inherits:
-
Object
- Object
- Resedit::Config
- Defined in:
- lib/resedit/classes/config.rb
Instance Attribute Summary collapse
-
#cfg ⇒ Object
Returns the value of attribute cfg.
Instance Method Summary collapse
- #[](nm) ⇒ Object
- #[]=(nm, value) ⇒ Object
- #each(&block) ⇒ Object
- #enter(section, createOnAbsent = true) ⇒ Object
- #enterOnFile(path) ⇒ Object
-
#initialize(fname, section = nil) ⇒ Config
constructor
A new instance of Config.
- #length ⇒ Object
- #load ⇒ Object
- #save ⇒ Object
Constructor Details
#initialize(fname, section = nil) ⇒ Config
Returns a new instance of Config.
8 9 10 11 12 13 14 15 16 |
# File 'lib/resedit/classes/config.rb', line 8 def initialize(fname, section=nil) @fname = fname @section = section ? section : [] @section = [@section] if !@section.is_a?(Array) @cfg = load() @section.each{|s| @cfg = (@cfg[s] or {}) } end |
Instance Attribute Details
#cfg ⇒ Object
Returns the value of attribute cfg.
7 8 9 |
# File 'lib/resedit/classes/config.rb', line 7 def cfg @cfg end |
Instance Method Details
#[](nm) ⇒ Object
18 |
# File 'lib/resedit/classes/config.rb', line 18 def [](nm); @cfg[nm] end |
#[]=(nm, value) ⇒ Object
19 |
# File 'lib/resedit/classes/config.rb', line 19 def []=(nm, value); @cfg[nm]=value end |
#each(&block) ⇒ Object
20 |
# File 'lib/resedit/classes/config.rb', line 20 def each(&block);@cfg.each(&block) end |
#enter(section, createOnAbsent = true) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/resedit/classes/config.rb', line 23 def enter(section, createOnAbsent = true) section = [section] if !section.is_a?(Array) section.each{|s| raise "Config section not found #{s} at #{@section}" if !@cfg[s] && !createOnAbsent @cfg = @cfg[s] or {} @section += [s] } end |
#enterOnFile(path) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/resedit/classes/config.rb', line 32 def enterOnFile(path) ret = nil ent = '' @cfg.each{|k, _| fn = File.join(path, k) ret = fn if File.exists?(fn) ent = k if ret break if ret } raise "Config not found for files at #{path}" if !ret enter(ent, false) return ret end |
#length ⇒ Object
21 |
# File 'lib/resedit/classes/config.rb', line 21 def length; @cfg.length end |
#load ⇒ Object
46 47 48 |
# File 'lib/resedit/classes/config.rb', line 46 def load(); File.exists?(@fname) ? JSON.parse(File.read(@fname)) : {} end |
#save ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/resedit/classes/config.rb', line 50 def save() if @section.length>0 c = load() cur = c @section[0..-2].each{|s| cur[s] = {} if !cur[s] cur = cur[s] } cur[@section[-1]]=@cfg else c = @cfg end open(@fname, "w"){|f| f.write(JSON.pretty_generate(c)) } end |