Class: Coradoc::AsciiDoc::Model::DocumentAttributes

Inherits:
Base
  • Object
show all
Defined in:
lib/coradoc/asciidoc/model/document_attributes.rb

Overview

Container for document-level attributes.

DocumentAttributes holds key-value pairs that define document metadata and configuration options. These are the ‘:key: value` declarations at the top of an AsciiDoc file.

Examples:

Access attributes as hash

attrs = Coradoc::AsciiDoc::Model::DocumentAttributes.new
attrs.data << Coradoc::AsciiDoc::Model::Attribute.new("author", "John Doe")
attrs.to_hash # => {"author" => "John Doe"}

Get specific attribute

value = attrs.get_attribute("author")

Instance Attribute Summary collapse

Attributes inherited from Base

#id

Instance Method Summary collapse

Methods inherited from Base

#block_level?, #inline?, #serialize_content, #simplify_block_content, #to_adoc, #to_h, visit, #visit

Instance Attribute Details

#dataArray<Attribute> (readonly)

Returns Array of attribute key-value pairs.

Returns:

  • (Array<Attribute>)

    Array of attribute key-value pairs



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/coradoc/asciidoc/model/document_attributes.rb', line 23

class DocumentAttributes < Base
  attribute :data, Attribute, collection: true

  def to_hash
    return {} if data.nil?

    data.each_with_object({}) do |attribute, hash|
      hash[attribute.key.to_s] = attribute.value
    end
  end

  def get_attribute(name)
    return nil if data.nil?

    attribute = data.find { |attr| attr.key.to_s == name.to_s }
    attribute&.value
  end
end

Instance Method Details

#get_attribute(name) ⇒ Object



34
35
36
37
38
39
# File 'lib/coradoc/asciidoc/model/document_attributes.rb', line 34

def get_attribute(name)
  return nil if data.nil?

  attribute = data.find { |attr| attr.key.to_s == name.to_s }
  attribute&.value
end

#to_hashObject



26
27
28
29
30
31
32
# File 'lib/coradoc/asciidoc/model/document_attributes.rb', line 26

def to_hash
  return {} if data.nil?

  data.each_with_object({}) do |attribute, hash|
    hash[attribute.key.to_s] = attribute.value
  end
end