Class: Ace::Support::Markdown::Models::Section
- Inherits:
-
Object
- Object
- Ace::Support::Markdown::Models::Section
- Defined in:
- lib/ace/support/markdown/models/section.rb
Overview
Immutable representation of a markdown section Contains heading information and content
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#heading ⇒ Object
readonly
Returns the value of attribute heading.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compare sections for equality.
-
#empty? ⇒ Boolean
Check if section is empty (no content).
-
#initialize(heading:, level:, content:, metadata: {}) ⇒ Section
constructor
Create a new Section.
-
#to_h ⇒ Hash
Hash representation.
-
#to_markdown ⇒ String
Convert section to markdown string.
-
#with_content(new_content) ⇒ Section
Create a new Section with updated content.
-
#with_heading(new_heading) ⇒ Section
Create a new Section with updated heading.
-
#with_metadata(new_metadata) ⇒ Section
Create a new Section with updated metadata.
-
#word_count ⇒ Integer
Get word count of section content.
Constructor Details
#initialize(heading:, level:, content:, metadata: {}) ⇒ Section
Create a new Section
17 18 19 20 21 22 23 24 |
# File 'lib/ace/support/markdown/models/section.rb', line 17 def initialize(heading:, level:, content:, metadata: {}) @heading = heading.freeze @level = level.freeze @content = content.freeze @metadata = .freeze validate! end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
10 11 12 |
# File 'lib/ace/support/markdown/models/section.rb', line 10 def content @content end |
#heading ⇒ Object (readonly)
Returns the value of attribute heading.
10 11 12 |
# File 'lib/ace/support/markdown/models/section.rb', line 10 def heading @heading end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
10 11 12 |
# File 'lib/ace/support/markdown/models/section.rb', line 10 def level @level end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
10 11 12 |
# File 'lib/ace/support/markdown/models/section.rb', line 10 def @metadata end |
Instance Method Details
#==(other) ⇒ Boolean
Compare sections for equality
84 85 86 87 88 89 |
# File 'lib/ace/support/markdown/models/section.rb', line 84 def ==(other) other.is_a?(Section) && @heading == other.heading && @level == other.level && @content == other.content end |
#empty? ⇒ Boolean
Check if section is empty (no content)
71 72 73 |
# File 'lib/ace/support/markdown/models/section.rb', line 71 def empty? @content.nil? || @content.strip.empty? end |
#to_h ⇒ Hash
Hash representation
93 94 95 96 97 98 99 100 |
# File 'lib/ace/support/markdown/models/section.rb', line 93 def to_h { heading: @heading, level: @level, content: @content, metadata: @metadata } end |
#to_markdown ⇒ String
Convert section to markdown string
64 65 66 67 |
# File 'lib/ace/support/markdown/models/section.rb', line 64 def to_markdown heading_prefix = "#" * @level "#{heading_prefix} #{@heading}\n\n#{@content}" end |
#with_content(new_content) ⇒ Section
Create a new Section with updated content
29 30 31 32 33 34 35 36 |
# File 'lib/ace/support/markdown/models/section.rb', line 29 def with_content(new_content) Section.new( heading: @heading, level: @level, content: new_content, metadata: @metadata ) end |
#with_heading(new_heading) ⇒ Section
Create a new Section with updated heading
41 42 43 44 45 46 47 48 |
# File 'lib/ace/support/markdown/models/section.rb', line 41 def with_heading(new_heading) Section.new( heading: new_heading, level: @level, content: @content, metadata: @metadata ) end |
#with_metadata(new_metadata) ⇒ Section
Create a new Section with updated metadata
53 54 55 56 57 58 59 60 |
# File 'lib/ace/support/markdown/models/section.rb', line 53 def () Section.new( heading: @heading, level: @level, content: @content, metadata: ) end |
#word_count ⇒ Integer
Get word count of section content
77 78 79 |
# File 'lib/ace/support/markdown/models/section.rb', line 77 def word_count @content.split(/\s+/).length end |