Note
represents Anki notes, which are the main thing that Anki Record is intended to create or update.
Class Public methods
new(note_type: nil, deck: nil, anki21_database: nil, data: nil) Link
Instantiates a note of type note_type
and belonging to deck deck
Source: show
# 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) Link
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=.
Source: show
# 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, *) Link
This allows respond_to? to be accurate for the ghost methods created by method_missing
.
Source: show
# 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