Class: Picky::Backends::Prepared::Text

Inherits:
Object
  • Object
show all
Includes:
Helpers::File
Defined in:
lib/picky/backends/prepared/text.rb

Overview

Index data dumped in the text format.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::File

#create_directory

Constructor Details

#initialize(cache_path, options = {}) ⇒ Text

Returns a new instance of Text.



15
16
17
# File 'lib/picky/backends/prepared/text.rb', line 15

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

Instance Attribute Details

#cache_pathObject (readonly)

Returns the value of attribute cache_path.



13
14
15
# File 'lib/picky/backends/prepared/text.rb', line 13

def cache_path
  @cache_path
end

Instance Method Details

#dump(hash) ⇒ Object

Text files are used exclusively for prepared data files.



41
42
43
# File 'lib/picky/backends/prepared/text.rb', line 41

def dump hash
  raise "Can't dump to text file. Use JSON or Marshal."
end

#extensionObject

Uses the extension “txt”.



21
22
23
# File 'lib/picky/backends/prepared/text.rb', line 21

def extension
  :txt
end

#initialObject

The initial content before loading.



27
28
29
# File 'lib/picky/backends/prepared/text.rb', line 27

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

#loadObject

Text files are used exclusively for prepared data files.



34
35
36
# File 'lib/picky/backends/prepared/text.rb', line 34

def load
  raise "Can't load from text file. Use JSON or Marshal."
end

#open(&block) ⇒ Object



67
68
69
70
# File 'lib/picky/backends/prepared/text.rb', line 67

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

#retrieveObject

Retrieves prepared index data in the form

  • id,datan

  • id,datan

  • id,datan

Yields an id string and a token.

TODO Think about the comma - what if you have commas in the id? Use CSV?



54
55
56
57
58
59
60
61
62
63
# File 'lib/picky/backends/prepared/text.rb', line 54

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