Browse files

First commit

  • Loading branch information...
1 parent d48370b commit c710d44e46010d65bfe9766dfab8f09faf0252df @winton committed Jan 13, 2011
View
2 LICENSE
@@ -15,4 +15,4 @@ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
60 README.md
@@ -1,36 +1,62 @@
-GemTemplate
+TimedConfig
===========
-A gem template for new projects.
+Starts a thread that loads a YAML config every X minutes
Requirements
------------
<pre>
-gem install stencil
+gem install timed_config
</pre>
-Setup the template
-------------------
+Install
+-------
-You only have to do this once.
+### Rails 2
+
+#### config/environment.rb
<pre>
-git clone git@github.com:winton/gem_template.git
-cd gem_template
-stencil
+config.gem 'acts_as_archive'
</pre>
-Setup a new project
--------------------
+### Rails 3
+
+#### Gemfile
+
+<pre>
+gem 'acts_as_archive'
+</pre>
+
+### Other
+
+<pre>
+require 'acts_as_archive'
+</pre>
+
+Starts by default
+-----------------
+
+<code>TimedConfig</code> will start automatically when you require it.
+
+If you are using Rails, it will try to find the config in <code>config/timed_config.yml</code>.
+
+By default, the refresh period is set to 1 minute.
+
+Accessing the config
+--------------------
+
+<pre>
+TimedConfig.config
+</pre>
-Do this for every new project.
+Changing defaults
+-----------------
<pre>
-mkdir my_project
-git init
-stencil gem_template
-rake rename
+TimedConfig.period = 120 # change period to two minutes
+TimedConfig.path = "path/to/yaml.yml"
</pre>
-The last command does a find-replace (gem\_template -> my\_project) on files and filenames.
+The config will reload any time one of these settings change.
View
18 Rakefile
@@ -1,13 +1,13 @@
-require File.dirname(__FILE__) + '/lib/gem_template/gems'
+require File.dirname(__FILE__) + '/lib/timed_config/gems'
-GemTemplate::Gems.activate %w(rake rspec)
+TimedConfig::Gems.activate %w(rake rspec)
require 'rake'
require 'spec/rake/spectask'
def gemspec
@gemspec ||= begin
- file = File.expand_path('../gem_template.gemspec', __FILE__)
+ file = File.expand_path('../timed_config.gemspec', __FILE__)
eval(File.read(file), binding, file)
end
end
@@ -29,9 +29,9 @@ task :gem do
root = File.expand_path('../', __FILE__)
pkg = "#{root}/pkg"
system "rm -Rf #{pkg}"
- GemTemplate::Gems.gemset_names.each do |gemset|
+ TimedConfig::Gems.gemset_names.each do |gemset|
ENV['GEMSET'] = gemset.to_s
- system "cd #{root} && gem build gem_template.gemspec"
+ system "cd #{root} && gem build timed_config.gemspec"
system "mkdir -p #{pkg} && mv *.gem pkg"
end
ENV['GEMSET'] = old_gemset
@@ -63,17 +63,17 @@ namespace :gems do
gemset = ENV['GEMSET']
sudo = ENV['SUDO'] == '1' ? 'sudo' : ''
- GemTemplate::Gems.gemset = gemset if gemset
+ TimedConfig::Gems.gemset = gemset if gemset
if dev
- gems = GemTemplate::Gems.gemspec.development_dependencies
+ gems = TimedConfig::Gems.gemspec.development_dependencies
else
- gems = GemTemplate::Gems.gemspec.dependencies
+ gems = TimedConfig::Gems.gemspec.dependencies
end
gems.each do |name|
name = name.to_s
- version = GemTemplate::Gems.versions[name]
+ version = TimedConfig::Gems.versions[name]
if Gem.source_index.find_name(name, version).empty?
version = version ? "-v #{version}" : ''
system "#{sudo} gem install #{name} #{version} #{docs}"
View
3 bin/gem_template
@@ -1,3 +0,0 @@
-#!/usr/bin/env ruby
-
-require File.expand_path(File.dirname(__FILE__) + "/../lib/gem_template")
View
6 config/database.yml
@@ -1,6 +0,0 @@
-development:
- adapter: mysql
- database: gem_template
- username: root
- password:
- host: localhost
View
2 config/gemsets.yml
@@ -1,3 +1,3 @@
-gem_template:
+timed_config:
rake: >=0.8.7
rspec: ~>1.0
View
19 config/gemspec.yml
@@ -1,13 +1,10 @@
-name: gem_template
+name: timed_config
version: 0.1.0
authors:
- -
-email:
-homepage:
-summary:
-description:
-dependencies:
- -
-development_dependencies:
- - rake
- - rspec
+ - 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
+dependencies: null
+development_dependencies: null
View
8 lib/gem_template.rb
@@ -1,8 +0,0 @@
-require File.dirname(__FILE__) + '/gem_template/gems'
-
-GemTemplate::Gems.activate %w()
-
-$:.unshift File.dirname(__FILE__)
-
-module GemTemplate
-end
View
41 lib/timed_config.rb
@@ -0,0 +1,41 @@
+require 'yaml'
+
+module TimedConfig
+
+ class <<self
+ attr_accessor :config
+
+ def path
+ @path || (defined?(Rails) ? "#{Rails.root}/config/timed_config.yml" : nil)
+ end
+
+ def path=(p)
+ thread.stop rescue nil
+ @path = p
+ thread.run
+ end
+
+ def period
+ @period || 60
+ end
+
+ def period=(seconds)
+ thread.stop rescue nil
+ @period = seconds
+ thread.run
+ 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
+ end
+ end
+ end
+
+ TimedConfig.thread
+ end
+end
View
4 lib/gem_template/gems.rb → lib/timed_config/gems.rb
@@ -1,8 +1,8 @@
-unless defined?(GemTemplate::Gems)
+unless defined?(TimedConfig::Gems)
require 'yaml'
- module GemTemplate
+ module TimedConfig
module Gems
class <<self
View
2 rails/init.rb
@@ -1 +1 @@
-require File.expand_path(File.dirname(__FILE__) + '/../lib/gem_template')
+require File.expand_path(File.dirname(__FILE__) + '/../lib/timed_config')
View
1 spec/fixtures/config.yml
@@ -0,0 +1 @@
+test: test
View
4 spec/gem_template_spec.rb
@@ -1,4 +0,0 @@
-require 'spec_helper'
-
-describe GemTemplate do
-end
View
6 spec/spec_helper.rb
@@ -1,11 +1,11 @@
require 'pp'
$root = File.expand_path('../../', __FILE__)
-require "#{$root}/lib/gem_template/gems"
+require "#{$root}/lib/timed_config/gems"
-GemTemplate::Gems.activate :rspec
+TimedConfig::Gems.activate :rspec
-require "#{$root}/lib/gem_template"
+require "#{$root}/lib/timed_config"
Spec::Runner.configure do |config|
end
View
106 spec/gem_template/gems_spec.rb → spec/timed_config/gems_spec.rb
@@ -1,36 +1,36 @@
require 'spec_helper'
-describe GemTemplate::Gems do
+describe TimedConfig::Gems do
before(:each) do
- @old_config = GemTemplate::Gems.config
+ @old_config = TimedConfig::Gems.config
- GemTemplate::Gems.config.gemspec = "#{$root}/spec/fixtures/gemspec.yml"
- GemTemplate::Gems.config.gemsets = [
+ TimedConfig::Gems.config.gemspec = "#{$root}/spec/fixtures/gemspec.yml"
+ TimedConfig::Gems.config.gemsets = [
"#{$root}/spec/fixtures/gemsets.yml"
]
- GemTemplate::Gems.config.warn = true
+ TimedConfig::Gems.config.warn = true
- GemTemplate::Gems.gemspec true
- GemTemplate::Gems.gemset = nil
+ TimedConfig::Gems.gemspec true
+ TimedConfig::Gems.gemset = nil
end
after(:each) do
- GemTemplate::Gems.config = @old_config
+ TimedConfig::Gems.config = @old_config
end
describe :activate do
it "should activate gems" do
- GemTemplate::Gems.stub!(:gem)
- GemTemplate::Gems.should_receive(:gem).with('rspec', '=1.3.1')
- GemTemplate::Gems.should_receive(:gem).with('rake', '=0.8.7')
- GemTemplate::Gems.activate :rspec, 'rake'
+ TimedConfig::Gems.stub!(:gem)
+ TimedConfig::Gems.should_receive(:gem).with('rspec', '=1.3.1')
+ TimedConfig::Gems.should_receive(:gem).with('rake', '=0.8.7')
+ TimedConfig::Gems.activate :rspec, 'rake'
end
end
describe :gemset= do
before(:each) do
- GemTemplate::Gems.config.gemsets = [
+ TimedConfig::Gems.config.gemsets = [
{
:name => {
:rake => '>0.8.6',
@@ -45,15 +45,15 @@
describe :default do
before(:each) do
- GemTemplate::Gems.gemset = :default
+ TimedConfig::Gems.gemset = :default
end
it "should set @gemset" do
- GemTemplate::Gems.gemset.should == :default
+ TimedConfig::Gems.gemset.should == :default
end
it "should set @gemsets" do
- GemTemplate::Gems.gemsets.should == {
+ TimedConfig::Gems.gemsets.should == {
:name => {
:rake => ">0.8.6",
:default => {
@@ -71,7 +71,7 @@
end
it "should set Gems.versions" do
- GemTemplate::Gems.versions.should == {
+ TimedConfig::Gems.versions.should == {
:externals => "=1.0.2",
:mysql => "=2.8.1",
:rake => ">0.8.6",
@@ -80,26 +80,26 @@
end
it "should return proper values for Gems.dependencies" do
- GemTemplate::Gems.dependencies.should == [ :rake, :mysql ]
- GemTemplate::Gems.development_dependencies.should == []
+ TimedConfig::Gems.dependencies.should == [ :rake, :mysql ]
+ TimedConfig::Gems.development_dependencies.should == []
end
it "should return proper values for Gems.gemset_names" do
- GemTemplate::Gems.gemset_names.should == [ :default, :rspec2, :solo ]
+ TimedConfig::Gems.gemset_names.should == [ :default, :rspec2, :solo ]
end
end
describe :rspec2 do
before(:each) do
- GemTemplate::Gems.gemset = "rspec2"
+ TimedConfig::Gems.gemset = "rspec2"
end
it "should set @gemset" do
- GemTemplate::Gems.gemset.should == :rspec2
+ TimedConfig::Gems.gemset.should == :rspec2
end
it "should set @gemsets" do
- GemTemplate::Gems.gemsets.should == {
+ TimedConfig::Gems.gemsets.should == {
:name => {
:rake => ">0.8.6",
:default => {
@@ -117,34 +117,34 @@
end
it "should set Gems.versions" do
- GemTemplate::Gems.versions.should == {
+ TimedConfig::Gems.versions.should == {
:mysql2 => "=0.2.6",
:rake => ">0.8.6",
:rspec => "=2.3.0"
}
end
it "should return proper values for Gems.dependencies" do
- GemTemplate::Gems.dependencies.should == [ :rake, :mysql2 ]
- GemTemplate::Gems.development_dependencies.should == []
+ TimedConfig::Gems.dependencies.should == [ :rake, :mysql2 ]
+ TimedConfig::Gems.development_dependencies.should == []
end
it "should return proper values for Gems.gemset_names" do
- GemTemplate::Gems.gemset_names.should == [ :default, :rspec2, :solo ]
+ TimedConfig::Gems.gemset_names.should == [ :default, :rspec2, :solo ]
end
end
describe :solo do
before(:each) do
- GemTemplate::Gems.gemset = :solo
+ TimedConfig::Gems.gemset = :solo
end
it "should set @gemset" do
- GemTemplate::Gems.gemset.should == :solo
+ TimedConfig::Gems.gemset.should == :solo
end
it "should set @gemsets" do
- GemTemplate::Gems.gemsets.should == {
+ TimedConfig::Gems.gemsets.should == {
:name => {
:rake => ">0.8.6",
:default => {
@@ -162,28 +162,28 @@
end
it "should set Gems.versions" do
- GemTemplate::Gems.versions.should == {:rake=>">0.8.6"}
+ TimedConfig::Gems.versions.should == {:rake=>">0.8.6"}
end
it "should return proper values for Gems.dependencies" do
- GemTemplate::Gems.dependencies.should == [:rake]
- GemTemplate::Gems.development_dependencies.should == []
+ TimedConfig::Gems.dependencies.should == [:rake]
+ TimedConfig::Gems.development_dependencies.should == []
end
it "should return proper values for Gems.gemset_names" do
- GemTemplate::Gems.gemset_names.should == [ :default, :rspec2, :solo ]
+ TimedConfig::Gems.gemset_names.should == [ :default, :rspec2, :solo ]
end
end
describe :nil do
before(:each) do
- GemTemplate::Gems.gemset = nil
+ TimedConfig::Gems.gemset = nil
end
it "should set everything to nil" do
- GemTemplate::Gems.gemset.should == nil
- GemTemplate::Gems.gemsets.should == nil
- GemTemplate::Gems.versions.should == nil
+ TimedConfig::Gems.gemset.should == nil
+ TimedConfig::Gems.gemsets.should == nil
+ TimedConfig::Gems.versions.should == nil
end
end
end
@@ -195,18 +195,18 @@
it "should return the correct gemset for name gem" do
Gem.should_receive(:loaded_specs).and_return({ "name" => nil })
- GemTemplate::Gems.send(:gemset_from_loaded_specs).should == :default
+ TimedConfig::Gems.send(:gemset_from_loaded_specs).should == :default
end
it "should return the correct gemset for name-rspec gem" do
Gem.should_receive(:loaded_specs).and_return({ "name-rspec2" => nil })
- GemTemplate::Gems.send(:gemset_from_loaded_specs).should == :rspec2
+ TimedConfig::Gems.send(:gemset_from_loaded_specs).should == :rspec2
end
end
describe :reload_gemspec do
it "should populate @gemspec" do
- GemTemplate::Gems.gemspec.hash.should == {
+ TimedConfig::Gems.gemspec.hash.should == {
"name" => "name",
"version" => "0.1.0",
"authors" => ["Author"],
@@ -224,26 +224,26 @@
end
it "should create methods from keys of @gemspec" do
- GemTemplate::Gems.gemspec.name.should == "name"
- GemTemplate::Gems.gemspec.version.should == "0.1.0"
- GemTemplate::Gems.gemspec.authors.should == ["Author"]
- GemTemplate::Gems.gemspec.email.should == "email@email.com"
- GemTemplate::Gems.gemspec.homepage.should == "http://github.com/author/name"
- GemTemplate::Gems.gemspec.summary.should == "Summary"
- GemTemplate::Gems.gemspec.description.should == "Description"
- GemTemplate::Gems.gemspec.dependencies.should == [
+ TimedConfig::Gems.gemspec.name.should == "name"
+ TimedConfig::Gems.gemspec.version.should == "0.1.0"
+ TimedConfig::Gems.gemspec.authors.should == ["Author"]
+ TimedConfig::Gems.gemspec.email.should == "email@email.com"
+ TimedConfig::Gems.gemspec.homepage.should == "http://github.com/author/name"
+ TimedConfig::Gems.gemspec.summary.should == "Summary"
+ TimedConfig::Gems.gemspec.description.should == "Description"
+ TimedConfig::Gems.gemspec.dependencies.should == [
"rake",
{ "default" => ["mysql"] },
{ "rspec2" => [ "mysql2" ] }
]
- GemTemplate::Gems.gemspec.development_dependencies.should == nil
+ TimedConfig::Gems.gemspec.development_dependencies.should == nil
end
it "should produce a valid gemspec" do
- GemTemplate::Gems.gemset = :default
- gemspec = File.expand_path("../../../gem_template.gemspec", __FILE__)
+ TimedConfig::Gems.gemset = :default
+ gemspec = File.expand_path("../../../timed_config.gemspec", __FILE__)
gemspec = eval(File.read(gemspec), binding, gemspec)
gemspec.validate.should == true
end
end
-end
+end
View
17 spec/timed_config_spec.rb
@@ -0,0 +1,17 @@
+require 'spec_helper'
+
+describe TimedConfig do
+ it "should be sleeping" do
+ TimedConfig.thread.status.should == 'sleep'
+ end
+
+ it "should not have set a config" do
+ TimedConfig.config.should == nil
+ end
+
+ it "should set a config when path set" do
+ TimedConfig.path = "#{$root}/spec/fixtures/config.yml"
+ sleep 0.1
+ TimedConfig.config.should == { 'test' => 'test' }
+ end
+end
View
20 gem_template.gemspec → timed_config.gemspec
@@ -3,26 +3,26 @@ root = File.expand_path('../', __FILE__)
lib = "#{root}/lib"
$:.unshift lib unless $:.include?(lib)
-require 'gem_template/gems'
-GemTemplate::Gems.gemset ||= ENV['GEMSET'] || :default
+require 'timed_config/gems'
+TimedConfig::Gems.gemset ||= ENV['GEMSET'] || :default
Gem::Specification.new do |s|
- GemTemplate::Gems.gemspec.hash.each do |key, value|
- if key == 'name' && GemTemplate::Gems.gemset != :default
- s.name = "#{value}-#{GemTemplate::Gems.gemset}"
- elsif key == 'summary' && GemTemplate::Gems.gemset == :solo
+ TimedConfig::Gems.gemspec.hash.each do |key, value|
+ if key == 'name' && TimedConfig::Gems.gemset != :default
+ s.name = "#{value}-#{TimedConfig::Gems.gemset}"
+ elsif key == 'summary' && TimedConfig::Gems.gemset == :solo
s.summary = value + " (no dependencies)"
elsif !%w(dependencies development_dependencies).include?(key)
s.send "#{key}=", value
end
end
- GemTemplate::Gems.dependencies.each do |g|
- s.add_dependency g.to_s, GemTemplate::Gems.versions[g]
+ TimedConfig::Gems.dependencies.each do |g|
+ s.add_dependency g.to_s, TimedConfig::Gems.versions[g]
end
- GemTemplate::Gems.development_dependencies.each do |g|
- s.add_development_dependency g.to_s, GemTemplate::Gems.versions[g]
+ TimedConfig::Gems.development_dependencies.each do |g|
+ s.add_development_dependency g.to_s, TimedConfig::Gems.versions[g]
end
s.executables = `cd #{root} && git ls-files -- {bin}/*`.split("\n").collect { |f| File.basename(f) }

0 comments on commit c710d44

Please sign in to comment.