Class: AnkiRecord::Anki21Database
- Inherits:
-
Object
- Object
- AnkiRecord::Anki21Database
- 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
-
.create_new(anki_package:) ⇒ Object
:nodoc:.
-
.update_new(anki_package:) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#add_deck(deck) ⇒ Object
:nodoc:.
-
#add_deck_options_group(deck_options_group) ⇒ Object
:nodoc:.
-
#add_note_type(note_type) ⇒ Object
:nodoc:.
-
#col_record ⇒ Object
:nodoc:.
-
#decks_json ⇒ Object
:nodoc:.
-
#find_deck_by(name: nil, id: nil) ⇒ Object
Returns the deck found by either
name
orid
, or nil if it is not found. -
#find_deck_options_group_by(id:) ⇒ Object
Returns the deck options group object found by
id
, or nil if it is not found. -
#find_note_by(sfld: nil, id: nil) ⇒ Object
rubocop:disable Metrics/MethodLength.
-
#find_note_type_by(name: nil, id: nil) ⇒ Object
Returns the note type found by either
name
orid
, or nil if it is not found. -
#find_notes_by_exact_text_match(text:) ⇒ Object
Returns an array of notes that have any field value matching
text
. -
#inspect ⇒ Object
:nocov:.
-
#models_json ⇒ Object
:nodoc:.
-
#prepare(sql) ⇒ Object
Returns an SQLite3::Statement object (sqlite3 gem) to be executed against the collection.anki21 database.
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:
122 123 124 125 126 |
# File 'lib/anki_record/anki21_database/anki21_database.rb', line 122 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:
128 129 130 131 132 |
# File 'lib/anki_record/anki21_database/anki21_database.rb', line 128 def () # :nodoc: raise ArgumentError unless .instance_of?(AnkiRecord::DeckOptionsGroup) << end |
#add_note_type(note_type) ⇒ Object
:nodoc:
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/anki_record/anki21_database/anki21_database.rb', line 110 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_record ⇒ Object
:nodoc:
106 107 108 |
# File 'lib/anki_record/anki21_database/anki21_database.rb', line 106 def col_record # :nodoc: prepare("select * from col").execute.first end |
#decks_json ⇒ Object
:nodoc:
98 99 100 |
# File 'lib/anki_record/anki21_database/anki21_database.rb', line 98 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.
83 84 85 86 87 88 89 90 |
# File 'lib/anki_record/anki21_database/anki21_database.rb', line 83 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 to find_deck_by." 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.
94 95 96 |
# File 'lib/anki_record/anki21_database/anki21_database.rb', line 94 def (id:) .find { || .id == id } end |
#find_note_by(sfld: nil, id: nil) ⇒ Object
rubocop:disable Metrics/MethodLength
Returns the note found by either +sfld“ or id
, or nil if it is not found.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/anki_record/anki21_database/anki21_database.rb', line 36 def find_note_by(sfld: nil, id: nil) if (id && sfld) || (id.nil? && sfld.nil?) raise ArgumentError, "You must pass either an id or sfld keyword argument to find_note_by." end note_cards_data = if id note_cards_data_for_note(id:) else note_cards_data_for_note(sfld:) end 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.
72 73 74 75 76 77 78 79 |
# File 'lib/anki_record/anki21_database/anki21_database.rb', line 72 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 to find_note_type_by." end name ? find_note_type_by_name(name:) : find_note_type_by_id(id:) end |
#find_notes_by_exact_text_match(text:) ⇒ Object
Returns an array of notes that have any field value matching text
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/anki_record/anki21_database/anki21_database.rb', line 54 def find_notes_by_exact_text_match(text:) return [] if text == "" like_matcher = "%#{text}%" note_datas = prepare("select * from notes where flds LIKE ?").execute([like_matcher]) note_datas.map do |note_data| id = note_data["id"] cards_data = prepare("select * from cards where nid = ?").execute([id]).to_a note_cards_data = { note_data:, cards_data: } AnkiRecord::Note.new(anki21_database: self, data: note_cards_data) end end |
#inspect ⇒ Object
:nocov:
135 136 137 |
# File 'lib/anki_record/anki21_database/anki21_database.rb', line 135 def inspect # :nodoc: "[= Anki21Database of package with name #{package.name} =]" end |
#models_json ⇒ Object
:nodoc:
102 103 104 |
# File 'lib/anki_record/anki21_database/anki21_database.rb', line 102 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 |