Class: MiddlemanMdocs::TOC::Item
- Inherits:
-
Object
- Object
- MiddlemanMdocs::TOC::Item
- Defined in:
- lib/middleman-mdocs/toc.rb
Overview
TOC item representation
Instance Attribute Summary collapse
-
#attrs ⇒ Object
Returns the value of attribute attrs.
-
#children ⇒ Object
Returns the value of attribute children.
Class Method Summary collapse
Instance Method Summary collapse
- #flatten ⇒ Object
-
#initialize(attrs = {}) ⇒ Item
constructor
A new instance of Item.
- #method_missing(sym) ⇒ Object
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
#attrs ⇒ Object
Returns the value of attribute attrs.
8 9 10 |
# File 'lib/middleman-mdocs/toc.rb', line 8 def attrs @attrs end |
#children ⇒ Object
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 |
Instance Method Details
#flatten ⇒ Object
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 |