Class: NSWTopo::TreeIndenter

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/nswtopo/tree_indenter.rb

Instance Method Summary collapse

Constructor Details

#initialize(items, parts = nil, &block) ⇒ TreeIndenter

Returns a new instance of TreeIndenter.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/nswtopo/tree_indenter.rb', line 3

def initialize(items, parts = nil, &block)
  @enum = Enumerator.new do |yielder|
    next unless items
    grouped = block_given? ? block.call(items) : items
    grouped.each.with_index do |(item, group), index|
      *new_parts, last_part = parts
      case last_part
      when "├─ " then new_parts << ""
      when "└─ " then new_parts << "   "
      end
      new_parts << case index
      when grouped.size - 1 then "└─ "
      else                       "├─ "
      end if parts
      yielder << [new_parts, item]
      TreeIndenter.new(group, new_parts, &block).each(&yielder)
    end
  end
end