class Nucleon::Config

Public Class Methods

array(data, default = [], split_string = false) click to toggle source
# File lib/core/config.rb, line 291
def self.array(data, default = [], split_string = false)
  return Util::Data.array(data, default, split_string)
end
ensure(config) click to toggle source
# File lib/core/config.rb, line 14
def self.ensure(config)
  case config
  when Nucleon::Config
    return config
  when Hash
    return new(config) 
  end
  return new
end
filter(data, method = false) click to toggle source
# File lib/core/config.rb, line 279
def self.filter(data, method = false)
  return Util::Data.filter(data, method)
end
hash(data, default = {}) click to toggle source
# File lib/core/config.rb, line 303
def self.hash(data, default = {})
  data = data.export if data.is_a?(Nucleon::Config)
  return Util::Data.hash(data, default)
end
init(options, contexts = [], hierarchy = [], defaults = {}) click to toggle source
# File lib/core/config.rb, line 26
def self.init(options, contexts = [], hierarchy = [], defaults = {})
  contexts = contexts(contexts, hierarchy)
  config   = new(get_options(contexts), defaults)
  config.import(options) unless Util::Data.empty?(options)
  return config
end
init_flat(options, contexts = [], defaults = {}) click to toggle source
# File lib/core/config.rb, line 35
def self.init_flat(options, contexts = [], defaults = {})
  return init(options, contexts, [], defaults)
end
new(data = {}, defaults = {}, force = true) click to toggle source
# File lib/core/config.rb, line 42
def initialize(data = {}, defaults = {}, force = true)
  @force      = force
  @properties = {}
  
  if defaults.is_a?(Hash) && ! defaults.empty?
    defaults = symbol_map(defaults.clone)
  end
  
  case data
  when Nucleon::Config
    @properties = Util::Data.merge([ defaults, data.export ], force)
  when Hash
    @properties = {}
    if data.is_a?(Hash)
      @properties = Util::Data.merge([ defaults, symbol_map(data.clone) ], force)
    end  
  end
end
string(data, default = '') click to toggle source
# File lib/core/config.rb, line 316
def self.string(data, default = '')
  return Util::Data.string(data, default)
end
string_map(data) click to toggle source
# File lib/core/config.rb, line 267
def self.string_map(data)
  return Util::Data.string_map(data)
end
symbol(data, default = :undefined) click to toggle source
# File lib/core/config.rb, line 328
def self.symbol(data, default = :undefined)
  return Util::Data.symbol(data, default)
end
symbol_map(data) click to toggle source
# File lib/core/config.rb, line 255
def self.symbol_map(data)
  return Util::Data.symbol_map(data)
end
test(data) click to toggle source
# File lib/core/config.rb, line 340
def self.test(data)
  return Util::Data.test(data)
end

Public Instance Methods

[](name, default = nil, format = false) click to toggle source
# File lib/core/config.rb, line 149
def [](name, default = nil, format = false)
  get(name, default, format)
end
[]=(name, value) click to toggle source
# File lib/core/config.rb, line 180
def []=(name, value)
  set(name, value)
end
array(data, default = [], split_string = false) click to toggle source
# File lib/core/config.rb, line 297
def array(data, default = [], split_string = false)
  return self.class.array(data, default, split_string)
end
clear() click to toggle source
# File lib/core/config.rb, line 194
def clear
  @properties = {}
  return self
end
defaults(defaults, options = {}) click to toggle source
# File lib/core/config.rb, line 241
def defaults(defaults, options = {})
  config = Config.new(options).set(:import_type, :default)
  return import_base(defaults, config)
end
delete(keys, default = nil) click to toggle source
# File lib/core/config.rb, line 186
def delete(keys, default = nil)
  existing = modify(@properties, array(keys).flatten, nil)
  return existing[:value] unless existing[:value].nil?
  return default
end
empty?() click to toggle source
# File lib/core/config.rb, line 64
def empty?
  @properties.empty?
end
export() click to toggle source
# File lib/core/config.rb, line 248
def export
  return @properties.clone
end
filter(data, method = false) click to toggle source
# File lib/core/config.rb, line 285
def filter(data, method = false)
  return self.class.filter(data, method)
end
get(keys, default = nil, format = false) click to toggle source
# File lib/core/config.rb, line 143
def get(keys, default = nil, format = false)
  return fetch(@properties, array(keys).flatten, default, format)
