Class: Balmora::Config
- Inherits:
-
Object
- Object
- Balmora::Config
- Defined in:
- lib/balmora/config.rb
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#dir ⇒ Object
Returns the value of attribute dir.
-
#file ⇒ Object
Returns the value of attribute file.
-
#old ⇒ Object
readonly
Returns the value of attribute old.
-
#require ⇒ Object
Returns the value of attribute require.
-
#variables ⇒ Object
Returns the value of attribute variables.
Class Method Summary collapse
Instance Method Summary collapse
- #_deep_clone(value) ⇒ Object
- #_glob(file) ⇒ Object
- #_load(path) ⇒ Object
- #_process(value) ⇒ Object
- #_process_array(value) ⇒ Object
- #_process_hash(value) ⇒ Object
- #get(key = [], options = {}) ⇒ Object
-
#initialize(path, variables = nil) ⇒ Config
constructor
A new instance of Config.
- #load ⇒ Object
Constructor Details
#initialize(path, variables = nil) ⇒ Config
Returns a new instance of Config.
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/balmora/config.rb', line 32 def initialize(path, variables = nil) @file = File @dir = Dir @variables = variables @path = path @require = Object.method(:require) @config = nil end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
8 9 10 |
# File 'lib/balmora/config.rb', line 8 def config @config end |
#dir ⇒ Object
Returns the value of attribute dir.
7 8 9 |
# File 'lib/balmora/config.rb', line 7 def dir @dir end |
#file ⇒ Object
Returns the value of attribute file.
7 8 9 |
# File 'lib/balmora/config.rb', line 7 def file @file end |
#old ⇒ Object (readonly)
Returns the value of attribute old.
8 9 10 |
# File 'lib/balmora/config.rb', line 8 def old @old end |
#require ⇒ Object
Returns the value of attribute require.
7 8 9 |
# File 'lib/balmora/config.rb', line 7 def require @require end |
#variables ⇒ Object
Returns the value of attribute variables.
7 8 9 |
# File 'lib/balmora/config.rb', line 7 def variables @variables end |
Class Method Details
.create(config = nil, variables = nil) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/balmora/config.rb', line 14 def self.create(config = nil, variables = nil) if config.nil?() config = File.join(Dir.home(), '.config/balmora/balmora.conf') end if !File.exist?(config) config = File.join('/etc/balmora/balmora.conf') end if !File.exist?(config) raise Error.new('Config not found in ~/.config/balmora/balmora.conf ' + 'and /etc/balmora/balmora.conf; you should create config to ' + 'continue or use --config switch') end return self.new(config || '~/.config/balmora/balmora.conf', variables) end |
.factory(state) ⇒ Object
10 11 12 |
# File 'lib/balmora/config.rb', line 10 def self.factory(state) return self.create(state.[:config], state.variables) end |
Instance Method Details
#_deep_clone(value) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/balmora/config.rb', line 162 def _deep_clone(value) if value.instance_of?(::Array) value = value.collect() { |item| _deep_clone(item) } elsif value.instance_of?(::Hash) value = Hash[ value.collect() { |key, item| [_deep_clone(key), _deep_clone(item)] } ] else is_clonable = ( !value.is_a?(::Numeric) && !value.is_a?(::TrueClass) && !value.is_a?(::FalseClass) && !value.is_a?(::NilClass) && !value.is_a?(::Symbol) ) if is_clonable value = value.clone() end end return value end |
#_glob(file) ⇒ Object
154 155 156 157 158 159 160 |
# File 'lib/balmora/config.rb', line 154 def _glob(file) if !file.include?('*') return [file] end return @dir.glob(file) end |
#_load(path) ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/balmora/config.rb', line 88 def _load(path) contents = @file.read(@file.(path)) value = JSON.parse(contents, {symbolize_names: true}) value = _process(value) return value rescue => error raise Error.new("Failed to read config file #{path}: #{error.to_s()}") end |
#_process(value) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/balmora/config.rb', line 97 def _process(value) result = if value.instance_of?(::Array) _process_array(value) elsif value.instance_of?(::Hash) _process_hash(value) else value end return result end |
#_process_array(value) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/balmora/config.rb', line 110 def _process_array(value) result = [] value.each() { |item| if item.instance_of?(::Hash) && item.keys() == [:'extend-file'] files = item[:'extend-file'] if !files.instance_of?(::Array) files = [files] end files.each() { |path| _glob(path).each() { |file| result.concat(_load(file)) } } else result.push(_process(item)) end } return result end |
#_process_hash(value) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/balmora/config.rb', line 132 def _process_hash(value) result = {} value.each() { |key, item| if key == :'include-file' files = item if !files.instance_of?(::Array) files = [files] end files.each() { |path| _glob(path).each() { |file| result.merge!(_load(file)) } } else result[key] = _process(item) end } return result end |
#get(key = [], options = {}) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/balmora/config.rb', line 43 def get(key = [], = {}) if !key.instance_of?(::Array) key = [key] end result = key. inject(@config) { |current, part| begin current = current.fetch(part) rescue KeyError if [:default] return [:default] end raise "Config value #{key.inspect()} should be defined in " + "#{@path.inspect()}" end next current } result = _deep_clone(result) if [:variables] != false result = @variables.inject(result, false) end return result end |
#load ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/balmora/config.rb', line 73 def load() @old = _deep_clone(@config) Dir.chdir(File.dirname(@path)) { @config = _load(File.basename(@path)) } @config[:config_dir] = File.dirname(@path) @config[:config_path] = @path get(:require, default: [], variables: false).each() { |file| @require.call(file) } end |