Class: Coradoc::AsciiDoc::Model::ContentList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/coradoc/asciidoc/model/content_list.rb

Overview

A list of content elements that provides unified content handling

ContentList handles mixed content types (strings, TextElements, model objects) and provides a consistent API for querying and manipulating content.

Examples:

Create from strings

content = ContentList.new("Hello", "World")
content.text # => "HelloWorld"

Create from mixed types

content = ContentList.new([
  "Hello ",
  Bold.new("World"),
  "!"
])
content.text # => "Hello World!"

Query by type

bold_items = content.find_type(Inline::Bold)

Iterate elements

content.each do |element|
  puts element.class
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*items) ⇒ ContentList

Create a new ContentList

Examples:

From strings

ContentList.new("Hello", "World")

From array

ContentList.new(["Hello", Bold.new("World")])

From nested array

ContentList.new([["Hello", " "], "World"])

Parameters:

  • items (Array, String, Object)

    Content items to initialize with



52
53
54
55
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 52

def initialize(*items)
  @items = normalize(items.flatten)
  freeze
end

Instance Attribute Details

#itemsArray (readonly)

Get the raw items array

Returns:

  • (Array)

    The content items



37
38
39
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 37

def items
  @items
end

Class Method Details

.from(value) ⇒ ContentList

Create a ContentList from a single value

Examples:

From string

ContentList.from("Hello")

From array

ContentList.from(["Hello", "World"])

Parameters:

  • value (Object)

    The content value

Returns:



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 68

def self.from(value)
  case value
  when ContentList
    value
  when Array
    new(*value)
  when nil
    new
  else
    new(value)
  end
end

Instance Method Details

#+(other) ⇒ ContentList

Concatenate another ContentList or array

Examples:

Concatenate

content + ContentList.new("more")

Parameters:

Returns:

  • (ContentList)

    New ContentList with combined items



256
257
258
259
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 256

def +(other)
  other_items = other.is_a?(ContentList) ? other.items : Array(other)
  ContentList.new(*@items, *other_items)
end

#<<(item) ⇒ ContentList

Add an item to the content

Note: This returns a new ContentList since ContentList is immutable

Examples:

Add item

content << "more text"

Parameters:

  • item (Object)

    Item to add

Returns:



103
104
105
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 103

def <<(item)
  ContentList.new(*@items, coerce(item))
end

#==(other) ⇒ Boolean Also known as: eql?

Two ContentLists are equal if their items are equal

Parameters:

  • other (Object)

    Object to compare

Returns:

  • (Boolean)

    true if equal



274
275
276
277
278
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 274

def ==(other)
  return false unless other.is_a?(ContentList)

  @items == other.items
end

#[](index) ⇒ Object?

Get item at index

Parameters:

  • index (Integer)

    Index

Returns:

  • (Object, nil)

    Item at index or nil



154
155
156
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 154

def [](index)
  @items[index]
end

#each {|Object| ... } ⇒ Enumerator

Iterate over content items

Examples:

Iterate

content.each { |item| puts item }

Yields:

  • (Object)

    Each content item

Returns:

  • (Enumerator)

    If no block given



89
90
91
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 89

def each(&block)
  @items.each(&block)
end

#empty?Boolean

Check if content is empty

Returns:

  • (Boolean)

    true if no items



136
137
138
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 136

def empty?
  @items.empty?
end

#find_type(type) ⇒ Array

Find all items of a specific type

Examples:

Find bold items

content.find_type(Inline::Bold)

Parameters:

  • type (Class, Module)

    Type to find

Returns:

  • (Array)

    Items of the specified type



128
129
130
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 128

def find_type(type)
  @items.select { |item| item.is_a?(type) }
end

#firstObject?

Get first item

Returns:

  • (Object, nil)

    First item or nil



162
163
164
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 162

def first
  @items.first
end

#hashInteger

Hash code for use in Hash keys

Returns:

  • (Integer)

    Hash code



285
286
287
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 285

def hash
  @items.hash
end

#include?(item) ⇒ Boolean

Check if content includes an item

Parameters:

  • item (Object)

    Item to check

Returns:

  • (Boolean)

    true if item is in content



232
233
234
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 232

def include?(item)
  @items.include?(item)
end

#include_type?(type) ⇒ Boolean

Check if content includes an item of a type

Examples:

Check for bold

content.include_type?(Inline::Bold)

Parameters:

  • type (Class, Module)

    Type to check

Returns:

  • (Boolean)

    true if any item is of the type



244
245
246
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 244

def include_type?(type)
  @items.any? { |item| item.is_a?(type) }
end

#inspectString

String representation for debugging

Returns:

  • (String)

    Debug string



265
266
267
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 265

def inspect
  "#<#{self.class.name} size=#{@items.size} items=#{@items.inspect}>"
end

#join(sep = '') ⇒ String

Join items with a separator

Examples:

Join with spaces

content.join(" ") # => "Hello World"

Parameters:

  • sep (String) (defaults to: '')

    Separator

Returns:

  • (String)

    Joined string



190
191
192
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 190

def join(sep = '')
  @items.map(&:to_s).join(sep)
end

#lastObject?

Get last item

Returns:

  • (Object, nil)

    Last item or nil



170
171
172
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 170

def last
  @items.last
end

#map {|Object| ... } ⇒ Array

Map over items

Examples:

Map

content.map(&:class) # => [String, Bold, String]

Yields:

  • (Object)

    Each item

Returns:

  • (Array)

    Mapped items



202
203
204
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 202

def map(&block)
  @items.map(&block)
end

#reject {|Object| ... } ⇒ Array

Reject items matching predicate

Yields:

  • (Object)

    Each item

Returns:

  • (Array)

    Remaining items



223
224
225
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 223

def reject(&block)
  @items.reject(&block)
end

#select {|Object| ... } ⇒ Array

Select items matching predicate

Examples:

Select strings

content.select { |i| i.is_a?(String) }

Yields:

  • (Object)

    Each item

Returns:

  • (Array)

    Selected items



214
215
216
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 214

def select(&block)
  @items.select(&block)
end

#sizeInteger Also known as: length

Get number of items

Returns:

  • (Integer)

    Number of items



144
145
146
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 144

def size
  @items.size
end

#textString Also known as: to_str, to_s

Get all items as a plain string

Examples:

Get text

content.text # => "Hello World"

Returns:

  • (String)

    All items joined as string



114
115
116
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 114

def text
  @items.map(&:to_s).join
end

#to_aArray

Convert to array

Returns:

  • (Array)

    Items as array



178
179
180
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 178

def to_a
  @items.dup
end