Sha256: 25fa45c00258482ced5d1bbe0382c487fe87ead81181391d75fa9d5866c6d0e4

Contents?: true

Size: 1.34 KB

Versions: 6

Compression:

Stored size: 1.34 KB

Contents

require 'base64'
require 'zlib'

module WhirledPeas
  module Utils
    module FileHandler
      module FileWriter
        VERSION = '1'

        def self.write(fp, renders)
          fp.puts renders.count
          renders.each do |strokes|
            encoded = Base64.encode64(strokes)
            fp.puts encoded.count("\n")
            fp.puts encoded
          end
        end
      end
      private_constant :FileWriter

      class FileReaderV1
        def self.read(fp)
          num_renders = Integer(fp.readline.chomp, 10)
          num_renders.times.map do
            num_strokes = Integer(fp.readline.chomp, 10)
            Base64.decode64(num_strokes.times.map { fp.readline }.join)
          end
        end

        private

        attr_reader :file
      end
      private_constant :FileReaderV1

      READERS = {
        '1' => FileReaderV1
      }
      private_constant :READERS

      def self.write(file, renders)
        Zlib::GzipWriter.open(file, Zlib::BEST_COMPRESSION) do |gz|
          gz.puts FileWriter::VERSION
          FileWriter.write(gz, renders)
        end
      end

      def self.read(file)
        Zlib::GzipReader.open(file) do |gz|
          version = gz.gets.chomp
          raise ArgumentError, "Invalid file: #{file}" unless READERS.key?(version)
          READERS[version].read(gz)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
whirled_peas-0.11.1 lib/whirled_peas/utils/file_handler.rb
whirled_peas-0.11.0 lib/whirled_peas/utils/file_handler.rb
whirled_peas-0.10.0 lib/whirled_peas/utils/file_handler.rb
whirled_peas-0.9.1 lib/whirled_peas/utils/file_handler.rb
whirled_peas-0.9.0 lib/whirled_peas/utils/file_handler.rb
whirled_peas-0.8.0 lib/whirled_peas/utils/file_handler.rb