class NCC::Config

Constants

Infinite

Attributes

file[R]

-jbrinkley/20130403

mtime[R]

-jbrinkley/20130403

Public Class Methods

new(source = ["/etc/ncc-api", " click to toggle source
# File lib/ncc/config.rb, line 35
def initialize(source = ["/etc/ncc-api",
                   "#{ENV['NCCAPI_HOME']}/etc"], opt={})
    @opt = opt
    @file = { }
    @mtime = nil
    source = [source] unless source.respond_to? :select
    @file = source.select { |f| File.exist? f }.first
    unless @file
        raise ArgumentError.new("Can't locate configuration in " +
                            "#{source.inspect}")
    end
    debug "Creating configuration from #{@file}"
    update_config
end

Public Instance Methods

[](key) click to toggle source
# File lib/ncc/config.rb, line 178
def [](key)
    update_config unless current?
    @data[key]
end
[]=(key, value) click to toggle source
# File lib/ncc/config.rb, line 183
def []=(key, value)
    update_config unless current?
    @mtime = Time.now
    @data[key] = value
end
current?() click to toggle source
# File lib/ncc/config.rb, line 163
def current?
    debug "checking currency (mtime=#{@mtime} " +
        "file=#{File.exist?(@file) ? File.stat(@file).mtime : nil})"
    case opt :staleness_threshold
    when 0, nil
        debug "staleness_threshold is 0 or nil, always checking"
        File.exist? @file and @mtime >= File.stat(@file).mtime
    when Infinite
        true
    else
        Time.now <= (@mtime + opt(:staleness_threshold)) or
            (File.exist? @file and @mtime >= File.stat(@file).mtime)
    end
end
debug(msg) click to toggle source
# File lib/ncc/config.rb, line 107
def debug(msg)
    # This produces a *lot* of debug logging, thus it's best to be
    # able to use something else to control its log level.
    if ENV.has_key? 'NCC_CONFIG_DEBUG' and not ENV['NCC_CONFIG_DEBUG'].empty?
        if opt(:logger) and opt(:logger).respond_to? :debug
            opt(:logger).debug "#<#{me}>: #{msg}"
        end
    end
end
delete(k) click to toggle source
# File lib/ncc/config.rb, line 206
def delete(k)
    update_config unless current?
    @data.delete(k)
end
do_warn(msg) click to toggle source
# File lib/ncc/config.rb, line 101
def do_warn(msg)
    if opt :logger
        opt(:logger).warn msg
    end
end
each() { |k, v| ... } click to toggle source
# File lib/ncc/config.rb, line 199
def each
    update_config unless current?
    @data.each do |k, v|
        yield k, v
    end
end
has_key?(key) click to toggle source
# File lib/ncc/config.rb, line 189
def has_key?(key)
    update_config unless current?
    @data.has_key? key
end
keys() click to toggle source
# File lib/ncc/config.rb, line 194
def keys
    update_config unless current?
    @data.keys
end
me() click to toggle source
# File lib/ncc/config.rb, line 117
def me
    "#{self.class}:#{@file}"
end
opt(optname) click to toggle source
# File lib/ncc/config.rb, line 50
def opt(optname)
    if @opt.has_key? optname
        @opt[optname]
    else
        nil
    end
end
to_array(*keys) click to toggle source
# File lib/ncc/config.rb, line 140
def to_array(*keys)
    update_config unless current?
    if keys.length > 0
        @data.select do |k, v|
                 keys.include? k
             end.map { |k, v| value_of(v) }
    else
        @data.map { |k, v| value_of(v) }
    end
end
to_hash(*keys) click to toggle source
# File lib/ncc/config.rb, line 125
def to_hash(*keys)
    update_config unless current?
    if keys.length > 0
        Hash[
        @data.select do |k, v|
                 keys.include? k
             end.map { |k, v| [k, value_of(v)] }
                            ]
    else
        Hash[
        @data.map { |k, v| [k, value_of(v)] }
                              ]
    end
end
to_json() click to toggle source
# File lib/ncc/config.rb, line 159
def to_json
    self.to_hash.to_json
end
to_s() click to toggle source
# File lib/ncc/config.rb, line 121
def to_s
    "#<#{me} #{@data.inspect}>"
end
update_config(tolerant=false) click to toggle source
# File lib/ncc/config.rb, line 58
def update_config(tolerant=false)
    debug "updating config"
    if File.directory? @file
        debug "#{@file} is a directory"
        @data ||= { }
        data = { }
        Dir.open(@file) do |dirh|
            @mtime = File.stat(@file).mtime
            debug "storing mtime: #{@mtime}"
            dirh.each do |entry|
                debug "considering #{entry}"
                next if entry[0] == "."[0]
                if File.directory? File.join(@file, entry) or
                        m = /(.*)\.conf$/.match(entry)
                    debug "#{entry} is further configuration"
                    key = m.nil? ? entry.intern : m[1]
                    if @data.has_key? key
                        data[key] = @data[key]
                    else
                        data[key] =
                            NCC::Config.new(File.join(@file, entry),
                                        @opt)
                    end
                end
            end
        end
        @data = data
    else
        debug "#{@file} is not a directory"
        begin
            File.open(@file, 'r') do |fh|
                @mtime = fh.stat.mtime
                @data = JSON.load(fh)
            end
        rescue Errno::ENOENT
            @mtime = Time.now
            @data = { }
        rescue JSON::ParserError => err
            do_warn "Error parsing JSON in #{@file}, not updating config"
        end
    end
end
value_of(v) click to toggle source
# File lib/ncc/config.rb, line 151
def value_of(v)
    if v.respond_to? :to_hash
        v.to_hash
    else
        v
    end
end