lib/settings.rb in capucine-0.1.7 vs lib/settings.rb in capucine-0.2.0
- old
+ new
@@ -1,75 +1,62 @@
+
module Capucine
- class NoUserConfigFile < RuntimeError
- end
-
class Settings
- require 'rubygems'
- require 'yaml'
+ require 'rubygems'
+ require 'yaml'
- attr_accessor :current_dir, :project_name, :is_usable, :config, :gem_dir, :gem_content_dir, :working_dir, :external_config, :user_config_file
+ attr_accessor :current_dir
+ attr_accessor :working_dir
+ attr_accessor :external_config
+ attr_accessor :project_name
+ attr_accessor :root_dir
+ attr_accessor :content_dir
+ attr_accessor :user_config_file
+ attr_accessor :conf
+
def initialize
- @current_dir = File.expand_path Dir.pwd
- @project_name = 'capucine'
- @is_usable = false
- @external_config = false
- @config = nil
- @gem_dir = Gem.loaded_specs[@project_name].full_gem_path
- @gem_content_dir = File.join @gem_dir, 'content'
- self.reset_working_dir
+ self.working_dir = File.expand_path(Dir.getwd)
+ self.project_name = 'capucine'
+ self.root_dir = File.expand_path('../..', __FILE__)
+ self.content_dir = File.expand_path('../../content', __FILE__)
+
+ self.set_user_config_file
+ return self
end
- def get_config user_config_file = nil
- @user_config_file = user_config_file
- default = File.join @gem_content_dir, "templates", "#{@project_name}.yaml"
- @config = YAML::load(File.open(default))
+ def set_user_config_file(file = nil, silent = false)
+ if not file
+ file = File.join self.working_dir, 'capucine.yaml'
+ end
- if user_config_file
- from_user = File.expand_path user_config_file
- @external_config = true
- @working_dir = File.dirname from_user
- else
- from_user = File.join @working_dir, "#{@project_name}.yaml"
+ if File.exist?(file)
+ self.user_config_file = File.expand_path(file)
end
- raise NoUserConfigFile, caller if not File.exist? from_user
+ self.get_conf
- @is_usable = true
- additional = YAML::load(File.open(from_user))
+ return self
+ end
+ def get_conf
+
+ default = File.join self.content_dir, "default.yaml"
+ self.conf = YAML::load_file(default)
+
+ if self.user_config_file
+ additional = YAML::load_file(self.user_config_file)
+ end
+
if additional
additional.each do |k, v|
- @config[k] = v
+ self.conf[k] = v
end
end
- #self.test_config
- end
- # def test_config
- # conf_dirs = []
- # dirs = []
- # for conf in conf_dirs
- # conf = @config[conf]
- # dirs.push File.join(@working_dir,conf)
- # end
-
- # for dir in dirs
- # unless File.directory?(dir)
- # puts "[error] #{dir} - Does not exist."
- # exit
- # end
- # end
- # end
-
- def reset_working_dir
- if @external_config
- from_user = File.expand_path @user_config_file
- @working_dir = File.dirname from_user
- else
- @working_dir = File.expand_path Dir.pwd
- end
+ return self
end
+
end
end