timed_config 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  TimedConfig
2
2
  ===========
3
3
 
4
- Starts a thread that loads a YAML config every X minutes
4
+ Load a YAML config every X minutes
5
5
 
6
6
  Requirements
7
7
  ------------
@@ -18,7 +18,7 @@ Install
18
18
  #### config/environment.rb
19
19
 
20
20
  <pre>
21
- config.gem 'acts_as_archive'
21
+ config.gem 'timed_config'
22
22
  </pre>
23
23
 
24
24
  ### Rails 3
@@ -26,31 +26,22 @@ config.gem 'acts_as_archive'
26
26
  #### Gemfile
27
27
 
28
28
  <pre>
29
- gem 'acts_as_archive'
29
+ gem 'timed_config'
30
30
  </pre>
31
31
 
32
32
  ### Other
33
33
 
34
34
  <pre>
35
- require 'acts_as_archive'
35
+ require 'timed_config'
36
36
  </pre>
37
37
 
38
- Starts by default
39
- -----------------
40
-
41
- <code>TimedConfig</code> will start automatically when you require it.
38
+ Defaults
39
+ --------
42
40
 
43
- If you are using Rails, it will try to find the config in <code>config/timed_config.yml</code>.
41
+ If you are using Rails, <code>TimedConfig</code> will try to locate the YAML config at <code>config/timed_config.yml</code>.
44
42
 
45
43
  By default, the refresh period is set to 1 minute.
46
44
 
47
- Accessing the config
48
- --------------------
49
-
50
- <pre>
51
- TimedConfig.config
52
- </pre>
53
-
54
45
  Changing defaults
55
46
  -----------------
56
47
 
@@ -59,4 +50,11 @@ TimedConfig.period = 120 # change period to two minutes
59
50
  TimedConfig.path = "path/to/yaml.yml"
60
51
  </pre>
61
52
 
62
- The config will reload any time one of these settings change.
53
+ The config will reload any time a setting changes.
54
+
55
+ Accessing the config
56
+ --------------------
57
+
58
+ <pre>
59
+ TimedConfig.config
60
+ </pre>
data/config/gemspec.yml CHANGED
@@ -1,10 +1,10 @@
1
1
  name: timed_config
2
- version: 0.1.0
2
+ version: 0.1.1
3
3
  authors:
4
4
  - Winton Welsh
5
5
  email: mail@wintoni.us
6
6
  homepage: http://github.com/winton/timed_config
7
- summary: Starts a thread that loads a YAML config every X minutes
8
- description: Starts a thread that loads a YAML config every X minutes
7
+ summary: Load a YAML config every X minutes
8
+ description: Load a YAML config every X minutes
9
9
  dependencies: null
10
10
  development_dependencies: null
data/lib/timed_config.rb CHANGED
@@ -1,41 +1,44 @@
1
1
  require 'yaml'
2
2
 
3
3
  module TimedConfig
4
-
5
4
  class <<self
6
- attr_accessor :config
5
+
6
+ def config
7
+ reload
8
+ end
7
9
 
8
10
  def path
9
11
  @path || (defined?(Rails) ? "#{Rails.root}/config/timed_config.yml" : nil)
10
12
  end
11
13
 
12
14
  def path=(p)
13
- thread.stop rescue nil
14
15
  @path = p
15
- thread.run
16
+ reload true
17
+ p
16
18
  end
17
19
 
18
20
  def period
19
21
  @period || 60
20
22
  end
21
23
 
22
- def period=(seconds)
23
- thread.stop rescue nil
24
- @period = seconds
25
- thread.run
24
+ def period=(p)
25
+ @period = p
26
+ reload true
27
+ p
26
28
  end
27
29
 
28
- def thread
29
- @thread ||= Thread.new do
30
- while true
31
- if TimedConfig.path && File.exists?(TimedConfig.path)
32
- TimedConfig.config = YAML::load(File.open(TimedConfig.path))
33
- end
34
- sleep TimedConfig.period
30
+ def reload(force=false)
31
+ if force || !@last_load || Time.now >= (@last_load + period)
32
+ if TimedConfig.path && File.exists?(TimedConfig.path)
33
+ @config = YAML::load(File.open(TimedConfig.path))
34
+ else
35
+ @config = nil
35
36
  end
