lib/alf/environment/folder.rb in alf-0.10.1 vs lib/alf/environment/folder.rb in alf-0.11.0

- old
+ new

@@ -7,56 +7,54 @@ # recognized files in a specific folder. "Recognized" files are simply # those for which a Reader subclass has been previously registered. # This environment then serves reader instances. # class Folder < Environment - - # - # (see Environment.recognizes?) - # - # Returns true if args contains onely a String which is an existing - # folder. - # - def self.recognizes?(args) - (args.size == 1) && - args.first.is_a?(String) && - File.directory?(args.first.to_s) - end - - # + + class << self + + # (see Environment.recognizes?) + # + # @return [Boolean] true if args contains one String only, which denotes + # an existing folder; false otherwise + def recognizes?(args) + (args.size == 1) && + args.first.is_a?(String) && + File.directory?(args.first.to_s) + end + + end # class << self + # Creates an environment instance, wired to the specified folder. # - # @param [String] folder path to the folder to use as dataset source - # + # @param [String] folder path to the folder to use as dataset source. def initialize(folder) @folder = folder end - + # (see Environment#dataset) def dataset(name) if file = find_file(name) Reader.reader(file, self) else raise NoSuchDatasetError, "No such dataset #{name} (#{@folder})" end end - + protected - + + # Finds a specific file by name + # + # @param [String] name the name of a dataset + # @return [String] path to an existing file if it exists, nil otherwise. def find_file(name) - # TODO: refactor this, because it allows getting out of the folder - if File.exists?(name.to_s) - name.to_s - elsif File.exists?(explicit = File.join(@folder, name.to_s)) && - File.file?(explicit) + if File.file?(explicit = File.join(@folder, name.to_s)) explicit else - Dir[File.join(@folder, "#{name}.*")].find do |f| - File.file?(f) - end + Dir[File.join(@folder, "#{name}.*")].find{|f| File.file?(f)} end end - + Environment.register(:folder, self) end # class Folder end # class Environment end # module Alf