Sha256: 08e2eb747baa2fc72f598462f1417130a451afddc20fc15e5136273530c996a9

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 KB

Contents

module Alf
  class Environment
    #
    # Specialization of Environment to work on files of a given folder.
    #
    # This kind of environment resolves datasets by simply looking at 
    # 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
      
      #
      # Creates an environment instance, wired to the specified folder.
      #
      # @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
      
      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)
          explicit
        else
          Dir[File.join(@folder, "#{name}.*")].find do |f|
            File.file?(f)
          end
        end
      end
      
      Environment.register(:folder, self)
    end # class Folder
  end # class Environment
end # module Alf

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
alf-0.10.1 lib/alf/environment/folder.rb