37
+ @last_load = Time.now
36
38
  end
39
+ @config
37
40
  end
38
41
 
39
- TimedConfig.thread
42
+ TimedConfig.reload
40
43
  end
41
44
  end
data/spec/spec_helper.rb CHANGED
@@ -8,4 +8,9 @@ TimedConfig::Gems.activate :rspec
8
8
  require "#{$root}/lib/timed_config"
9
9
 
10
10
  Spec::Runner.configure do |config|
11
+ end
12
+
13
+ def write_to_fixture(data)
14
+ config = "#{$root}/spec/fixtures/config.yml"
15
+ File.open(config, 'w') {|f| f.write(data) }
11
16
  end
@@ -1,17 +1,35 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe TimedConfig do
4
- it "should be sleeping" do
5
- TimedConfig.thread.status.should == 'sleep'
4
+
5
+ after(:all) do
6
+ FileUtils.rm "#{$root}/spec/fixtures/config.yml"
6
7
  end
7
8
 
8
9
  it "should not have set a config" do
9
10
  TimedConfig.config.should == nil
10
11
  end
11
12
 
12
- it "should set a config when path set" do
13
+ it "should update config when path set" do
14
+ write_to_fixture "test: test"
13
15
  TimedConfig.path = "#{$root}/spec/fixtures/config.yml"
14
- sleep 0.1
15
16
  TimedConfig.config.should == { 'test' => 'test' }
16
17
  end
18
+
19
+ it "should update config when period set" do
20
+ write_to_fixture "test2: test2"
21
+ TimedConfig.period = 2
22
+ TimedConfig.config.should == { 'test2' => 'test2' }
23
+ end
24
+
25
+ it "should not update config in one second" do
26
+ write_to_fixture "test3: test3"
27
+ sleep 1
28
+ TimedConfig.config.should == { 'test2' => 'test2' }
29
+ end
30
+
31
+ it "should update config in two seconds" do
32
+ sleep 1
33
+ TimedConfig.config.should == { 'test3' => 'test3' }
34
+ end
17
35
  end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timed_config
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
4
+ hash: 25
5
+ prerelease:
5
6
  segments:
6
7
  - 0
7
8
  - 1
8
- - 0
9
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
10
11
  platform: ruby
11
12
  authors:
12
13
  - Winton Welsh
@@ -14,11 +15,11 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2011-01-13 00:00:00 -08:00
18
+ date: 2011-01-24 00:00:00 -08:00
18
19
  default_executable:
19
20
  dependencies: []
20
21
 
21
- description: Starts a thread that loads a YAML config every X minutes
22
+ description: Load a YAML config every X minutes
22
23
  email: mail@wintoni.us
23
24
  executables: []
24
25
 
@@ -37,7 +38,6 @@ files:
37
38
  - lib/timed_config.rb
38
39
  - lib/timed_config/gems.rb
39
40
  - rails/init.rb
40
- - spec/fixtures/config.yml
41
41
  - spec/fixtures/gemsets.yml
42
42
  - spec/fixtures/gemspec.yml
43
43
  - spec/spec_helper.rb
@@ -58,6 +58,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
+ hash: 3
61
62
  segments:
62
63
  - 0
63
64
  version: "0"
@@ -66,18 +67,18 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
67
  requirements:
67
68
  - - ">="
68
69
  - !ruby/object:Gem::Version
70
+ hash: 3
69
71
  segments:
70
72
  - 0
71
73
  version: "0"
72
74
  requirements: []
73
75
 
74
76
  rubyforge_project:
75
- rubygems_version: 1.3.7
77
+ rubygems_version: 1.4.2
76
78
  signing_key:
77
79
  specification_version: 3
78
- summary: Starts a thread that loads a YAML config every X minutes
80
+ summary: Load a YAML config every X minutes
79
81
  test_files:
80
- - spec/fixtures/config.yml
81
82
  - spec/fixtures/gemsets.yml
82
83
  - spec/fixtures/gemspec.yml
83
84
  - spec/spec_helper.rb
@@ -1 +0,0 @@
1
- test: test