Class: Philiprehberger::TomlKit::CommentDocument
- Inherits:
-
Object
- Object
- Philiprehberger::TomlKit::CommentDocument
- 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
-
#comments ⇒ Object
readonly
Returns the value of attribute comments.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#header_comments ⇒ Object
readonly
Returns the value of attribute header_comments.
-
#table_comments ⇒ Object
readonly
Returns the value of attribute table_comments.
Class Method Summary collapse
-
.parse(input) ⇒ CommentDocument
Parse a TOML string preserving comments.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Access the underlying data hash.
-
#[]=(key, value) ⇒ Object
Set a value in the underlying data hash.
-
#initialize(data = {}, comments: {}, table_comments: {}, header_comments: []) ⇒ CommentDocument
constructor
A new instance of CommentDocument.
-
#to_h ⇒ Hash
Return the data hash for compatibility.
-
#to_toml ⇒ String
Serialize back to a TOML string with comments preserved.
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
#comments ⇒ Object (readonly)
Returns the value of attribute comments.
13 14 15 |
# File 'lib/philiprehberger/toml_kit/comment_document.rb', line 13 def comments @comments end |
#data ⇒ Object (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_comments ⇒ Object (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_comments ⇒ Object (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.
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.
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.
49 50 51 |
# File 'lib/philiprehberger/toml_kit/comment_document.rb', line 49 def []=(key, value) @data[key] = value end |
#to_h ⇒ Hash
Return the data hash for compatibility.
56 57 58 |
# File 'lib/philiprehberger/toml_kit/comment_document.rb', line 56 def to_h @data end |
#to_toml ⇒ String
Serialize back to a TOML string with comments preserved.
33 34 35 |
# File 'lib/philiprehberger/toml_kit/comment_document.rb', line 33 def to_toml CommentPreservingSerializer.new.serialize(self) end |