Class: AnkiRecord::Note

Inherits:
Object
  • Object
show all
Includes:
Helpers::ChecksumHelper, Helpers::SharedConstantsHelper, Helpers::TimeHelper, NoteAttributes
Defined in:
lib/anki_record/note/note.rb

Overview

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

Instance Attribute Summary

Attributes included from NoteAttributes

#anki21_database, #cards, #data, #deck, #field_contents, #flags, #guid, #id, #last_modified_timestamp, #note_type, #tags, #usn

Instance Method Summary collapse

Methods included from Helpers::TimeHelper

#milliseconds_since_epoch, #seconds_since_epoch

Methods included from Helpers::ChecksumHelper

#checksum

Constructor Details

#initialize(note_type: nil, deck: nil, anki21_database: nil, data: nil) ⇒ Note

Instantiates a note of type note_type and belonging to deck deck



21
22
23
24
25
26
27
28
29
30
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, field_content = nil) ⇒ Object

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=.

Raises:

  • (NoMethodError)


45
46
47
48
49
50
51
52
# 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

Instance Method Details

#respond_to_missing?(method_name) ⇒ Boolean

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

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
# 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

#saveObject

Saves the note and its cards



34
35
36
37
# 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_valueObject

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



68
69
70
# File 'lib/anki_record/note/note.rb', line 68

def sort_field_value
  @field_contents[note_type.snake_case_sort_field_name]
end