Permalink
Browse files

Over engineered a bit on the thread business :)

  • Loading branch information...
winton committed Jan 25, 2011
1 parent c710d44 commit 8a61167dfabb0ea0d060f3eae1470f746b2c1939
Showing with 63 additions and 40 deletions.
  1. +15 −17 README.md
  2. +2 −2 config/gemspec.yml
  3. +19 −16 lib/timed_config.rb
  4. +0 −1 spec/fixtures/config.yml
  5. +5 −0 spec/spec_helper.rb
  6. +22 −4 spec/timed_config_spec.rb
View
@@ -1,7 +1,7 @@
TimedConfig
===========
-Starts a thread that loads a YAML config every X minutes
+Load a YAML config every X minutes
Requirements
------------
@@ -18,39 +18,30 @@ Install
#### config/environment.rb
<pre>
-config.gem 'acts_as_archive'
+config.gem 'timed_config'
</pre>
### Rails 3
#### Gemfile
<pre>
-gem 'acts_as_archive'
+gem 'timed_config'
</pre>
### Other
<pre>
-require 'acts_as_archive'
+require 'timed_config'
</pre>
-Starts by default
------------------
-
-<code>TimedConfig</code> will start automatically when you require it.
+Defaults
+--------
-If you are using Rails, it will try to find the config in <code>config/timed_config.yml</code>.
+If you are using Rails, <code>TimedConfig</code> will try to locate the YAML config at <code>config/timed_config.yml</code>.
By default, the refresh period is set to 1 minute.
-Accessing the config
---------------------
-
-<pre>
-TimedConfig.config
-</pre>
-
Changing defaults
-----------------
@@ -59,4 +50,11 @@ TimedConfig.period = 120 # change period to two minutes
TimedConfig.path = "path/to/yaml.yml"
</pre>
-The config will reload any time one of these settings change.
+The config will reload any time a setting changes.
+
+Accessing the config
+--------------------
+
+<pre>
+TimedConfig.config
+</pre>
View
@@ -4,7 +4,7 @@ authors:
- Winton Welsh
email: mail@wintoni.us
homepage: http://github.com/winton/timed_config
-summary: Starts a thread that loads a YAML config every X minutes
-description: Starts a thread that loads a YAML config every X minutes
+summary: Load a YAML config every X minutes
+description: Load a YAML config every X minutes
dependencies: null
development_dependencies: null
View
@@ -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
View
@@ -1 +0,0 @@
-test: test
View
@@ -8,4 +8,9 @@
require "#{$root}/lib/timed_config"
Spec::Runner.configure do |config|
+end
+
+def write_to_fixture(data)
+ config = "#{$root}/spec/fixtures/config.yml"
+ File.open(config, 'w') {|f| f.write(data) }
end
View
@@ -1,17 +1,35 @@
require 'spec_helper'
describe TimedConfig do
- it "should be sleeping" do
- TimedConfig.thread.status.should == 'sleep'
+
+ after(:all) do
+ FileUtils.rm "#{$root}/spec/fixtures/config.yml"
end
it "should not have set a config" do
TimedConfig.config.should == nil
end
- it "should set a config when path set" do
+ it "should update config when path set" do
+ write_to_fixture "test: test"
TimedConfig.path = "#{$root}/spec/fixtures/config.yml"
- sleep 0.1
TimedConfig.config.should == { 'test' => 'test' }
end
+
+ it "should update config when period set" do
+ write_to_fixture "test2: test2"
+ TimedConfig.period = 2
+ TimedConfig.config.should == { 'test2' => 'test2' }
+ end
+
+ it "should not update config in one second" do
+ write_to_fixture "test3: test3"
+ sleep 1
+ TimedConfig.config.should == { 'test2' => 'test2' }
+ end
+
+ it "should update config in two seconds" do
+ sleep 1
+ TimedConfig.config.should == { 'test3' => 'test3' }
+ end
end

0 comments on commit 8a61167

Please sign in to comment.