Class: MiddlemanMdocs::TOC::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/middleman-mdocs/toc.rb

Overview

TOC item representation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Item

Returns a new instance of Item.



12
13
14
15
# File 'lib/middleman-mdocs/toc.rb', line 12

def initialize(attrs = {})
  @attrs = attrs.with_indifferent_access
  @children = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/middleman-mdocs/toc.rb', line 23

def method_missing(sym, *)
  if attrs.include? sym
    @attrs[sym]
  else
    super
  end
end

Instance Attribute Details

#attrsObject

Returns the value of attribute attrs.



8
9
10
# File 'lib/middleman-mdocs/toc.rb', line 8

def attrs
  @attrs
end

#childrenObject

Returns the value of attribute children.



8
9
10
# File 'lib/middleman-mdocs/toc.rb', line 8

def children
  @children
end

Class Method Details

.create(item) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/middleman-mdocs/toc.rb', line 31

def self.create(item)
  Item.new.tap do |composed|
    [item, item.value || {}].each do |obj|
      composed.merge!(obj.try(:attr) || {}).merge!(obj.try(:options) || {})
    end

    (item.value.try(:children) || []).each do |el|
      composed.merge!(create(el).attrs)
    end
  end
end

.parse_kramdown_toc(toc) ⇒ Object



43
44
45
46
47
# File 'lib/middleman-mdocs/toc.rb', line 43

def self.parse_kramdown_toc(toc)
  root = create(toc)
  root.children = toc.children.map { |item| parse_kramdown_toc(item) }
  root
end

Instance Method Details

#flattenObject



17
18
19
20
21
# File 'lib/middleman-mdocs/toc.rb', line 17

def flatten
  children.each_with_object([attrs.empty? ? [] : self]) do |child, list|
    list.append(child.flatten)
  end.flatten
end