Class: Bonethug::Conf
- Inherits:
-
Object
- Object
- Bonethug::Conf
- Defined in:
- lib/bonethug/conf.rb
Constant Summary collapse
- @@default_paths =
{ File.('./config/cnf.yml') => nil, File.('./config/database.yml') => {root: 'dbs.default'} }
- @@fallbacks =
{ 'name' => 'database', 'user' => 'username', 'pass' => 'password' }
Instance Method Summary collapse
-
#a2h(arr) ⇒ Object
Method Aliases ————–.
- #add(new_path) ⇒ Object
- #add_path(new_path) ⇒ Object
- #all_paths_loaded? ⇒ Boolean
- #array2hash(arr) ⇒ Object
- #check_path!(path) ⇒ Object
- #check_paths ⇒ Object
- #check_paths! ⇒ Object
- #compile_configuration ⇒ Object
- #compiled_hash ⇒ Object
- #compiled_hash=(new_hash) ⇒ Object
- #config_hashes ⇒ Object
- #each ⇒ Object
- #get(node = nil, force_type = nil) ⇒ Object
- #get_compiled_hash_node_handle(node = nil) ⇒ Object
- #get_hash(node = nil) ⇒ Object
- #handle_node_value(node) ⇒ Object
- #has_key?(key) ⇒ Boolean
-
#initialize(new_hash = nil, options = {}) ⇒ Conf
constructor
A new instance of Conf.
- #load_path(path) ⇒ Object
- #load_path?(path) ⇒ Boolean
- #load_paths ⇒ Object
- #merge(node) ⇒ Object
- #node_merge(node1, node2) ⇒ Object
- #node_merge!(node1, node2) ⇒ Object
- #path_ok?(path) ⇒ Boolean
- #paths ⇒ Object
-
#paths=(new_paths) ⇒ Object
Getters and Setters ——————-.
- #remove(path_to_remove) ⇒ Object
- #remove_path(path_to_remove) ⇒ Object
- #set_compiled_hash(new_hash) ⇒ Object
- #to_a ⇒ Object
- #to_hash ⇒ Object
- #use_defaults ⇒ Object
Constructor Details
#initialize(new_hash = nil, options = {}) ⇒ Conf
Returns a new instance of Conf.
17 18 19 20 21 22 23 24 |
# File 'lib/bonethug/conf.rb', line 17 def initialize(new_hash = nil, = {}) raise "New hash must be of type Hash" if new_hash && new_hash.class.name != 'Hash' @options = {use_fallbacks: true}.merge @loaded_paths = [] @paths = {} @config_hashes = {} @compiled_hash = new_hash ? new_hash : {} end |
Instance Method Details
#a2h(arr) ⇒ Object
Method Aliases
262 263 264 |
# File 'lib/bonethug/conf.rb', line 262 def a2h(arr) array2hash arr end |
#add(new_path) ⇒ Object
270 271 272 |
# File 'lib/bonethug/conf.rb', line 270 def add(new_path) add_path new_path end |
#add_path(new_path) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/bonethug/conf.rb', line 26 def add_path(new_path) if new_path.class.name == 'Hash' path_hash = new_path elsif new_path.class.name == 'String' path_hash = {new_path => nil} else raise "add_path only accepts stings or hashes" end @paths = @paths.merge path_hash self end |
#all_paths_loaded? ⇒ Boolean
80 81 82 83 84 85 |
# File 'lib/bonethug/conf.rb', line 80 def all_paths_loaded? @paths.each do |path,| return false unless @loaded_paths.include? path end true end |
#array2hash(arr) ⇒ Object
180 181 182 183 184 185 186 187 |
# File 'lib/bonethug/conf.rb', line 180 def array2hash(arr) return arr if arr.class.name == 'Hash' hsh = {} arr.each_with_index do |item,i| hsh[i] = item end hsh end |
#check_path!(path) ⇒ Object
91 92 93 |
# File 'lib/bonethug/conf.rb', line 91 def check_path!(path) raise 'config file "' + path.to_s + '" does not exist' unless path_ok? path end |
#check_paths ⇒ Object
103 104 105 106 107 108 |
# File 'lib/bonethug/conf.rb', line 103 def check_paths @paths.each do |path,| @paths.delete path unless path_ok? path end self end |
#check_paths! ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/bonethug/conf.rb', line 95 def check_paths! raise 'No config files have not been set' if @paths.empty? @paths.each do |path,| check_path! path end self end |
#compile_configuration ⇒ Object
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 72 73 74 75 76 77 78 |
# File 'lib/bonethug/conf.rb', line 44 def compile_configuration # load the defaults if we haven't loaded anything use_defaults if @paths.empty? # generate output out = {} @paths.each do |path,| # load the file if we haven't already load_path path unless @loaded_paths.include? path # create a base fragment fragment_base = {} # create the other nodes if and .has_key? :root fragment = fragment_base nodes = [:root].split '.' nodes.each_with_index do |node,i| fragment[node] = i == nodes.length-1 ? @config_hashes[path] : {} fragment = fragment[node] end else fragment_base = @config_hashes[path] end # output out = out.merge fragment_base end @compiled_hash = out self end |
#compiled_hash ⇒ Object
243 244 245 246 |
# File 'lib/bonethug/conf.rb', line 243 def compiled_hash compile_configuration if @compiled_hash.empty? @compiled_hash end |
#compiled_hash=(new_hash) ⇒ Object
248 249 250 251 |
# File 'lib/bonethug/conf.rb', line 248 def compiled_hash=(new_hash) raise "compiled hash must be a hash" unless new_hash.class.name == 'Hash' @compiled_hash = new_hash end |
#config_hashes ⇒ Object
239 240 241 |
# File 'lib/bonethug/conf.rb', line 239 def config_hashes @config_hashes end |
#each ⇒ Object
221 222 223 224 225 |
# File 'lib/bonethug/conf.rb', line 221 def each compiled_hash.each do |k,v| yield k,handle_node_value(v) end end |
#get(node = nil, force_type = nil) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/bonethug/conf.rb', line 135 def get(node = nil, force_type = nil) node_val = node ? get_compiled_hash_node_handle(node) : self case force_type when 'Array' return [] unless node_val return [node_val] if node_val.class.name == 'String' return node_val.class.name == 'Array' ? node_val.clone : node_val.to_a when 'Hash' return {} unless node_val if node_val.class.name == 'Array' return array2hash node_val elsif node_val.class.name == 'Hash' return node_val.clone else return node_val.to_hash end else return handle_node_value node_val end end |
#get_compiled_hash_node_handle(node = nil) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/bonethug/conf.rb', line 160 def get_compiled_hash_node_handle(node = nil) if node nodes = node.split('.') current = compiled_hash nodes.each do |node| node = @@fallbacks[node] if @options[:use_fallbacks] and !current[node] and @@fallbacks[node] current = (current.class.name == 'Hash' or current.class.name == 'Array') ? current[node] : nil end return current else return self.compiled_hash end end |
#get_hash(node = nil) ⇒ Object
189 190 191 |
# File 'lib/bonethug/conf.rb', line 189 def get_hash(node = nil) get(node).compiled_hash end |
#handle_node_value(node) ⇒ Object
174 175 176 177 178 |
# File 'lib/bonethug/conf.rb', line 174 def handle_node_value(node) return node if node.class.name == 'Conf' node = array2hash node if node.class.name == 'Array' return node.class.name == 'Hash' ? self.clone.set_compiled_hash(node) : node end |
#has_key?(key) ⇒ Boolean
156 157 158 |
# File 'lib/bonethug/conf.rb', line 156 def has_key?(key) compiled_hash.has_key? key end |
#load_path(path) ⇒ Object
117 118 119 120 |
# File 'lib/bonethug/conf.rb', line 117 def load_path(path) load_path? path self end |
#load_path?(path) ⇒ Boolean
122 123 124 125 126 127 |
# File 'lib/bonethug/conf.rb', line 122 def load_path?(path) return false unless path_ok? path @loaded_paths.push path @config_hashes[path] = YAML.load_file path self end |
#load_paths ⇒ Object
110 111 112 113 114 115 |
# File 'lib/bonethug/conf.rb', line 110 def load_paths @paths.each do |path,| load_path path end self end |
#merge(node) ⇒ Object
216 217 218 219 |
# File 'lib/bonethug/conf.rb', line 216 def merge(node) return handle_node_value compiled_hash.merge(node.to_hash) if node return self end |
#node_merge(node1, node2) ⇒ Object
208 209 210 211 212 213 214 |
# File 'lib/bonethug/conf.rb', line 208 def node_merge(node1,node2) cnf1 = get_compiled_hash_node_handle node1 cnf2 = get_compiled_hash_node_handle node2 return handle_node_value cnf1 if cnf1 && !cnf2 return handle_node_value cnf1 if cnf2 && !cnf1 return handle_node_value cnf1.merge(cnf2) if cnf1 && cnf2 end |
#node_merge!(node1, node2) ⇒ Object
201 202 203 204 205 206 |
# File 'lib/bonethug/conf.rb', line 201 def node_merge!(node1,node2) cnf1 = get_compiled_hash_node_handle node1 cnf2 = get_compiled_hash_node_handle node2 cnf1.merge!(cnf2) if cnf1 && cnf2 return self end |
#path_ok?(path) ⇒ Boolean
87 88 89 |
# File 'lib/bonethug/conf.rb', line 87 def path_ok?(path) path && path.class.name == 'String' and File.exist?(path) and File.file?(path) end |
#paths ⇒ Object
235 236 237 |
# File 'lib/bonethug/conf.rb', line 235 def paths @paths end |
#paths=(new_paths) ⇒ Object
Getters and Setters
230 231 232 233 |
# File 'lib/bonethug/conf.rb', line 230 def paths=(new_paths) raise "paths must be a hash" unless new_hash.class.name == 'Hash' @paths = new_paths end |
#remove(path_to_remove) ⇒ Object
274 275 276 |
# File 'lib/bonethug/conf.rb', line 274 def remove(path_to_remove) remove_path path_to_remove end |
#remove_path(path_to_remove) ⇒ Object
38 39 40 41 42 |
# File 'lib/bonethug/conf.rb', line 38 def remove_path(path_to_remove) # deletes an element from a hash if its key can be found @paths.delete path_to_remove self end |
#set_compiled_hash(new_hash) ⇒ Object
253 254 255 256 257 |
# File 'lib/bonethug/conf.rb', line 253 def set_compiled_hash(new_hash) raise "compiled hash must be a hash" unless new_hash.class.name == 'Hash' @compiled_hash = new_hash self end |
#to_a ⇒ Object
197 198 199 |
# File 'lib/bonethug/conf.rb', line 197 def to_a to_hash.to_a end |
#to_hash ⇒ Object
193 194 195 |
# File 'lib/bonethug/conf.rb', line 193 def to_hash compiled_hash.clone end |
#use_defaults ⇒ Object
129 130 131 132 133 |
# File 'lib/bonethug/conf.rb', line 129 def use_defaults @paths = @@default_paths if @paths.empty? load_paths self end |