Sha256: 9cb93a9781d7ab19f1d67f789e8d90730d2eedd5b4c1a3e362fe1add50e1c2e6

Contents?: true

Size: 1.83 KB

Versions: 3

Compression:

Stored size: 1.83 KB

Contents

module RConfig
  module LoadPaths
    include Constants

    ##
    # Sets the list of directories to search for
    # configuration files.
    # The argument must be an array of strings representing
    # the paths to the directories, or a string representing
    # either a single path or a list of paths separated by
    # either a colon (:) or a semi-colon (;).
    def set_load_paths(paths)
      self.load_paths = parse_load_paths(paths)
      reload(true)  # Load Paths have changed so force a reload
    end

    ##
    # Adds the specified path to the list of directories to search for
    # configuration files.
    # It only allows one path to be entered at a time.
    def add_load_path(path)
      if path = parse_load_paths(path).first # only accept first one.
        self.load_paths << path
        return reload(true)  # Load Paths have changed so force a reload
      end
      false
    end

    ##
    # If the paths are made up of a delimited string, then parse out the
    # individual paths. Verify that each path is valid.
    def parse_load_paths(paths)
      return [] unless paths
      if paths.is_a? String
        path_sep = (paths =~ /;/) ? ';' : ':'
        paths = paths.split(/#{path_sep}+/)
      end
      raise ArgumentError, "Path(s) must be a String or an Array [#{paths.inspect}]" unless paths.is_a? Array
      raise ArgumentError, "Must provide at least one load path: [#{paths.inspect}]" if paths.empty?
      paths.each do |dir|
        dir = CONFIG_ROOT if dir == 'CONFIG_ROOT'
        raise RConfig::InvalidConfigPathError, "This directory is invalid: [#{dir.inspect}]" unless Dir.exists?(dir)
      end
      paths
    end

    ##
    # Indicates whether or not config_paths have been set.
    # Returns true if self.load_paths has at least one directory.
    def load_paths_set?
      not load_paths.blank?
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rconfig-0.4.2 lib/rconfig/load_paths.rb
rconfig-0.4.1 lib/rconfig/load_paths.rb
rconfig-0.4.0 lib/rconfig/load_paths.rb