Class: Olelo::Config
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #each(&block) ⇒ Object
- #freeze ⇒ Object
-
#initialize(base = nil) ⇒ Config
constructor
A new instance of Config.
- #load(file) ⇒ Object
- #load!(file) ⇒ Object
- #to_hash ⇒ Object
- #update(hash) ⇒ Object
Constructor Details
#initialize(base = nil) ⇒ Config
Returns a new instance of Config.
7 8 9 10 |
# File 'lib/olelo/config.rb', line 7 def initialize(base = nil) @hash = {} @base = base.freeze end |
Instance Attribute Details
#base ⇒ Object (readonly)
Returns the value of attribute base.
5 6 7 |
# File 'lib/olelo/config.rb', line 5 def base @base end |
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
5 6 7 |
# File 'lib/olelo/config.rb', line 5 def hash @hash end |
Class Method Details
.[](key) ⇒ Object
56 57 58 |
# File 'lib/olelo/config.rb', line 56 def self.[](key) instance[key] end |
Instance Method Details
#[](key) ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/olelo/config.rb', line 12 def [](key) key = key.to_s if i = key.index('.') not_found(key) unless Config === hash[key[0...i]] hash[key[0...i]][key[i+1..-1]] else not_found(key) if !hash.include?(key) hash[key] end end |
#[]=(key, value) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/olelo/config.rb', line 23 def []=(key, value) key = key.to_s if i = key.index('.') child(key[0...i])[key[i+1..-1]] = value elsif Hash === value child(key).update(value) else hash[key] = value.freeze end end |
#each(&block) ⇒ Object
48 49 50 |
# File 'lib/olelo/config.rb', line 48 def each(&block) hash.each(&block) end |
#freeze ⇒ Object
68 69 70 71 |
# File 'lib/olelo/config.rb', line 68 def freeze hash.freeze super end |
#load(file) ⇒ Object
40 41 42 |
# File 'lib/olelo/config.rb', line 40 def load(file) load!(file) if File.file?(file) end |
#load!(file) ⇒ Object
44 45 46 |
# File 'lib/olelo/config.rb', line 44 def load!(file) update(YAML.load_file(file)) end |
#to_hash ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/olelo/config.rb', line 60 def to_hash h = Hash.with_indifferent_access hash.each_pair do |k, v| h[k] = Config === v ? v.to_hash : v end h end |
#update(hash) ⇒ Object
34 35 36 37 38 |
# File 'lib/olelo/config.rb', line 34 def update(hash) hash.each_pair do |key, value| self[key] = value end end |