Class: Ace::Support::Markdown::Models::Section

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(heading:, level:, content:, metadata: {}) ⇒ Section

Create a new Section

Parameters:

  • heading (String)

    The section heading text

  • level (Integer)

    The heading level (1-6)

  • content (String)

    The section content (without heading)

  • metadata (Hash) (defaults to: {})

    Optional metadata about the 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

#contentObject (readonly)

Returns the value of attribute content.



10
11
12
# File 'lib/ace/support/markdown/models/section.rb', line 10

def content
  @content
end

#headingObject (readonly)

Returns the value of attribute heading.



10
11
12
# File 'lib/ace/support/markdown/models/section.rb', line 10

def heading
  @heading
end

#levelObject (readonly)

Returns the value of attribute level.



10
11
12
# File 'lib/ace/support/markdown/models/section.rb', line 10

def level
  @level
end

#metadataObject (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

Parameters:

Returns:

  • (Boolean)


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)

Returns:

  • (Boolean)


71
72
73
# File 'lib/ace/support/markdown/models/section.rb', line 71

def empty?
  @content.nil? || @content.strip.empty?
end

#to_hHash

Hash representation

Returns:

  • (Hash)


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_markdownString

Convert section to markdown string

Returns:

  • (String)

    The complete section as markdown



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

Parameters:

  • new_content (String)

    The new content

Returns:

  • (Section)

    New Section instance



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

Parameters:

  • new_heading (String)

    The new heading

Returns:

  • (Section)

    New Section instance



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

Parameters:

  • new_metadata (Hash)

    The new metadata

Returns:

  • (Section)

    New Section instance



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_countInteger

Get word count of section content

Returns:

  • (Integer)


77
78
79
# File 'lib/ace/support/markdown/models/section.rb', line 77

def word_count
  @content.split(/\s+/).length
end