lib/ore/config.rb in ore-0.10.0 vs lib/ore/config.rb in ore-0.11.0

- old
+ new

@@ -1,7 +1,6 @@ -require 'rubygems' -require 'pathname' +require 'ore/options' module Ore module Config # The users home directory HOME = Gem.user_home @@ -13,62 +12,57 @@ OPTIONS_FILE = File.join(PATH,'options.yml') # Custom Ore Templates directory TEMPLATES_DIR = File.join(PATH,'templates') - # The `data/` directory for Ore - DATA_DIR = File.expand_path(File.join('..','..','data'),File.dirname(__FILE__)) + # The `data/ore` directory for Ore + DATA_DIR = File.expand_path(File.join('..','..','data','ore'),File.dirname(__FILE__)) - # Specifies whether user settings will be loaded - @@enabled = true + # The `data/ore/templates` directory for Ore + BUILTIN_TEMPLATES_DIR = File.join(DATA_DIR,'templates') + @enabled = true + # - # Enables access to user settings. + # Enables access to user config. # + # @api private + # # @since 0.5.0 # - def Config.enable! - @@enabled = true + def self.enable! + @enabled = true end # - # Disables access to user settings. + # Disables access to user config. # + # @api private + # # @since 0.5.0 # - def Config.disable! - @@enabled = false + def self.disable! + @enabled = false end # # Loads the default options from `~/.ore/options.yml`. # - # @return [Hash] + # @return [Options] # The loaded default options. # # @raise [RuntimeError] # The `~/.ore/options.yml` did not contain a YAML encoded Hash. # # @since 0.9.0 # - def Config.options - options = {} - - if (@@enabled && File.file?(OPTIONS_FILE)) - new_options = YAML.load_file(OPTIONS_FILE) - - # default options must be a Hash - unless new_options.kind_of?(Hash) - raise("#{OPTIONS_FILE} must contain a YAML encoded Hash") - end - - new_options.each do |name,value| - options[name.to_sym] = value - end - end - - return options + def self.options + @options ||= if @enabled && File.file?(OPTIONS_FILE) + Options.load(OPTIONS_FILE) + else + Options.new + end end # # The builtin templates. # @@ -76,15 +70,13 @@ # The given block will be passed every builtin template. # # @yieldparam [String] path # The path of a Ore template directory. # - def Config.builtin_templates - path = File.join(DATA_DIR,'ore','templates') - - if File.directory?(path) - Dir.glob("#{path}/*") do |template| + def self.builtin_templates + if File.directory?(BUILTIN_TEMPLATES_DIR) + Dir.glob("#{BUILTIN_TEMPLATES_DIR}/*") do |template| yield template if File.directory?(template) end end end @@ -95,15 +87,15 @@ # The given block will be passed every installed template. # # @yieldparam [String] path # The path of a Ore template directory. # - def Config.installed_templates - if @@enabled - if File.directory?(TEMPLATES_DIR) - Dir.glob("#{TEMPLATES_DIR}/*") do |template| - yield template if File.directory?(template) - end + def self.installed_templates + return unless @enabled + + if File.directory?(TEMPLATES_DIR) + Dir.glob("#{TEMPLATES_DIR}/*") do |template| + yield template if File.directory?(template) end end end end end