Skip to Content Skip to Search

NoteType represents an Anki note type (also called a model).

Methods
A
F
N
S
Included Modules

Constants

NOTE_TYPES_WITHOUT_TAGS_AND_VERS_VALUES = ["Basic", "Basic (and reversed card)", "Basic (optional reversed card)", "Basic (type in the answer)"].freeze
 

Class Public methods

new(anki21_database:, name: nil, args: nil)

Instantiates a new note type belonging to anki21_database with name name

# File lib/anki_record/note_type/note_type.rb, line 24
def initialize(anki21_database:, name: nil, args: nil)
  raise ArgumentError unless (name && args.nil?) || (args && args["name"])

  @anki21_database = anki21_database

  if args
    setup_note_type_instance_variables_from_existing(args:)
  else
    setup_instance_variables_for_new_note_type(name:)
  end

  @anki21_database.add_note_type self
end

Instance Public methods

allowed_card_template_answer_format_field_names()

Returns allowed field_name values in {{field_name}} in the answer format.

# File lib/anki_record/note_type/note_type.rb, line 92
def allowed_card_template_answer_format_field_names
  allowed_card_template_question_format_field_names + ["FrontSide"]
end

allowed_card_template_question_format_field_names()

Returns allowed field_name values in {{field_name}} in the question format.

# File lib/anki_record/note_type/note_type.rb, line 85
def allowed_card_template_question_format_field_names
  allowed = field_names_in_order
  cloze ? allowed + field_names_in_order.map { |field_name| "cloze:#{field_name}" } : allowed
end

field_names_in_order()

Returns an array of the note type’s fields’ names ordered by field ordinal values.

# File lib/anki_record/note_type/note_type.rb, line 65
def field_names_in_order
  @note_fields.sort_by(&:ordinal_number).map(&:name)
end

find_card_template_by(name:)

Returns the note type object’s card template with name name or nil if it is not found

# File lib/anki_record/note_type/note_type.rb, line 59
def find_card_template_by(name:)
  card_templates.find { |template| template.name == name }
end

save()

Saves the note type to the collection.anki21 database

# File lib/anki_record/note_type/note_type.rb, line 40
def save
  collection_models_hash = anki21_database.models_json
  collection_models_hash[@id] = to_h
  sql = "update col set models = ? where id = ?"
  anki21_database.prepare(sql).execute([JSON.generate(collection_models_hash), anki21_database.collection.id])
end

sort_field_name()

Returns the name of the note type’s field used to sort notes in the browser.

# File lib/anki_record/note_type/note_type.rb, line 75
def sort_field_name
  @note_fields.find { |field| field.ordinal_number == @sort_field }&.name
end