Class: Anki::Importer::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/anki/importer/model.rb

Overview

Schema for Anki cards.

A deck can have multiple models.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(anki_id, name, description, tags, features) ⇒ Model

:nodoc: private



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/anki/importer/model.rb', line 43

def initialize(anki_id, name, description, tags, features)
  @anki_id = anki_id
  @name = name
  @description = description
  @tags = tags
  @features = features

  @fields = []
  @fields_by_id = {}
  @card_models = []
  @card_models_by_id = {}
  @facts = []
  @facts_by_id = {}
end

Instance Attribute Details

#anki_idObject (readonly)

Unique ID in the models table.



17
18
19
# File 'lib/anki/importer/model.rb', line 17

def anki_id
  @anki_id
end

#card_modelsObject (readonly)

Schema for cards based off this model.



26
27
28
# File 'lib/anki/importer/model.rb', line 26

def card_models
  @card_models
end

#card_models_by_idObject (readonly)

:nodoc: private



78
79
80
# File 'lib/anki/importer/model.rb', line 78

def card_models_by_id
  @card_models_by_id
end

#descriptionObject (readonly)

Generally empty.



13
14
15
# File 'lib/anki/importer/model.rb', line 13

def description
  @description
end

#factsObject (readonly)

Facts implementing this model.



20
21
22
# File 'lib/anki/importer/model.rb', line 20

def facts
  @facts
end

#facts_by_idObject (readonly)

:nodoc: private



89
90
91
# File 'lib/anki/importer/model.rb', line 89

def facts_by_id
  @facts_by_id
end

#fieldsObject (readonly)

Fields belonging to this model.



23
24
25
# File 'lib/anki/importer/model.rb', line 23

def fields
  @fields
end

#fields_by_idObject (readonly)

:nodoc: private



67
68
69
# File 'lib/anki/importer/model.rb', line 67

def fields_by_id
  @fields_by_id
end

#nameObject (readonly)

Name assigned in the Anki UI.



11
12
13
# File 'lib/anki/importer/model.rb', line 11

def name
  @name
end

#tagsObject (readonly)

Assigned in the Anki UI. Space-separated string.



15
16
17
# File 'lib/anki/importer/model.rb', line 15

def tags
  @tags
end

Class Method Details

.from_db(deck_db) ⇒ Object

Reads the models from an Anki deck.

Args:

deck_db:: a Sqlite3::Datbase

Returns an array of Model instances.



34
35
36
37
38
39
40
# File 'lib/anki/importer/model.rb', line 34

def self.from_db(deck_db)
  query = 'SELECT id, name, description, tags, features FROM models'
  models = deck_db.execute(query).map do |anki_id, name, description, tags,
                                          features|
    self.new anki_id, name, description, tags, features
  end
end

Instance Method Details

#add_card_model(card_model) ⇒ Object

:nodoc: private



70
71
72
73
74
75
76
# File 'lib/anki/importer/model.rb', line 70

def add_card_model(card_model)
  if @card_models_by_id[card_model.anki_id]
    raise ArgumentError, 'The CardModel has a duplicate Anki ID.'
  end
  @card_models << card_model
  @card_models_by_id[card_model.anki_id] = card_model
end

#add_fact(fact) ⇒ Object

:nodoc: private



81
82
83
84
85
86
87
# File 'lib/anki/importer/model.rb', line 81

def add_fact(fact)
  if @facts_by_id[fact.anki_id]
    raise ArgumentError, 'The Fact has a duplicate Anki ID.'
  end
  @facts << fact
  @facts_by_id[fact.anki_id] = fact
end

#add_field(field) ⇒ Object

:nodoc: private



59
60
61
62
63
64
65
# File 'lib/anki/importer/model.rb', line 59

def add_field(field)
  if @fields_by_id[field.anki_id]
    raise ArgumentError, 'The Field has a duplicate Anki ID.'
  end
  @fields << field
  @fields_by_id[field.anki_id] = field
end