Sha256: 7ec509836e403d1840ac0a87b74f4904e7a0e927b5aa243a832ef9536de8bbd4

Contents?: true

Size: 976 Bytes

Versions: 1

Compression:

Stored size: 976 Bytes

Contents

require 'rubygems'
require 'json'

require 'rindle/collection'

class Rindle
  class Collections < Hash

    class NoSuchFile < Exception; end

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

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

    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

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rindle-0.3.2 lib/rindle/collections.rb