class Bonethug::Conf

Public Class Methods

new(new_hash = nil, options = {}) click to toggle source
# File lib/bonethug/conf.rb, line 17
def initialize(new_hash = nil, options = {})
  raise "New hash must be of type Hash" if new_hash && new_hash.class.name != 'Hash'
  @options = {use_fallbacks: true}.merge options
  @loaded_paths = []
  @paths = {}
  @config_hashes = {}
  @compiled_hash = new_hash ? new_hash : {}
end

Public Instance Methods

a2h(arr) click to toggle source

Method Aliases


# File lib/bonethug/conf.rb, line 262
def a2h(arr)
  array2hash arr
end
add(new_path) click to toggle source
# File lib/bonethug/conf.rb, line 270
def add(new_path)
  add_path new_path
end
add_path(new_path) click to toggle source
# 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?() click to toggle source
# File lib/bonethug/conf.rb, line 80
def all_paths_loaded?
  @paths.each do |path,options|
    return false unless @loaded_paths.include? path
  end
  true
end
array2hash(arr) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/bonethug/conf.rb, line 103
def check_paths
  @paths.each do |path,options|
    @paths.delete path unless path_ok? path
  end
  self
end
check_paths!() click to toggle source
# 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,options|
    check_path! path
  end
  self
end
compile_configuration() click to toggle source
# 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,options|

    # 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 options and options.has_key? :root
      fragment = fragment_base
      nodes = options[: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() click to toggle source
# File lib/bonethug/conf.rb, line 243
def compiled_hash
  compile_configuration if @compiled_hash.empty?
  @compiled_hash
end
compiled_hash=(new_hash) click to toggle source
# 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() click to toggle source
# File lib/bonethug/conf.rb, line 239
def config_hashes
  @config_hashes
end
each() { |k,handle_node_value(v)| ... } click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/bonethug/conf.rb, line 189
def get_hash(node = nil)
  get(node).compiled_hash
end
handle_node_value(node) click to toggle source
# 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) click to toggle source
# File lib/bonethug/conf.rb, line 156
def has_key?(key)
  compiled_hash.has_key? key
end
load_path(path) click to toggle source
# File lib/bonethug/conf.rb, line 117
def load_path(path)
  load_path? path
  self
end
load_path?(path) click to toggle source
# 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() click to toggle source
# File lib/bonethug/conf.rb, line 110
def load_paths
  @paths.each do |path,options|
    load_path path
  end
  self
end
merge(node) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/bonethug/conf.rb, line 235
def paths
  @paths
end
paths=(new_paths) click to toggle source

Getters and Setters


# 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) click to toggle source
# File lib/bonethug/conf.rb, line 274
def remove(path_to_remove)
  remove_path path_to_remove
end
remove_path(path_to_remove) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/bonethug/conf.rb, line 197
def to_a
  to_hash.to_a
end
to_hash() click to toggle source
# File lib/bonethug/conf.rb, line 193
def to_hash
  compiled_hash.clone
end
use_defaults() click to toggle source
# File lib/bonethug/conf.rb, line 129
def use_defaults
  @paths = @@default_paths if @paths.empty?
  load_paths
  self
end