Class: Rindle::Collections

Inherits:
Hash
  • Object
show all
Defined in:
lib/rindle/collections.rb

Defined Under Namespace

Classes: NoSuchFile

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kindle_root) ⇒ Collections

Returns a new instance of Collections.



11
12
13
# File 'lib/rindle/collections.rb', line 11

def initialize(kindle_root)
  @collections_file = File.join(kindle_root, 'system', 'collections.json')
end

Class Method Details

.load(kindle_root) ⇒ Object



15
16
17
# File 'lib/rindle/collections.rb', line 15

def self.load(kindle_root)
  Collections.new(kindle_root).load
end

Instance Method Details

#loadObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rindle/collections.rb', line 29

def load
  unless File.exists?(@collections_file)
    raise NoSuchFile, "Not found: #{@collections_file}"
  end

  hash = File.open(@collections_file, 'r') do |file|
    begin
      JSON.load(file)
    rescue Exception => e
      {}
    end
  end
  hash.each_pair do |name, data|
    col = Collection.new name, :indices => data['items'],
                               :last_access => data['lastAccess']
    self[col.name] = col
  end
  self
end

#saveObject



19
20
21
22
23
24
25
26
27
# File 'lib/rindle/collections.rb', line 19

def save
  hash = {}
  values.each do |col|
    hash.merge! col.to_hash
  end
  File.open(@collections_file, 'w+') do |f|
    JSON.dump(hash, f)
  end
end