Class: Anki::Importer::Fact

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

Overview

An association tested by flash cards.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(anki_id, model, tags, created_at, modified_at) ⇒ Fact

:nodoc: private



49
50
51
52
53
54
55
56
# File 'lib/anki/importer/fact.rb', line 49

def initialize(anki_id, model, tags, created_at, modified_at)
  @anki_id = anki_id
  @model = model
  @tags = tags
  @created_at = created_at
  @modified_at = modified_at
  @fields = {}
end

Instance Attribute Details

#anki_idObject (readonly)

Unique ID in the facts table.



19
20
21
# File 'lib/anki/importer/fact.rb', line 19

def anki_id
  @anki_id
end

#created_atObject (readonly)

Fact creation time.



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

def created_at
  @created_at
end

#fieldsObject (readonly)

Maps Fields with their values.



16
17
18
# File 'lib/anki/importer/fact.rb', line 16

def fields
  @fields
end

#modelObject (readonly)

The model that this field belongs to.



21
22
23
# File 'lib/anki/importer/fact.rb', line 21

def model
  @model
end

#modified_atObject (readonly)

Fact modification time.



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

def modified_at
  @modified_at
end

#tagsObject (readonly)

Assigned in the Anki UI. Space-separated string.



9
10
11
# File 'lib/anki/importer/fact.rb', line 9

def tags
  @tags
end

Class Method Details

.from_db(deck_db, deck) ⇒ Object

Reads the 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.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/anki/importer/fact.rb', line 30

def self.from_db(deck_db, deck)
  fact_query = 'SELECT id, modelId, tags, created, modified FROM facts'
  facts = deck_db.execute(fact_query).map do |anki_id, model_id, tags,
                                              t_created, t_modified|
    self.new anki_id, deck.models_by_id[model_id], tags, Time.at(t_created),
             Time.at(t_modified)
  end
  facts_by_id = facts.index_by(&:anki_id)
  
  field = 'SELECT factId, fieldModelId, value FROM fields'
  deck_db.execute(field) do |fact_id, field_model_id, value|
    fact = facts_by_id[fact_id]
    field = deck.fields_by_id[field_model_id]
    fact.fields[field] = value
  end
  facts
end