Class: Configuration
- Inherits:
-
Object
- Object
- Configuration
- Defined in:
- lib/configuration.rb
Overview
Indexing
Author: Stefan Rusterholz Contact: [email protected]> Version: 1.0.0 Date: 2007-10-12
About
Map configuration keys/data to files
Synopsis
config = Configuration.new("my/config")
config["some/key"] = "Some value"
config["some/key"] = config["some/key"].upcase # can't use upcase!
Defined Under Namespace
Modules: VERSION Classes: ConfFile, InvalidKey
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #delete(key) ⇒ Object
- #exist?(key) ⇒ Boolean (also: #has_key?)
-
#initialize(base, dir_tree = []) ⇒ Configuration
constructor
A new instance of Configuration.
- #setup(dirs) ⇒ Object
Constructor Details
#initialize(base, dir_tree = []) ⇒ Configuration
Returns a new instance of Configuration.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/configuration.rb', line 53 def initialize(base, dir_tree=[]) # make it resistant against Dir.chdir @base = File.(base).freeze @files = {} unless File.directory?(@base) && File.exist?("#@base/.configdir.yaml") then if File.file?(@base) then raise "Can't create directory #@base because of existing file #@base" end if File.exist?(@base) then raise "Directory #@base already exists and is not a configuration directory" end end unless File.exist?(@base) then FileUtils.mkdir_p(@base) File.open("#@base/.configdir.yaml", "w") { |fh| fh.write({ :created => Time.now, :modified => Time.now, :revision => 1, }.to_yaml) } end setup(dir_tree) end |
Instance Attribute Details
#base ⇒ Object (readonly)
Returns the value of attribute base.
51 52 53 |
# File 'lib/configuration.rb', line 51 def base @base end |
Instance Method Details
#[](key) ⇒ Object
79 80 81 82 83 |
# File 'lib/configuration.rb', line 79 def [](key) file, key = split(key) return nil unless key_exist?(file, key) key ? @files[file][key] : @files[file] end |
#[]=(key, value) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/configuration.rb', line 85 def []=(key, value) file, key = split(key) load(file) if key then if Hash === @files[file] then @files[file][key] = value else @files[file] = {key => value} end else @files[file] = value end store(file) value end |
#delete(key) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/configuration.rb', line 107 def delete(key) file, key = split(key) return nil unless key_exist?(file, key) and File.exist?(file) if key.empty? then File.delete(file) else load(file) @files[file].delete(key) store(file) end end |
#exist?(key) ⇒ Boolean Also known as: has_key?
101 102 103 104 |
# File 'lib/configuration.rb', line 101 def exist?(key) file, key = split(key) key_exist?(file, key) end |
#setup(dirs) ⇒ Object
119 120 121 |
# File 'lib/configuration.rb', line 119 def setup(dirs) FileUtils.mkdir_p(dirs.map { |dir| "#@base/#{dir}" }) end |