end
get_array(keys, default = []) click to toggle source
# File lib/core/config.rb, line 155
def get_array(keys, default = [])
  return get(keys, default, :array)
end
get_hash(keys, default = {}) click to toggle source
# File lib/core/config.rb, line 161
def get_hash(keys, default = {})
  return get(keys, default, :hash)
end
has_key?(keys) click to toggle source
# File lib/core/config.rb, line 70
def has_key?(keys)
  get(keys).nil? ? false : true
end
hash(data, default = {}) click to toggle source
# File lib/core/config.rb, line 310
def hash(data, default = {})
  return self.class.hash(data, default)
end
import(properties, options = {}) click to toggle source
# File lib/core/config.rb, line 235
def import(properties, options = {})
  return import_base(properties, options)
end
init(keys, default = nil) click to toggle source
# File lib/core/config.rb, line 167
def init(keys, default = nil)
  return set(keys, get(keys, default))
end
keys() click to toggle source
# File lib/core/config.rb, line 77
def keys
  @properties.keys
end
set(keys, value) click to toggle source
# File lib/core/config.rb, line 173
def set(keys, value)
  modify(@properties, array(keys).flatten, value)
  return self
end
string(data, default = '') click to toggle source
# File lib/core/config.rb, line 322
def string(data, default = '')
  return self.class.string(data, default)
end
string_map(data) click to toggle source
# File lib/core/config.rb, line 273
def string_map(data)
  return self.class.string_map(data)
end
symbol(data, default = :undefined) click to toggle source
# File lib/core/config.rb, line 334
def symbol(data, default = :undefined)
  return self.class.symbol(data, default)
end
symbol_map(data) click to toggle source
# File lib/core/config.rb, line 261
def symbol_map(data)
  return self.class.symbol_map(data)
end
test(data) click to toggle source
# File lib/core/config.rb, line 346
def test(data)
  return self.class.test(data)
end

Protected Instance Methods

fetch(data, keys, default = nil, format = false) click to toggle source
# File lib/core/config.rb, line 83
def fetch(data, keys, default = nil, format = false)
  if keys.is_a?(String) || keys.is_a?(Symbol)
    keys = [ keys ]
  end
  
  keys = keys.flatten.compact    
  key  = keys.shift.to_sym
  
  if data.has_key?(key)
    value = data[key]
    
    if keys.empty?
      return filter(value, format)
    else
      return fetch(data[key], keys, default, format) if data[key].is_a?(Hash)   
    end
  end
  return filter(default, format)
end
import_base(properties, options = {}) click to toggle source
# File lib/core/config.rb, line 202
def import_base(properties, options = {})
  config      = Config.new(options, { :force => @force }).set(:context, :hash)    
  import_type = config.get(:import_type, :override)
  
  properties  = properties.export if properties.is_a?(Nucleon::Config)
  
  case properties
  when Hash
    data = [ @properties, symbol_map(properties.clone) ]
    data = data.reverse if import_type != :override
    
    @properties = Util::Data.merge(data, config)
  
  when String, Symbol
    properties = self.class.lookup(properties.to_s, {}, config)
    
    data = [ @properties, symbol_map(properties) ]
    data = data.reverse if import_type != :override
  
    @properties = Util::Data.merge(data, config)
   
  when Array
    properties.clone.each do |item|
      import_base(item, config)
    end
  end
  
  return self
end
modify(data, keys, value = nil) click to toggle source
# File lib/core/config.rb, line 106
def modify(data, keys, value = nil) 
  if keys.is_a?(String) || keys.is_a?(Symbol)
    keys = [ keys ]
  end
  
  keys     = keys.flatten.compact
  key      = keys.shift.to_sym
  has_key  = data.has_key?(key)
  existing = { 
    :key   => key, 
    :value => ( has_key ? data[key] : nil ) 
  }
  
  if keys.empty?      
    existing[:value] = data[key] if has_key
    
    if value.nil?
      data.delete(key) if has_key
    else
      data[key] = value
    end
  else      
    data[key] = {} unless has_key
    
    if data[key].is_a?(Hash)
      existing = modify(data[key], keys, value)
    else
      existing[:value] = nil 
    end  
  end
  
  return existing
end