class KindleCG::Generator

Public Class Methods

new() click to toggle source
# File lib/KindleCG.rb, line 38
def initialize
  @collections = Collections.new
end

Public Instance Methods

backup() click to toggle source
# File lib/KindleCG.rb, line 67
def backup
  FileUtils.cp(KindleCG.os_collections_path, [KindleCG.os_collections_path, 'bak'].join('.'), {preserve: false})
end
generate_collections() click to toggle source
# File lib/KindleCG.rb, line 42
def generate_collections
  generate_tree(KindleCG.os_documents_path)
  @collections
end
generate_tree(path, current_collection = nil) click to toggle source
# File lib/KindleCG.rb, line 47
def generate_tree(path, current_collection = nil)
  Dir.foreach(path) do |item|
    next if item[0] == '.'

    fullpath = [path, item].join("/")

    if File.directory?(fullpath)
      new_collection = @collections.add(relative_path(fullpath))
      generate_tree(fullpath, new_collection)
    else
      begin
        ebook = Ebook::Ebook.new(fullpath)
        @collections.add_item_to_collection(current_collection, ebook) unless current_collection.nil?
      rescue Ebook::EbookError
        next
      end
    end
  end
end
save() click to toggle source
# File lib/KindleCG.rb, line 71
def save
  IO.write(KindleCG.os_collections_path, @collections.to_json)
end

Private Instance Methods

relative_path(path) click to toggle source
# File lib/KindleCG.rb, line 77
def relative_path(path)
  _path = path.dup
  _path.slice!(KindleCG.os_documents_path + '/')
  _path == KindleCG.os_documents_path ? "" : _path
end