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, #to_hash

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



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

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 35

#contentString?

Returns text content of the list item.

Returns:

  • (String, nil)

    text content of the list item



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

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., ‘*’, ‘**’, ‘.’, ‘..’, ‘-’)



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

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:



73
74
75
76
77
78
79
80
# File 'lib/coradoc/core_model/list_item.rb', line 73

def self.from_h(hash)
  new(
    marker: hash[:marker],
    content: hash[:content],
    nested: hash[:nested_list],
    children: hash[:children] || []
  )
end

Instance Method Details

#nestedObject

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



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

def nested
  nested_list if respond_to?(:nested_list)
end

#nested=(value) ⇒ Object

Set nested list

Parameters:

  • value (ListBlock, nil)

    nested list block



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

def nested=(value)
  self.nested_list = value if respond_to?(:nested_list=)
end

#semantically_equivalent?(other) ⇒ Boolean

Override semantic equivalence to handle nested structures properly

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/coradoc/core_model/list_item.rb', line 83

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



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

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