Browse files

Load projects directory from config file

  • Loading branch information...
1 parent d11d1e2 commit b1ac36a1e934768c63160b94608b32c6ed6238b3 @joefiorini committed May 31, 2011
View
11 lib/projit.rb
@@ -1,9 +1,18 @@
require "projit/version"
+require 'yaml'
module Projit
- autoload :CLI, 'projit/cli'
+ autoload :CLI, 'projit/cli'
autoload :ProjectGenerator, 'projit/project_generator'
+ def self.config
+ @config ||= load_config!
+ @config
+ end
+
+ def self.load_config!
+ YAML.load_file("~/.projit/config")
+ end
end
View
35 lib/projit/config.rb
@@ -0,0 +1,35 @@
+module Projit
+
+ class Config
+
+ def projects_home
+ config_dir(read_config(:projects_home) || defaults[:projects_home])
+ end
+
+ def read_config(config)
+ return nil unless read_config_file
+ read_config_file[config]
+ end
+
+ private
+
+ def read_config_file
+ YAML.load_file(projit_config)
+ end
+
+ def projit_config
+ File.expand_path("~/.projit/config")
+ end
+
+ def config_dir(dir)
+ File.expand_path(dir)
+ end
+
+ def defaults
+ {
+ projects_home: "~/Projects"
+ }
+ end
+
+ end
+end
View
2 lib/projit/project_generator.rb
@@ -29,7 +29,7 @@ def in_project_directory(&block)
end
def projects_root
- Pathname.new File.expand_path("~/Projects")
+ Projit.config.projects_home
end
end
View
2 projit.gemspec
@@ -15,6 +15,8 @@ Gem::Specification.new do |s|
s.add_dependency 'thor'
+ s.add_development_dependency 'rspec'
+
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
View
38 spec/projit/config_spec.rb
@@ -0,0 +1,38 @@
+require 'projit/config'
+
+describe Projit::Config do
+
+ context "projit settings" do
+
+ before do
+ (subject.stub :read_config_file).and_return({ projects_home: "~/Testing" })
+ end
+
+ its(:projit_config) { should == "/Users/joe/.projit/config" }
+ its(:projects_home) { should == "/Users/joe/Testing" }
+
+ end
+
+ context "setting defaults" do
+
+ before do
+ (subject.stub :read_config_file).and_return(nil)
+ end
+
+ its(:projects_home) { should == "/Users/joe/Projects" }
+
+ end
+
+ context "reading config settings" do
+
+ before do
+ (subject.stub :read_config_file).and_return(nil)
+ end
+
+ it "doesn't crash for non-existant config" do
+ expect { subject.read_config(:blah) }.to_not raise_error
+ end
+
+ end
+
+end
View
12 spec/projit/project_generator_spec.rb
@@ -0,0 +1,12 @@
+require 'projit/project_generator'
+
+describe Projit::ProjectGenerator do
+
+ subject { Projit::ProjectGenerator.new ["blah"] }
+
+ it "loads projects_root from config settings" do
+ ((Projit.stub :config).and_return stub projects_home: "~/Projects")
+ (subject.send :projects_root).should == "~/Projects"
+ end
+
+end
View
20 spec/projit_spec.rb
@@ -0,0 +1,20 @@
+require 'projit'
+
+describe Projit do
+
+ it "loads configs from YAML" do
+ YAML.should_receive(:load_file).with("~/.projit/config")
+ Projit.load_config!
+ end
+
+ context "when retrieving config" do
+
+ it "loads config if not already set" do
+ Projit.instance_variable_set("@config", nil)
+ Projit.should_receive(:load_config!)
+ Projit.config
+ end
+
+ end
+
+end

0 comments on commit b1ac36a

Please sign in to comment.