Sha256: c0a2168393462128efc4c6d69ec2054c5cf68adbcb28ef00257915ef891831d8

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

module Picky

  module Backends

    class Memory

      # Index data dumped in the text format.
      #
      # TODO Should this really be Memory::Text?
      #
      class Text < Basic

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

        # The initial content before loading.
        #
        def default
          raise "Can't have a default 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 symbol token.
        #
        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).to_sym
            end
          end
        end

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


      end

    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
picky-3.1.11 lib/picky/backends/memory/text.rb
picky-3.1.10 lib/picky/backends/memory/text.rb