Class: AnkiRecord::Anki21Database

Inherits:
Object
  • Object
show all
Includes:
Anki21DatabaseAttributes, Anki21DatabaseConstructors
Defined in:
lib/anki_record/anki21_database/anki21_database.rb

Overview

Anki21Database represents the collection.anki21 Anki SQLite database in the Anki Package

Constant Summary

Constants included from Anki21DatabaseConstructors

AnkiRecord::Anki21DatabaseConstructors::FILENAME

Instance Attribute Summary

Attributes included from Anki21DatabaseAttributes

#anki_package, #collection, #database, #deck_options_groups, #decks, #note_types

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Anki21DatabaseConstructors

#create_initialize, #update_initialize

Class Method Details

.create_new(anki_package:) ⇒ Object

:nodoc:



13
14
15
16
17
# File 'lib/anki_record/anki21_database/anki21_database.rb', line 13

def self.create_new(anki_package:) # :nodoc:
  anki21_database = new
  anki21_database.create_initialize(anki_package:)
  anki21_database
end

.update_new(anki_package:) ⇒ Object

:nodoc:



19
20
21
22
23
# File 'lib/anki_record/anki21_database/anki21_database.rb', line 19

def self.update_new(anki_package:) # :nodoc:
  anki21_database = new
  anki21_database.update_initialize(anki_package:)
  anki21_database
end

Instance Method Details

#add_deck(deck) ⇒ Object

:nodoc:

Raises:

  • (ArgumentError)


94
95
96
97
98
# File 'lib/anki_record/anki21_database/anki21_database.rb', line 94

def add_deck(deck) # :nodoc:
  raise ArgumentError unless deck.instance_of?(AnkiRecord::Deck)

  decks << deck
end

#add_deck_options_group(deck_options_group) ⇒ Object

:nodoc:

Raises:

  • (ArgumentError)


100
101
102
103
104
# File 'lib/anki_record/anki21_database/anki21_database.rb', line 100

def add_deck_options_group(deck_options_group) # :nodoc:
  raise ArgumentError unless deck_options_group.instance_of?(AnkiRecord::DeckOptionsGroup)

  deck_options_groups << deck_options_group
end

#add_note_type(note_type) ⇒ Object

:nodoc:

Raises:

  • (ArgumentError)


82
83
84
85
86
87
88
89
90
91
92
# File 'lib/anki_record/anki21_database/anki21_database.rb', line 82

def add_note_type(note_type) # :nodoc:
  raise ArgumentError unless note_type.instance_of?(AnkiRecord::NoteType)

  existing_note_type = nil
  note_types.each do |nt|
    existing_note_type = nt if nt.id == note_type.id
  end
  note_types.delete(existing_note_type) if existing_note_type

  note_types << note_type
end

#col_recordObject

:nodoc:



78
79
80
# File 'lib/anki_record/anki21_database/anki21_database.rb', line 78

def col_record # :nodoc:
  prepare("select * from col").execute.first
end

#decks_jsonObject

:nodoc:



70
71
72
# File 'lib/anki_record/anki21_database/anki21_database.rb', line 70

def decks_json # :nodoc:
  JSON.parse(prepare("select decks from col;").execute.first["decks"])
end

#find_deck_by(name: nil, id: nil) ⇒ Object

Returns the deck found by either name or id, or nil if it is not found.



55
56
57
58
59
60
61
62
# File 'lib/anki_record/anki21_database/anki21_database.rb', line 55

def find_deck_by(name: nil, id: nil)
  if (id && name) || (id.nil? && name.nil?)
    raise ArgumentError,
          "You must pass either an id or name keyword argument."
  end

  name ? find_deck_by_name(name:) : find_deck_by_id(id:)
end

#find_deck_options_group_by(id:) ⇒ Object

Returns the deck options group object found by id, or nil if it is not found.



66
67
68
# File 'lib/anki_record/anki21_database/anki21_database.rb', line 66

def find_deck_options_group_by(id:)
  deck_options_groups.find { |deck_options_group| deck_options_group.id == id }
end

#find_note_by(id:) ⇒ Object

Returns the note found by id, or nil if it is not found.



35
36
37
38
39
40
# File 'lib/anki_record/anki21_database/anki21_database.rb', line 35

def find_note_by(id:)
  note_cards_data = note_cards_data_for_note_id(id:)
  return nil unless note_cards_data

  AnkiRecord::Note.new(anki21_database: self, data: note_cards_data)
end

#find_note_type_by(name: nil, id: nil) ⇒ Object

Returns the note type found by either name or id, or nil if it is not found.



44
45
46
47
48
49
50
51
# File 'lib/anki_record/anki21_database/anki21_database.rb', line 44

def find_note_type_by(name: nil, id: nil)
  if (id && name) || (id.nil? && name.nil?)
    raise ArgumentError,
          "You must pass either an id or name keyword argument."
  end

  name ? find_note_type_by_name(name:) : find_note_type_by_id(id:)
end

#inspectObject

:nocov:



107
108
109
# File 'lib/anki_record/anki21_database/anki21_database.rb', line 107

def inspect # :nodoc:
  "[= Anki21Database of package with name #{package.name} =]"
end

#models_jsonObject

:nodoc:



74
75
76
# File 'lib/anki_record/anki21_database/anki21_database.rb', line 74

def models_json # :nodoc:
  JSON.parse(prepare("select models from col;").execute.first["models"])
end

#prepare(sql) ⇒ Object

Returns an SQLite3::Statement object (sqlite3 gem) to be executed against the collection.anki21 database.

Statement#execute executes the statement.



29
30
31
# File 'lib/anki_record/anki21_database/anki21_database.rb', line 29

def prepare(sql)
  database.prepare sql
end