Class: Anki::Importer::CardModel

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

Overview

Schema for Anki cards.

Each model has multiple card models, and card is generated by exactly one model, from one fact.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(anki_id, model, name, description, active, question_template, answer_template, question_style, answer_style, answer_field) ⇒ CardModel

:nodoc: private



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/anki/importer/card_model.rb', line 83

def initialize(anki_id, model, name, description, active, question_template,
               answer_template, question_style, answer_style, answer_field)
  @anki_id = anki_id
  @model = model
  @name = name
  @description = description
  @active = active
  @question_template = question_template
  @answer_template = answer_template
  @question_style = question_style
  @answer_style = answer_style
  @answer_field = answer_field
end

Instance Attribute Details

#activeObject (readonly)

True if the model is cards are generated based on this model.



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

def active
  @active
end

#anki_idObject (readonly)

Unique ID in the fields table.



48
49
50
# File 'lib/anki/importer/card_model.rb', line 48

def anki_id
  @anki_id
end

#answer_fieldObject (readonly)

The field that the user’s answer is checked against.

If this is true, the user is asked to type an answer in the query stage, and their answer is checked against the field value here.



45
46
47
# File 'lib/anki/importer/card_model.rb', line 45

def answer_field
  @answer_field
end

#answer_styleObject (readonly)

Hash with the following keys: :font_family, :font_size, :color, :text_align.



34
35
36
# File 'lib/anki/importer/card_model.rb', line 34

def answer_style
  @answer_style
end

#answer_templateObject (readonly)

Shown during the answer stage.

%(field_name)s inlines field values. This isn’t necessarily related what the user’s answer is verified against.



28
29
30
# File 'lib/anki/importer/card_model.rb', line 28

def answer_template
  @answer_template
end

#descriptionObject (readonly)

Generally empty.



14
15
16
# File 'lib/anki/importer/card_model.rb', line 14

def description
  @description
end

#modelObject (readonly)

The model that this field belongs to.



50
51
52
# File 'lib/anki/importer/card_model.rb', line 50

def model
  @model
end

#nameObject (readonly)

Name assigned in the Anki UI.



12
13
14
# File 'lib/anki/importer/card_model.rb', line 12

def name
  @name
end

#question_in_answerObject (readonly)

If true, the question is showed in the answer stage.

Otherwise, the answer presumably contains the question.



39
40
41
# File 'lib/anki/importer/card_model.rb', line 39

def question_in_answer
  @question_in_answer
end

#question_styleObject (readonly)

Hash with the following keys: :font_family, :font_size, :color, :text_align.



31
32
33
# File 'lib/anki/importer/card_model.rb', line 31

def question_style
  @question_style
end

#question_templateObject (readonly)

Shown during the query stage and maybe during the answer stage.

%(field_name)s inlines field values.



22
23
24
# File 'lib/anki/importer/card_model.rb', line 22

def question_template
  @question_template
end

Class Method Details

.from_db(deck_db, deck) ⇒ Object

Reads the card models from an Anki deck.

Args:

deck_db:: a Sqlite3::Datbase
deck: the (under construction) Anki::Importer::Deck for deck_db

Returns an array of Field instances.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/anki/importer/card_model.rb', line 59

def self.from_db(deck_db, deck)
  query = 'SELECT id, modelId, name, description, active, qformat, aformat, questionInAnswer, questionFontFamily, questionFontSize, questionFontColour, questionAlign, answerFontFamily, answerFontSize, answerFontColour, answerAlign, typeAnswer FROM cardModels ORDER BY ordinal'
  models = deck_db.execute(query).map do |anki_id, model_id, name,
      description, active, question_template, answer_template,
      question_in_answer, question_font_family, question_font_size,
      question_font_color, question_align, answer_font_family,
      answer_font_size, answer_font_color, answer_align, answer_field_name|
    
    model = deck.models_by_id[model_id]
    question_style = { :font_family => question_font_family,
        :font_size => question_font_size, :color => question_font_color,
        :text_align => question_align }
    answer_style = { :font_family => answer_font_family,
        :font_size => answer_font_size, :color => answer_font_color,
        :text_align => answer_align }
    answer_field = model.fields.find { |f| f.name == answer_field_name }
    
    self.new anki_id, model, name, description, active == 1,
             question_template, answer_template, question_style, answer_style,
             answer_field
  end
end