Class: Philiprehberger::TomlKit::CommentDocument

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/toml_kit/comment_document.rb

Overview

Represents a TOML document that preserves comments during round-trip parse/serialize operations.

Comments are stored as metadata attached to keys or sections:

- :header_comments -> comments at the top of the document
- :comments -> hash mapping "path.to.key" to { before: [...], inline: "..." }
- :table_comments -> hash mapping "[table]" to { before: [...], inline: "..." }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}, comments: {}, table_comments: {}, header_comments: []) ⇒ CommentDocument

Returns a new instance of CommentDocument.



15
16
17
18
19
20
# File 'lib/philiprehberger/toml_kit/comment_document.rb', line 15

def initialize(data = {}, comments: {}, table_comments: {}, header_comments: [])
  @data = data
  @comments = comments
  @table_comments = table_comments
  @header_comments = header_comments
end

Instance Attribute Details

#commentsObject (readonly)

Returns the value of attribute comments.



13
14
15
# File 'lib/philiprehberger/toml_kit/comment_document.rb', line 13

def comments
  @comments
end

#dataObject (readonly)

Returns the value of attribute data.



13
14
15
# File 'lib/philiprehberger/toml_kit/comment_document.rb', line 13

def data
  @data
end

#header_commentsObject (readonly)

Returns the value of attribute header_comments.



13
14
15
# File 'lib/philiprehberger/toml_kit/comment_document.rb', line 13

def header_comments
  @header_comments
end

#table_commentsObject (readonly)

Returns the value of attribute table_comments.



13
14
15
# File 'lib/philiprehberger/toml_kit/comment_document.rb', line 13

def table_comments
  @table_comments
end

Class Method Details

.parse(input) ⇒ CommentDocument

Parse a TOML string preserving comments.

Parameters:

  • input (String)

    TOML document

Returns:



26
27
28
# File 'lib/philiprehberger/toml_kit/comment_document.rb', line 26

def self.parse(input)
  CommentPreservingParser.new.parse(input)
end

Instance Method Details

#[](key) ⇒ Object

Access the underlying data hash.

Parameters:

  • key (String)

    top-level key

Returns:

  • (Object)

    value



41
42
43
# File 'lib/philiprehberger/toml_kit/comment_document.rb', line 41

def [](key)
  @data[key]
end

#[]=(key, value) ⇒ Object

Set a value in the underlying data hash.

Parameters:

  • key (String)

    top-level key

  • value (Object)

    value to set



49
50
51
# File 'lib/philiprehberger/toml_kit/comment_document.rb', line 49

def []=(key, value)
  @data[key] = value
end

#to_hHash

Return the data hash for compatibility.

Returns:

  • (Hash)


56
57
58
# File 'lib/philiprehberger/toml_kit/comment_document.rb', line 56

def to_h
  @data
end

#to_tomlString

Serialize back to a TOML string with comments preserved.

Returns:

  • (String)

    TOML formatted string with comments



33
34
35
# File 'lib/philiprehberger/toml_kit/comment_document.rb', line 33

def to_toml
  CommentPreservingSerializer.new.serialize(self)
end