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
174 175 176 177 |
# File 'lib/ncc/config.rb', line 174 def [](key) update_config unless current? @data[key] end |
#[]=(key, value) ⇒ Object
179 180 181 182 183 |
# File 'lib/ncc/config.rb', line 179 def []=(key, value) update_config unless current? @mtime = Time.now @data[key] = value end |
#current? ⇒ Boolean
159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/ncc/config.rb', line 159 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 |
# File 'lib/ncc/config.rb', line 107 def debug(msg) if opt(:logger) and opt(:logger).respond_to? :debug opt(:logger).debug "#<#{me}>: #{msg}" end end |
#delete(k) ⇒ Object
202 203 204 205 |
# File 'lib/ncc/config.rb', line 202 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
195 196 197 198 199 200 |
# File 'lib/ncc/config.rb', line 195 def each update_config unless current? @data.each do |k, v| yield k, v end end |
#has_key?(key) ⇒ Boolean
185 186 187 188 |
# File 'lib/ncc/config.rb', line 185 def has_key?(key) update_config unless current? @data.has_key? key end |
#keys ⇒ Object
190 191 192 193 |
# File 'lib/ncc/config.rb', line 190 def keys update_config unless current? @data.keys end |
#me ⇒ Object
113 114 115 |
# File 'lib/ncc/config.rb', line 113 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
136 137 138 139 140 141 142 143 144 145 |
# File 'lib/ncc/config.rb', line 136 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
121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/ncc/config.rb', line 121 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
155 156 157 |
# File 'lib/ncc/config.rb', line 155 def to_json self.to_hash.to_json end |
#to_s ⇒ Object
117 118 119 |
# File 'lib/ncc/config.rb', line 117 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
147 148 149 150 151 152 153 |
# File 'lib/ncc/config.rb', line 147 def value_of(v) if v.respond_to? :to_hash v.to_hash else v end end |