class TivoHMO::Config

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/tivohmo/config.rb, line 9
def initialize
  super
  @primary_data = Data.new
  @secondary_data = Data.new
end

Public Instance Methods

get(scoped_key) click to toggle source
# File lib/tivohmo/config.rb, line 52
def get(scoped_key)
  scoped_key = Array(scoped_key)
  result = nil

  key = scoped_key.pop
  path = scoped_key
  (0..path.size).to_a.reverse.each do |i|
    partial = path[0, i] << key

    begin
      result = @secondary_data.deep_fetch(*partial)
    rescue Data::UndefinedPathError
      begin
        result = @primary_data.deep_fetch(*partial)
      rescue Data::UndefinedPathError
      end
    end

    break if ! result.nil?
  end

  if result.nil?
    registered = known_config[key]
    result = registered[:default_value] if registered
  end

  result
end
known_config() click to toggle source
# File lib/tivohmo/config.rb, line 48
def known_config
  @known_config ||= {}
end
reset() click to toggle source
# File lib/tivohmo/config.rb, line 42
def reset
  @primary_file = @secondary_file = nil
  @primary_data = @secondary_data = nil
  @known_config = nil
end
set(scoped_key, value, persist=true) click to toggle source
# File lib/tivohmo/config.rb, line 81
def set(scoped_key, value, persist=true)
  scoped_key = Array(scoped_key)
  key = scoped_key.pop

  val_hash = {key => value}
  scoped_key.reverse.each do |k|
    val_hash = {k => val_hash}
  end

  @secondary_data = @secondary_data.deep_merge(val_hash)
  File.write(@secondary_file, YAML.dump(@secondary_data)) if persist && @secondary_file

  registered = known_config[key]
  if registered && registered[:on_change]
    registered[:on_change].call
  end
end
setup(primary_filename, secondary_filename=nil) click to toggle source
# File lib/tivohmo/config.rb, line 16
def setup(primary_filename, secondary_filename=nil)
  @primary_file = primary_filename
  @secondary_file = secondary_filename

  @primary_data = Data.new
  @secondary_data = Data.new

  if File.exist?(@primary_file.to_s)
    logger.info "Loading primary config from: '#{@primary_file}'"
    @primary_data = Data.load(@primary_file)
  else
    logger.info "No primary config at file: '#{@primary_file}'"
  end

  # get secondary config from primary if present and not set explictly
  secondary = get(:settings)
  @secondary_file ||= File.expand_path(secondary) if secondary

  if File.exist?(@secondary_file.to_s)
    logger.info "Loading secondary config from: '#{@secondary_file}'"
    @secondary_data = Data.load(@secondary_file)
  else
    logger.info "No secondary config at file: '#{@secondary_file}'"
  end
end