Class: Coradoc::CoreModel::ListItem

Inherits:
Base
  • Object
show all
Includes:
ChildrenContent
Defined in:
lib/coradoc/core_model/list_item.rb,
lib/coradoc/core_model/list_block.rb,
lib/coradoc/core_model/list_block.rb

Overview

Re-open ListItem to properly type nested_list now that ListBlock is defined

Instance Attribute Summary collapse

Attributes inherited from Base

#element_attributes, #id, #metadata_entries, #title

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ChildrenContent

#flat_text, included, #renderable_content

Methods inherited from Base

#accept, #attr, #metadata, #set_attr, #set_metadata

Constructor Details

#initialize(args = {}) ⇒ ListItem

Initialize with optional nested structure support

Parameters:

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

    initialization arguments



41
42
43
44
45
# File 'lib/coradoc/core_model/list_item.rb', line 41

def initialize(args = {})
  # Support :nested as alias for :nested_list
  args[:nested_list] = args.delete(:nested) if args.key?(:nested)
  super
end

Instance Attribute Details

#childrenArray

Returns array of attached blocks/elements (mixed content, via ChildrenContent).

Returns:

  • (Array)

    array of attached blocks/elements (mixed content, via ChildrenContent)



# File 'lib/coradoc/core_model/list_item.rb', line 36

#contentString?

Returns text content of the list item.

Returns:

  • (String, nil)

    text content of the list item



34
# File 'lib/coradoc/core_model/list_item.rb', line 34

attribute :content, :string

#markerString?

Returns the marker character(s) for this item (e.g., ‘*’, ‘**’, ‘.’, ‘..’, ‘-’).

Returns:

  • (String, nil)

    the marker character(s) for this item (e.g., ‘*’, ‘**’, ‘.’, ‘..’, ‘-’)



30
# File 'lib/coradoc/core_model/list_item.rb', line 30

attribute :marker, :string

#nested_listListBlock?

Note: Typed as string initially, retyped after ListBlock defined

Returns:

  • (ListBlock, nil)

    nested list within this item



40
# File 'lib/coradoc/core_model/list_block.rb', line 40

attribute :nested_list, :string

Class Method Details

.from_h(hash) ⇒ ListItem

Create from hash

Parameters:

  • hash (Hash)

    hash representation

Returns:



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/coradoc/core_model/list_item.rb', line 74

def self.from_h(hash)
  raw_children = hash[:children] || []
  children = raw_children.map do |child|
    if child.is_a?(Hash) && child.key?(:text)
      CoreModel::TextContent.new(text: child[:text])
    elsif child.is_a?(CoreModel::Base)
      child
    end
  end.compact

  new(
    marker: hash[:marker],
    content: hash[:content],
    nested: hash[:nested_list],
    children: children
  )
end

Instance Method Details

#nestedObject

Delegate nested to nested_list (lutaml attribute added by list_block.rb)



48
49
50
# File 'lib/coradoc/core_model/list_item.rb', line 48

def nested
  nested_list
end

#nested=(value) ⇒ Object

Set nested list

Parameters:

  • value (ListBlock, nil)

    nested list block



54
55
56
# File 'lib/coradoc/core_model/list_item.rb', line 54

def nested=(value)
  self.nested_list = value
end

#semantically_equivalent?(other) ⇒ Boolean

Override semantic equivalence to handle nested structures properly

Returns:

  • (Boolean)


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/coradoc/core_model/list_item.rb', line 93

def semantically_equivalent?(other)
  return false unless other.is_a?(self.class)
  return false unless content == other.content

  # Compare nested lists if present
  if nested || other.nested
    return false if nested.nil? != other.nested.nil?
    return false if nested && !lists_equivalent?(nested, other.nested)
  end

  # Compare children if present
  if children || other.children
    return false if children.nil? != other.children.nil?
    return false if children && !arrays_equivalent?(children, other.children)
  end

  true
end

#to_hHash

Convert to hash representation

Returns:

  • (Hash)

    hash representation of the list item



61
62
63
64
65
66
67
68
# File 'lib/coradoc/core_model/list_item.rb', line 61

def to_h
  {
    marker: marker,
    content: content,
    nested_list: nested&.to_h,
    children: children&.map { |child| child.is_a?(CoreModel::TextContent) ? { text: child.text } : child.to_h }
  }.compact
end