Sha256: f5054834bd508482b2454ce15af7018a7056965a3a52ea56a2ce5eb3808166cb

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

class Forgery

  class FileReader

    # Returns an array of strings containing each line in the dictionary
    def self.read_dictionary(dictionary)
      read_file(find_file(dictionary, :dictionaries))
    end

    # Returns an array of strings containing each line in the format
    def self.read_format(format)
      read_file(find_file(format, :formats))
    end

  protected

    # Reads a file while stripping leading and trailing whitespace, including
    # newlines
    def self.read_file(file)
      lines = []
      IO.foreach(file) do |line|
        lines << line.strip unless line.strip == ''
      end
      lines
    end

    # Search a file in all load_paths, starting from last to first, so
    # last takes precedence over first.
    def self.find_file(name, folder)
      Forgery.load_paths.reverse.each do |path|
        file = "#{path}/#{folder}/#{name}"
        return file if File.exists?(file)
      end
      raise ArgumentError.new("File '#{name}' wasn't found in '#{folder}' folder. Searched paths: \n#{Forgery.load_paths.join('\n')}")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
forgery-0.6.0 lib/forgery/file_reader.rb