lib/timed_config.rb in timed_config-0.1.0 vs lib/timed_config.rb in timed_config-0.1.1

- old
+ new

@@ -1,41 +1,44 @@ require 'yaml' module TimedConfig - class <<self - attr_accessor :config + def config + reload + end + def path @path || (defined?(Rails) ? "#{Rails.root}/config/timed_config.yml" : nil) end def path=(p) - thread.stop rescue nil @path = p - thread.run + reload true + p end def period @period || 60 end - def period=(seconds) - thread.stop rescue nil - @period = seconds - thread.run + def period=(p) + @period = p + reload true + p end - def thread - @thread ||= Thread.new do - while true - if TimedConfig.path && File.exists?(TimedConfig.path) - TimedConfig.config = YAML::load(File.open(TimedConfig.path)) - end - sleep TimedConfig.period + def reload(force=false) + if force || !@last_load || Time.now >= (@last_load + period) + if TimedConfig.path && File.exists?(TimedConfig.path) + @config = YAML::load(File.open(TimedConfig.path)) + else + @config = nil end + @last_load = Time.now end + @config end - TimedConfig.thread + TimedConfig.reload end end \ No newline at end of file