Class: Bogo::Config
- Inherits:
-
Object
- Object
- Bogo::Config
- Extended by:
- Memoization, Forwardable
- Includes:
- Lazy
- Defined in:
- lib/bogo/config.rb,
lib/bogo/config/version.rb
Defined Under Namespace
Classes: FileLoadError
Constant Summary collapse
- VERSION =
Current library version
Gem::Version.new('0.3.1')
Instance Attribute Summary collapse
- #initial ⇒ String, Hash readonly
-
#path ⇒ String
readonly
Configuration path.
Class Method Summary collapse
-
.reload! ⇒ TrueClass
Reload any registered ‘Bogo::Config` instances.
-
.reloadable(config) ⇒ TrueClass
Register config instance to auto reload on HUP.
-
.reloader ⇒ Thread
Internal reloader.
Instance Method Summary collapse
-
#dirty ⇒ Smash
Override to force consistent data access (removes dirty functionality).
- #eval_disabled? ⇒ TrueClass, FalseClass
- #eval_enabled? ⇒ TrueClass, FalseClass
-
#extract_error_for(path, errors) ⇒ Exception, NilClass
Extract appropriate execption based on path.
-
#immutable! ⇒ self
Freeze underlying configuration data.
-
#init! ⇒ self
Intialize the configuration.
-
#initialize(path_or_hash = nil) ⇒ self
constructor
Create new instance.
-
#json_load(file_path) ⇒ Smash
Read and parse JSON file.
-
#load! ⇒ self
Load configuration from file(s).
-
#load_file(file_path) ⇒ Smash
Load configuration file.
-
#reloadable! ⇒ TrueClass
Enables automatic reloading on SIGHUP.
-
#struct_load(file_path) ⇒ Smash
Read and parse AttributeStruct file.
- #to_json(*args) ⇒ String
-
#xml_format(result) ⇒ Smash
Format XML types.
-
#xml_format_value(value) ⇒ Object
Format XML value types.
-
#xml_load(file_path) ⇒ Smash
Read and parse XML file.
-
#yaml_load(file_path) ⇒ Smash
Read and parse YAML file.
Constructor Details
#initialize(path_or_hash = nil) ⇒ self
Create new instance
113 114 115 116 117 118 |
# File 'lib/bogo/config.rb', line 113 def initialize(path_or_hash=nil) super() @initial = path_or_hash @data = Smash.new init! end |
Instance Attribute Details
#initial ⇒ String, Hash (readonly)
107 108 109 |
# File 'lib/bogo/config.rb', line 107 def initial @initial end |
#path ⇒ String (readonly)
Returns configuration path.
105 106 107 |
# File 'lib/bogo/config.rb', line 105 def path @path end |
Class Method Details
.reload! ⇒ TrueClass
Reload any registered ‘Bogo::Config` instances
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/bogo/config.rb', line 40 def reload! obj_ids = memoize(:bogo_reloadable_configs, :global) objects = Thread.exclusive do ObjectSpace.each_object.find_all do |obj| obj_ids.include?(obj.object_id) end end objects.map(&:init!) memoize(:bogo_reloadable_configs, :global).delete_if do |oid| !obj_ids.include?(oid) end true end |
.reloadable(config) ⇒ TrueClass
Register config instance to auto reload on HUP
58 59 60 61 62 63 64 65 66 |
# File 'lib/bogo/config.rb', line 58 def reloadable(config) if(config.is_a?(Bogo::Config)) reloader memoize(:bogo_reloadable_configs, :global){ [] }.push(config.object_id).uniq! else raise TypeError.new "Expecting type `Bogo::Config`. Received: `#{config.class}`" end true end |
.reloader ⇒ Thread
Internal reloader
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 |
# File 'lib/bogo/config.rb', line 71 def reloader memoize(:bogo_config_reloader, :global) do Thread.new do begin loop do begin sleep rescue SignalException => e if(e.signm == 'SIGHUP') if(ENV['BOGO_DEBUG']) $stdout.puts 'SIGHUP encountered. Reloading `Bogo::Config` instances.' end Bogo::Config.reload! else raise end end end rescue => e if(ENV['BOGO_DEBUG']) $stderr.puts "#{e.class}: #{e}\n#{e.backtrace.join("\n")}" end retry end end end end |
Instance Method Details
#dirty ⇒ Smash
Override to force consistent data access (removes dirty functionality)
162 163 164 |
# File 'lib/bogo/config.rb', line 162 def dirty data end |
#eval_disabled? ⇒ TrueClass, FalseClass
313 314 315 |
# File 'lib/bogo/config.rb', line 313 def eval_disabled? !eval_enabled? end |
#eval_enabled? ⇒ TrueClass, FalseClass
308 309 310 |
# File 'lib/bogo/config.rb', line 308 def eval_enabled? ENV['BOGO_CONFIG_DISABLE_EVAL'].to_s.downcase != 'true' end |
#extract_error_for(path, errors) ⇒ Exception, NilClass
Extract appropriate execption based on path
322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/bogo/config.rb', line 322 def extract_error_for(path, errors) content = File.read(path).strip if(content.start_with?('{')) errors[:json_load] elsif(content.start_with?('<')) errors[:xml_load] elsif(content.match(/\.new\s*(do|\{)/)) errors[:struct_load] else errors[:yaml_load] end end |
#immutable! ⇒ self
Freeze underlying configuration data
131 132 133 |
# File 'lib/bogo/config.rb', line 131 def immutable! @data = data.to_smash(:freeze) end |
#init! ⇒ self
Intialize the configuration
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/bogo/config.rb', line 138 def init! if(initial.is_a?(String)) @path = initial.dup hash = load! else hash = initial end if(hash) is_immutable = data.frozen? # TODO: synchronize here load_data(hash) @data = hash.to_smash.deep_merge(data.to_smash) @data.to_smash(:freeze) if is_immutable end self end |
#json_load(file_path) ⇒ Smash
Read and parse JSON file
244 245 246 |
# File 'lib/bogo/config.rb', line 244 def json_load(file_path) MultiJson.load(File.read(file_path)).to_smash end |
#load! ⇒ self
Load configuration from file(s)
174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/bogo/config.rb', line 174 def load! if(path) if(File.directory?(path)) conf = Dir.glob(File.join(path, '*')).sort.inject(Smash.new) do |memo, file_path| memo.deep_merge(load_file(file_path)) end elsif(File.file?(path)) conf = load_file(path) else raise Errno::ENOENT.new path end conf end end |
#load_file(file_path) ⇒ Smash
Load configuration file
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/bogo/config.rb', line 193 def load_file(file_path) result = nil errors = Smash.new error = nil begin result = case File.extname(file_path) when '.yaml', '.yml' yaml_load(file_path) when '.json' json_load(file_path) when '.xml' xml_load(file_path) when '.rb' struct_load(file_path) else [:struct_load, :json_load, :yaml_load, :xml_load].each do |loader| begin result = send(loader, file_path) break rescue StandardError, ScriptError => e errors[loader] = e if(ENV['BOGO_DEBUG']) $stderr.puts "#{e.class}: #{e}\n#{e.backtrace.join("\n")}" end end end result end rescue => error end unless(result) raise FileLoadError.new( "Failed to load configuration from file (#{file_path})", error || extract_error_for(file_path, errors) ) end result end |
#reloadable! ⇒ TrueClass
Enables automatic reloading on SIGHUP
123 124 125 126 |
# File 'lib/bogo/config.rb', line 123 def reloadable! Bogo::Config.reloadable(self) true end |
#struct_load(file_path) ⇒ Smash
Read and parse AttributeStruct file
296 297 298 299 300 301 302 303 304 305 |
# File 'lib/bogo/config.rb', line 296 def struct_load(file_path) if(eval_disabled?) raise 'Ruby based configuration evaluation is currently disabled!' else result = Module.new.instance_eval( IO.read(file_path), file_path, 1 ) result._dump.to_smash end end |
#to_json(*args) ⇒ String
167 168 169 |
# File 'lib/bogo/config.rb', line 167 def to_json(*args) MultiJson.dump(data, *args) end |
#xml_format(result) ⇒ Smash
Format XML types
262 263 264 |
# File 'lib/bogo/config.rb', line 262 def xml_format(result) Smash[result.map{|k,v| [k, xml_format_value(v)]}] end |
#xml_format_value(value) ⇒ Object
Format XML value types
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/bogo/config.rb', line 270 def xml_format_value(value) case value when Hash xml_format(value) when Array value.map{|v| xml_format_value(v)} else value.strip! if(value == 'true') true elsif(value == 'false') false elsif(value.to_i.to_s == value) value.to_i elsif(value.to_f.to_s == value) value.to_f else value end end end |
#xml_load(file_path) ⇒ Smash
supar ENTERPRISE
Read and parse XML file
253 254 255 256 |
# File 'lib/bogo/config.rb', line 253 def xml_load(file_path) result = MultiXml.parse(File.read(file_path)).to_smash[:configuration] xml_format(result) end |
#yaml_load(file_path) ⇒ Smash
Read and parse YAML file
236 237 238 |
# File 'lib/bogo/config.rb', line 236 def yaml_load(file_path) YAML.load(File.read(file_path)).to_smash end |