Sha256: 53ebee36eb21a7b5011ac2fd3f66eb10251601223f8005621d86cf2965cd2e7e

Contents?: true

Size: 1.65 KB

Versions: 19

Compression:

Stored size: 1.65 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?
        #
        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, (token.chomp! || token)
            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

19 entries across 19 versions & 1 rubygems

Version Path
picky-4.17.1 lib/picky/backends/prepared/text.rb
picky-4.17.0 lib/picky/backends/prepared/text.rb
picky-4.16.0 lib/picky/backends/prepared/text.rb
picky-4.15.1 lib/picky/backends/prepared/text.rb
picky-4.15.0 lib/picky/backends/prepared/text.rb
picky-4.14.0 lib/picky/backends/prepared/text.rb
picky-4.13.1 lib/picky/backends/prepared/text.rb
picky-4.13.0 lib/picky/backends/prepared/text.rb
picky-4.12.13 lib/picky/backends/prepared/text.rb
picky-4.12.12 lib/picky/backends/prepared/text.rb
picky-4.12.11 lib/picky/backends/prepared/text.rb
picky-4.12.10 lib/picky/backends/prepared/text.rb
picky-4.12.8 lib/picky/backends/prepared/text.rb
picky-4.12.7 lib/picky/backends/prepared/text.rb
picky-4.12.6 lib/picky/backends/prepared/text.rb
picky-4.12.5 lib/picky/backends/prepared/text.rb
picky-4.12.4 lib/picky/backends/prepared/text.rb
picky-4.12.3 lib/picky/backends/prepared/text.rb
picky-4.12.2 lib/picky/backends/prepared/text.rb