Class: Coradoc::AsciiDoc::Model::ContentList
- Inherits:
-
Object
- Object
- Coradoc::AsciiDoc::Model::ContentList
- 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.
Instance Attribute Summary collapse
-
#items ⇒ Array
readonly
Get the raw items array.
Class Method Summary collapse
-
.from(value) ⇒ ContentList
Create a ContentList from a single value.
Instance Method Summary collapse
-
#+(other) ⇒ ContentList
Concatenate another ContentList or array.
-
#<<(item) ⇒ ContentList
Add an item to the content.
-
#==(other) ⇒ Boolean
(also: #eql?)
Two ContentLists are equal if their items are equal.
-
#[](index) ⇒ Object?
Get item at index.
-
#each {|Object| ... } ⇒ Enumerator
Iterate over content items.
-
#empty? ⇒ Boolean
Check if content is empty.
-
#find_type(type) ⇒ Array
Find all items of a specific type.
-
#first ⇒ Object?
Get first item.
-
#hash ⇒ Integer
Hash code for use in Hash keys.
-
#include?(item) ⇒ Boolean
Check if content includes an item.
-
#include_type?(type) ⇒ Boolean
Check if content includes an item of a type.
-
#initialize(*items) ⇒ ContentList
constructor
Create a new ContentList.
-
#inspect ⇒ String
String representation for debugging.
-
#join(sep = '') ⇒ String
Join items with a separator.
-
#last ⇒ Object?
Get last item.
-
#map {|Object| ... } ⇒ Array
Map over items.
-
#reject {|Object| ... } ⇒ Array
Reject items matching predicate.
-
#select {|Object| ... } ⇒ Array
Select items matching predicate.
-
#size ⇒ Integer
(also: #length)
Get number of items.
-
#text ⇒ String
(also: #to_str, #to_s)
Get all items as a plain string.
-
#to_a ⇒ Array
Convert to array.
Constructor Details
#initialize(*items) ⇒ ContentList
Create a new ContentList
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
#items ⇒ Array (readonly)
Get the raw items array
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
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
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
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
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
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
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
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
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 |
#first ⇒ Object?
Get first item
162 163 164 |
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 162 def first @items.first end |
#hash ⇒ Integer
Hash code for use in Hash keys
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
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
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 |
#inspect ⇒ String
String representation for debugging
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
190 191 192 |
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 190 def join(sep = '') @items.map(&:to_s).join(sep) end |
#last ⇒ Object?
Get last item
170 171 172 |
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 170 def last @items.last end |
#map {|Object| ... } ⇒ Array
Map over 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
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
214 215 216 |
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 214 def select(&block) @items.select(&block) end |
#size ⇒ Integer Also known as: length
Get number of items
144 145 146 |
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 144 def size @items.size end |
#text ⇒ String Also known as: to_str, to_s
Get all items as a plain string
114 115 116 |
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 114 def text @items.map(&:to_s).join end |
#to_a ⇒ Array
Convert to array
178 179 180 |
# File 'lib/coradoc/asciidoc/model/content_list.rb', line 178 def to_a @items.dup end |