lib/config.rb in xolti-0.0.0 vs lib/config.rb in xolti-0.1.0

- old
+ new

@@ -15,31 +15,46 @@ # # You should have received a copy of the GNU General Public License # along with Xolti. If not, see <http://www.gnu.org/licenses/>. require "yaml" require "pathname" +require "date" -def extract_project_info(raw_project_info) - { - author: raw_project_info["author"], - project_name: raw_project_info["project_name"], - year: raw_project_info["year"] || Date.today().year - } -end +require_relative "default_comment_tokens" +require_relative "resources" -module XoltiConfig - def XoltiConfig.find_config_file(path = Pathname.getwd) - return nil if path.root? +class XoltiConfig + + def self.find_config_file(path = Pathname.getwd) potential_config_file = (path + "xolti.yml") - potential_config_file.file? ? potential_config_file.to_s : find_config_file(path.parent) + return potential_config_file.to_s if potential_config_file.file? + raise "No xolti.yml found" if path.root? + find_config_file(path.parent) end - def XoltiConfig.load_config() + def self.load_config() raw_config = YAML.load(IO.binread(find_config_file())) + XoltiConfig.new(raw_config) + end + + attr_reader :project_info, :template, :offset, :license + + def initialize(raw_config) + @project_info = extract_project_info(raw_config["project_info"]) + @comment = DefaultComment::HASH.merge!(raw_config["comment"] || {}) + @license = raw_config["license"] + @template = raw_config.include?("template") ? raw_config["template"] : IO.binread(Resources.get_template_path(@license)) + @offset = raw_config["offset"] || 0 + end + + def get_comment(ext) + @comment[ext.delete('.')] + end + + private def extract_project_info(raw_project_info) { - project_info: extract_project_info(raw_config["project_info"]), - comment: raw_config["comment"] || ["/*", " * ", " */"], - template: raw_config["template"] || "header.txt", - offset: raw_config["offset"] || 0 + author: raw_project_info["author"], + project_name: raw_project_info["project_name"], + year: raw_project_info["year"] || Date.today().year.to_s } end end