Class: AnkiRecord::NoteType
- Inherits:
-
Object
- Object
- AnkiRecord::NoteType
- Defined in:
- lib/anki_record/note_type/note_type.rb
Overview
NoteType represents an Anki note type (also called a model).
Constant Summary collapse
- NOTE_TYPES_WITHOUT_TAGS_AND_VERS_VALUES =
["Basic", "Basic (and reversed card)", "Basic (optional reversed card)", "Basic (type in the answer)"].freeze
Instance Attribute Summary
Attributes included from NoteTypeAttributes
#anki21_database, #card_templates, #cloze, #css, #deck_id, #id, #last_modified_timestamp, #latex_postamble, #latex_preamble, #latex_svg, #name, #note_fields, #req, #sort_field, #tags, #usn, #vers
Instance Method Summary collapse
-
#add_card_template(card_template) ⇒ Object
:nodoc:.
-
#add_note_field(note_field) ⇒ Object
:nodoc:.
-
#allowed_card_template_answer_format_field_names ⇒ Object
Returns allowed field_name values in {field_name} in the answer format.
-
#allowed_card_template_question_format_field_names ⇒ Object
Returns allowed field_name values in {field_name} in the question format.
-
#field_names_in_order ⇒ Object
Returns an array of the note type’s fields’ names ordered by field ordinal values.
-
#find_card_template_by(name:) ⇒ Object
Returns the note type object’s card template with name
nameor nil if it is not found. -
#initialize(anki21_database:, name: nil, args: nil) ⇒ NoteType
constructor
Instantiates a new note type belonging to
anki21_databasewith namename. -
#save ⇒ Object
Saves the note type to the collection.anki21 database.
-
#snake_case_field_names ⇒ Object
:nodoc:.
-
#snake_case_sort_field_name ⇒ Object
:nodoc:.
-
#sort_field_name ⇒ Object
Returns the name of the note type’s field used to sort notes in the browser.
-
#to_h ⇒ Object
:nodoc:.
Methods included from NoteTypeAttributes
Methods included from Helpers::TimeHelper
#milliseconds_since_epoch, #seconds_since_epoch
Constructor Details
#initialize(anki21_database:, name: nil, args: nil) ⇒ NoteType
Instantiates a new note type belonging to anki21_database with name name
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# 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 Method Details
#add_card_template(card_template) ⇒ Object
:nodoc:
102 103 104 105 106 |
# File 'lib/anki_record/note_type/note_type.rb', line 102 def add_card_template(card_template) # :nodoc: raise ArgumentError unless card_template.instance_of?(AnkiRecord::CardTemplate) @card_templates << card_template end |
#add_note_field(note_field) ⇒ Object
:nodoc:
96 97 98 99 100 |
# File 'lib/anki_record/note_type/note_type.rb', line 96 def add_note_field(note_field) # :nodoc: raise ArgumentError unless note_field.instance_of?(AnkiRecord::NoteField) @note_fields << note_field end |
#allowed_card_template_answer_format_field_names ⇒ Object
Returns allowed field_name values in {field_name} in the answer format.
92 93 94 |
# 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 ⇒ Object
Returns allowed field_name values in {field_name} in the question format.
85 86 87 88 |
# 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 ⇒ Object
Returns an array of the note type’s fields’ names ordered by field ordinal values.
65 66 67 |
# 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:) ⇒ Object
Returns the note type object’s card template with name name or nil if it is not found
59 60 61 |
# 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 ⇒ Object
Saves the note type to the collection.anki21 database
40 41 42 43 44 45 |
# 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 |
#snake_case_field_names ⇒ Object
:nodoc:
69 70 71 |
# File 'lib/anki_record/note_type/note_type.rb', line 69 def snake_case_field_names # :nodoc: field_names_in_order.map { |field_name| field_name.downcase.tr(" ", "_") } end |
#snake_case_sort_field_name ⇒ Object
:nodoc:
79 80 81 |
# File 'lib/anki_record/note_type/note_type.rb', line 79 def snake_case_sort_field_name # :nodoc: sort_field_name.downcase.tr(" ", "_") end |
#sort_field_name ⇒ Object
Returns the name of the note type’s field used to sort notes in the browser.
75 76 77 |
# 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 |
#to_h ⇒ Object
:nodoc:
47 48 49 50 51 52 53 54 55 |
# File 'lib/anki_record/note_type/note_type.rb', line 47 def to_h # :nodoc: self_to_h = { id: @id, name: @name, type: @cloze ? 1 : 0, mod: @last_modified_timestamp, usn: @usn, sortf: @sort_field, did: @deck_id, tmpls: @card_templates.map(&:to_h), flds: @note_fields.map(&:to_h), css: @css, latexPre: @latex_preamble, latexPost: @latex_postamble, latexsvg: @latex_svg, req: @req } self_to_h.merge({ tags: @tags, vers: @vers }) unless NOTE_TYPES_WITHOUT_TAGS_AND_VERS_VALUES.include?(@name) self_to_h end |