Class: NCC::Config
Constant Summary collapse
- Infinite =
+1.0/0.0
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Not really math, per se, but Lewis Carroll would have liked it -jbrinkley/20130403.
-
#mtime ⇒ Object
readonly
Not really math, per se, but Lewis Carroll would have liked it -jbrinkley/20130403.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #current? ⇒ Boolean
- #debug(msg) ⇒ Object
- #delete(k) ⇒ Object
- #do_warn(msg) ⇒ Object
- #each ⇒ Object
- #has_key?(key) ⇒ Boolean
-
#initialize(source = ["/etc/ncc-api", "#{ENV['NCCAPI_HOME']}/etc"], opt = {}) ⇒ Config
constructor
A new instance of Config.
- #keys ⇒ Object
- #me ⇒ Object
- #opt(optname) ⇒ Object
- #to_array(*keys) ⇒ Object
- #to_hash(*keys) ⇒ Object
- #to_json ⇒ Object
- #to_s ⇒ Object
- #update_config(tolerant = false) ⇒ Object
- #value_of(v) ⇒ Object
Constructor Details
#initialize(source = ["/etc/ncc-api", "#{ENV['NCCAPI_HOME']}/etc"], opt = {}) ⇒ Config
Returns a new instance of Config.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# 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 |
Instance Attribute Details
#file ⇒ Object (readonly)
Not really math, per se, but Lewis Carroll would have liked it -jbrinkley/20130403
33 34 35 |
# File 'lib/ncc/config.rb', line 33 def file @file end |
#mtime ⇒ Object (readonly)
Not really math, per se, but Lewis Carroll would have liked it -jbrinkley/20130403
33 34 35 |
# File 'lib/ncc/config.rb', line 33 def mtime @mtime end |
Instance Method Details
#[](key) ⇒ Object
178 179 180 181 |
# File 'lib/ncc/config.rb', line 178 def [](key) update_config unless current? @data[key] end |
#[]=(key, value) ⇒ Object
183 184 185 186 187 |
# File 'lib/ncc/config.rb', line 183 def []=(key, value) update_config unless current? @mtime = Time.now @data[key] = value end |
#current? ⇒ Boolean
163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# 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) ⇒ Object
107 108 109 110 111 112 113 114 115 |
# 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) ⇒ Object
206 207 208 209 |
# File 'lib/ncc/config.rb', line 206 def delete(k) update_config unless current? @data.delete(k) end |
#do_warn(msg) ⇒ Object
101 102 103 104 105 |
# File 'lib/ncc/config.rb', line 101 def do_warn(msg) if opt :logger opt(:logger).warn msg end end |
#each ⇒ Object
199 200 201 202 203 204 |
# 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) ⇒ Boolean
189 190 191 192 |
# File 'lib/ncc/config.rb', line 189 def has_key?(key) update_config unless current? @data.has_key? key end |
#keys ⇒ Object
194 195 196 197 |
# File 'lib/ncc/config.rb', line 194 def keys update_config unless current? @data.keys end |
#me ⇒ Object
117 118 119 |
# File 'lib/ncc/config.rb', line 117 def me "#{self.class}:#{@file}" end |
#opt(optname) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/ncc/config.rb', line 50 def opt(optname) if @opt.has_key? optname @opt[optname] else nil end end |
#to_array(*keys) ⇒ Object
140 141 142 143 144 145 146 147 148 149 |
# 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) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# 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 ⇒ Object
159 160 161 |
# File 'lib/ncc/config.rb', line 159 def to_json self.to_hash.to_json end |
#to_s ⇒ Object
121 122 123 |
# File 'lib/ncc/config.rb', line 121 def to_s "#<#{me} #{@data.inspect}>" end |
#update_config(tolerant = false) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# 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) ⇒ Object
151 152 153 154 155 156 157 |
# File 'lib/ncc/config.rb', line 151 def value_of(v) if v.respond_to? :to_hash v.to_hash else v end end |