class Kitchen::NoteElement

An element for a note

Public Class Methods

new(node:, document: nil) click to toggle source

Creates a new NoteElement

@param node [Nokogiri::XML::Node] the node this element wraps @param document [Document] this element's document

Calls superclass method Kitchen::ElementBase::new
# File lib/kitchen/note_element.rb, line 13
def initialize(node:, document: nil)
  super(node: node,
        document: document,
        enumerator_class: NoteElementEnumerator)
end
short_type() click to toggle source

Returns the short type @return [Symbol]

# File lib/kitchen/note_element.rb, line 22
def self.short_type
  :note
end

Public Instance Methods

autogenerated_title() click to toggle source

Get the autogenerated title for this note

@return [String]

# File lib/kitchen/note_element.rb, line 54
def autogenerated_title
  if indicates_autogenerated_title?
    I18n.t(:"notes.#{detected_note_title_key}")
  else
    "unknown title for note with classes #{classes}"
  end
end
indicates_autogenerated_title?() click to toggle source

Returns true if the note's title is autogenerated

@return [Boolean]

# File lib/kitchen/note_element.rb, line 46
def indicates_autogenerated_title?
  detected_note_title_key != 0 && detected_note_title_key.present?
end
title() click to toggle source

Returns the note's title element

@return [Element, nil]

# File lib/kitchen/note_element.rb, line 30
def title
  block_error_if(block_given?)
  note_body = first('div.os-note-body')
  first_child = note_body ? note_body.element_children[0] : element_children[0]

  if first_child[:'data-type'] == 'title'
    first_child
  elsif first_child&.element_children&.[](0)&.[](:'data-type') == 'title'
    first_child.element_children[0]
  end
end

Protected Instance Methods

detected_note_title_key() click to toggle source
# File lib/kitchen/note_element.rb, line 64
def detected_note_title_key
  @detected_note_title_key ||= begin
    return 0 if classes.empty? || !I18n.t('.').key?(:notes)

    possible_keys = I18n.t(:notes).keys.map(&:to_s)
    keys = possible_keys & classes

    raise("too many translation keys: #{keys.join(', ')}") if keys.many?
    return 0 if keys.empty?

    keys.first
  end
end