Skip to Content Skip to Search

Note represents Anki notes, which are the main thing that Anki Record is intended to create or update.

Methods
M
N
R
S
Included Modules

Class Public methods

new(note_type: nil, deck: nil, anki21_database: nil, data: nil)

Instantiates a note of type note_type and belonging to deck deck

# File lib/anki_record/note/note.rb, line 21
def initialize(note_type: nil, deck: nil, anki21_database: nil, data: nil)
  if note_type && deck
    setup_new_note(note_type:, deck:)
  elsif anki21_database && data
    setup_existing_note(anki21_database:,
                        note_data: data[:note_data], cards_data: data[:cards_data])
  else
    raise ArgumentError
  end
end

Instance Public methods

method_missing(method_name, field_content = nil)

Overrides BasicObject#method_missing and creates “ghost methods”

The ghost methods are the setters and getters for the note field values.

For example, if the note type has a field called “front” then there will be two ghost methods: front and front=.

# File lib/anki_record/note/note.rb, line 45
def method_missing(method_name, field_content = nil)
  raise NoMethodError, "##{method_name} is not defined or a ghost method" unless respond_to_missing? method_name

  method_name = method_name.to_s
  return @field_contents[method_name] unless method_name.end_with?("=")

  @field_contents[method_name.chomp("=")] = field_content
end

respond_to_missing?(method_name, *)

This allows respond_to? to be accurate for the ghost methods created by method_missing.

# File lib/anki_record/note/note.rb, line 56
def respond_to_missing?(method_name, *)
  method_name = method_name.to_s
  if method_name.end_with?("=")
    note_type.snake_case_field_names.include?(method_name.chomp("="))
  else
    note_type.snake_case_field_names.include?(method_name)
  end
end

save()

Saves the note and its cards

# File lib/anki_record/note/note.rb, line 34
def save
  anki21_database.find_note_by(id: @id) ? update_note_in_collection_anki21 : insert_new_note_in_collection_anki21
  true
end

sort_field_value()

Returns the text value of the note’s sfld (sort field) which is determined by the note’s note type

# File lib/anki_record/note/note.rb, line 68
def sort_field_value
  @field_contents[note_type.snake_case_sort_field_name]
end