Class: AnkiRecord::AnkiPackage

Inherits:
Object
  • Object
show all
Defined in:
lib/anki_record/anki_package/anki_package.rb

Overview

AnkiPackage represents the Anki deck package file which has the .apkg file extension

This is a zip file containing two SQLite databases (collection.anki21 and collection.anki2), a media file, and possibly the media (images and sound files). The gem currently does not have any support for adding or changing media in the Anki package.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#anki21_databaseObject

:nodoc:



21
22
23
# File 'lib/anki_record/anki_package/anki_package.rb', line 21

def anki21_database
  @anki21_database
end

#anki2_databaseObject

:nodoc:



21
22
23
# File 'lib/anki_record/anki_package/anki_package.rb', line 21

def anki2_database
  @anki2_database
end

#mediaObject

:nodoc:



21
22
23
# File 'lib/anki_record/anki_package/anki_package.rb', line 21

def media
  @media
end

#nameObject

:nodoc:



21
22
23
# File 'lib/anki_record/anki_package/anki_package.rb', line 21

def name
  @name
end

#target_directoryObject

:nodoc:



21
22
23
# File 'lib/anki_record/anki_package/anki_package.rb', line 21

def target_directory
  @target_directory
end

#tmpdirObject

:nodoc:



21
22
23
# File 'lib/anki_record/anki_package/anki_package.rb', line 21

def tmpdir
  @tmpdir
end

#tmpfilesObject

:nodoc:



21
22
23
# File 'lib/anki_record/anki_package/anki_package.rb', line 21

def tmpfiles
  @tmpfiles
end

Class Method Details

.create(name:, target_directory: Dir.pwd, &closure) ⇒ Object

Creates a new Anki package file (see the README)



25
26
27
28
29
# File 'lib/anki_record/anki_package/anki_package.rb', line 25

def self.create(name:, target_directory: Dir.pwd, &closure)
  anki_package = new
  anki_package.create_initialize(name:, target_directory:, &closure)
  anki_package
end

.update(path:, &closure) ⇒ Object

Opens an existing Anki package file to update its contents (see the README)



46
47
48
49
50
# File 'lib/anki_record/anki_package/anki_package.rb', line 46

def self.update(path:, &closure)
  anki_package = new
  anki_package.update_initialize(path:, &closure)
  anki_package
end

Instance Method Details

#create_initialize(name:, target_directory: Dir.pwd, &closure) ⇒ Object

:nodoc:



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/anki_record/anki_package/anki_package.rb', line 31

def create_initialize(name:, target_directory: Dir.pwd, &closure) # :nodoc:
  validate_arguments(name:, target_directory:)
  @name = new_apkg_name(name:)
  @target_directory = target_directory
  @tmpdir = Dir.mktmpdir
  @tmpfiles = [Anki21Database::FILENAME, Anki2Database::FILENAME, Media::FILENAME]
  @anki21_database = Anki21Database.create_new(anki_package: self)
  @anki2_database = Anki2Database.create_new(anki_package: self)
  @media = Media.create_new(anki_package: self)

  execute_closure_and_zip(anki21_database, &closure) if closure
end

#inspectObject

:nocov:



73
74
75
# File 'lib/anki_record/anki_package/anki_package.rb', line 73

def inspect
  "[= AnkiPackage name: #{name} target_directory: #{target_directory} =]"
end

#update_initialize(path:, &closure) ⇒ Object

:nodoc:



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/anki_record/anki_package/anki_package.rb', line 52

def update_initialize(path:, &closure) # :nodoc:
  validate_path(path:)

  @tmpdir = Dir.mktmpdir
  unzip_apkg_into_tmpdir(path:)
  @tmpfiles = [Anki21Database::FILENAME, Anki2Database::FILENAME, Media::FILENAME]
  @anki21_database = Anki21Database.update_new(anki_package: self)
  @anki2_database = Anki2Database.update_new(anki_package: self)
  @media = Media.update_new(anki_package: self)

  @updating_existing_apkg = true
  execute_closure_and_zip(anki21_database, &closure) if closure
end

#zipObject

:nodoc:



67
68
69
70
# File 'lib/anki_record/anki_package/anki_package.rb', line 67

def zip
  @updating_existing_apkg ? replace_zip_file : create_zip_file
  destroy_temporary_directory
end