Class: Interpres::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/interpres/cache.rb

Constant Summary collapse

CACHE =
'.interpres_cache.json'

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



6
7
8
9
10
11
# File 'lib/interpres/cache.rb', line 6

def initialize
  mode = File.file?(CACHE) ? 'r' : 'w+'
  file = File.open(CACHE, mode)
  @cached_texts = Oj.load(file.read) || []
  file.close
end

Instance Method Details

#diff(texts) ⇒ Object



24
25
26
# File 'lib/interpres/cache.rb', line 24

def diff(texts)
  texts - @cached_texts
end

#readObject



13
14
15
# File 'lib/interpres/cache.rb', line 13

def read
  @cached_texts
end

#write(texts) ⇒ Object



17
18
19
20
21
22
# File 'lib/interpres/cache.rb', line 17

def write(texts)
  file = File.open(CACHE, 'w')
  file.write(Oj.dump texts)
  file.close
  @cached_texts = texts
end