Sha256: c2bb7a22962a43f2d359d618b2b266b8eb036129cf24e540268a6d109b963cf8

Contents?: true

Size: 1.67 KB

Versions: 27

Compression:

Stored size: 1.67 KB

Contents

module Picky

  module Backends

    class Prepared

      # Index data dumped in the text format.
      #
      class Text

        include Helpers::File

        attr_reader :cache_path

        def initialize cache_path, options = {}
          @cache_path = "#{cache_path}.prepared.#{extension}"
        end

        # Uses the extension "txt".
        #
        def extension
          :txt
        end

        # The initial content before loading.
        #
        def initial
          raise "Can't have an initial content from text file. Use JSON or Marshal."
        end

        # Text files are used exclusively for
        # prepared data files.
        #
        def load
          raise "Can't load from text file. Use JSON or Marshal."
        end

        # Text files are used exclusively for
        # prepared data files.
        #
        def dump hash
          raise "Can't dump to text file. Use JSON or Marshal."
        end

        # Retrieves prepared index data in the form
        # * id,data\n
        # * id,data\n
        # * id,data\n
        #
        # Yields an id string and a token.
        #
        # TODO Think about the comma - what if you have commas in the id? Use CSV?
        #
        def retrieve
          id    = nil
          token = nil
          ::File.open(cache_path, 'r:utf-8') do |file|
            file.each_line do |line|
              id, token = line.split ?,, 2
              yield id.freeze, (token.chomp! || token).freeze
            end
          end
        end

        #
        #
        def open &block
          create_directory cache_path
          ::File.open cache_path, 'w:utf-8', &block
        end


      end

    end

  end

end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
picky-4.31.3 lib/picky/backends/prepared/text.rb
picky-4.31.2 lib/picky/backends/prepared/text.rb
picky-4.31.1 lib/picky/backends/prepared/text.rb
picky-4.31.0 lib/picky/backends/prepared/text.rb
picky-4.30.0 lib/picky/backends/prepared/text.rb
picky-4.29.0 lib/picky/backends/prepared/text.rb
picky-4.28.1 lib/picky/backends/prepared/text.rb
picky-4.27.1 lib/picky/backends/prepared/text.rb
picky-4.27.0 lib/picky/backends/prepared/text.rb
picky-4.26.2 lib/picky/backends/prepared/text.rb
picky-4.26.1 lib/picky/backends/prepared/text.rb
picky-4.26.0 lib/picky/backends/prepared/text.rb
picky-4.25.3 lib/picky/backends/prepared/text.rb
picky-4.25.2 lib/picky/backends/prepared/text.rb
picky-4.25.1 lib/picky/backends/prepared/text.rb
picky-4.25.0 lib/picky/backends/prepared/text.rb
picky-4.24.0 lib/picky/backends/prepared/text.rb
picky-4.23.2 lib/picky/backends/prepared/text.rb
picky-4.23.1 lib/picky/backends/prepared/text.rb
picky-4.23.0 lib/picky/backends/prepared/text.rb