module ModulePlus::ConfigConvention

Attributes

config_hash[R]
config_defs[R]

Public Class Methods

add_def(c) click to toggle source
# File lib/module_plus/config_convention.rb, line 26
def add_def(c)
  array = (c.path << c.key.to_s).reverse
  h = array.inject(c.sample){ |h, k| {k => h}}
  @config_hash ||= Hash.new { |h,k| h[k] = Hash.new(&h.default_proc) }
  @config_hash.deep_merge!( h )

end
config_dir() click to toggle source
# File lib/module_plus/config_convention.rb, line 67
def config_dir
  @config_dir
end
config_dir=(d) click to toggle source
# File lib/module_plus/config_convention.rb, line 63
def config_dir=(d)
  @config_dir ||= d
end
config_file() click to toggle source
# File lib/module_plus/config_convention.rb, line 51
def config_file
  @config_file || 'config.yml'
end
config_file=(f) click to toggle source
# File lib/module_plus/config_convention.rb, line 47
def config_file=(f)
  @config_file ||= f
end
dump(fs) click to toggle source
# File lib/module_plus/config_convention.rb, line 71
def dump(fs)
  YAML.dump(@config_hash, fs)
end
extended(klass) click to toggle source
Calls superclass method
# File lib/module_plus/config_convention.rb, line 19
def extended(klass)
  super
  klass.extend(ModulePlus::Identification)
end
field_path(array) click to toggle source
# File lib/module_plus/config_convention.rb, line 42
def field_path(array)
  field_path_position_no_head
  array.drop(@field_path_position)
end
field_path_position_no_head() click to toggle source
# File lib/module_plus/config_convention.rb, line 38
def field_path_position_no_head
  @field_path_position ||= 1
end
field_path_position_top() click to toggle source
# File lib/module_plus/config_convention.rb, line 34
def field_path_position_top
  @field_path_position ||= 0
end
root_dir() click to toggle source
# File lib/module_plus/config_convention.rb, line 59
def root_dir
  @config_root || ENV['HOME']
end
root_dir=(d) click to toggle source
# File lib/module_plus/config_convention.rb, line 55
def root_dir=(d)
  @config_root = d
end

Public Instance Methods

config() click to toggle source
# File lib/module_plus/config_convention.rb, line 76
def config
  @_config ||= config_load
end
config_dir() click to toggle source
# File lib/module_plus/config_convention.rb, line 82
def config_dir
  Pathname.new(ConfigConvention.root_dir)
    .join(ConfigConvention.config_dir || '.' + root_name.underscore)
end
config_file() click to toggle source
# File lib/module_plus/config_convention.rb, line 87
def config_file
  config_dir.join(ConfigConvention.config_file)
end

Protected Instance Methods

config_def(key, attributes = {} ) click to toggle source
# File lib/module_plus/config_convention.rb, line 93
def config_def(key, attributes = {} )
  @config_defs ||= {}
  c = ConfigDef.new(key, field_path, attributes)
  @config_defs[c.key] = c
  ConfigConvention.add_def(c)
end

Private Instance Methods

config_load() click to toggle source
# File lib/module_plus/config_convention.rb, line 104
def config_load
  if config_file.exist? and yaml = YAML.load_file(config_file)                
    c = field_path.inject(yaml) do |h, k|
      if h[k].is_a? Hash
        h[k]
      else
        raise NotFoundField.new( "Not Found Field #{k} in #{config_file}")
      end
    end
    diff = @config_defs.keys - c.keys
    raise NotFoundField.new( "Field Key: #{diff.join(", ")} in #{} #{config_file}") unless diff.empty?
    @config_defs.values.each do |d|
      c[d.key] = d.value(c[d.key])
    end
    ost = OpenStruct.new(c)
    ost.freeze
  else
    ost = OpenStruct.new
    ost.freeze
  end
end
field_path() click to toggle source
# File lib/module_plus/config_convention.rb, line 126
def field_path
  ConfigConvention.field_path(full_names.map{ |str| str.underscore })
end