class Prettyrb::Document::Builder

Attributes

parts[R]

Public Class Methods

new(*args) click to toggle source
# File lib/prettyrb/document.rb, line 42
def initialize(*args)
  @parts = args
end

Public Instance Methods

each() click to toggle source
# File lib/prettyrb/document.rb, line 46
def each
  @parts.each
end
groups() click to toggle source
# File lib/prettyrb/document.rb, line 58
def groups
  parts.select { |p| p.is_a?(Group) } + parts.flat_map do |p|
    p.groups if p.respond_to?(:groups)
  end.compact
end
has_group_part?() click to toggle source
# File lib/prettyrb/document.rb, line 54
def has_group_part?
  parts.any? { |p| p.is_a?(Group) }
end
inspect() click to toggle source
# File lib/prettyrb/document.rb, line 50
def inspect
  inspect_children(self, indent_level: 0)
end
max_group_depth() click to toggle source
# File lib/prettyrb/document.rb, line 64
def max_group_depth
  return 0 if !parts
  return @max_group_depth if defined?(@max_group_depth)

  has_groups = parts.any? { |p| p.is_a?(Group) }

  total = if has_groups
    1
  else
    0
  end

  # TODO swap filter/flat_map for single iteration
  nested_total = parts.
    filter { |p| p.respond_to?(:max_group_depth) }.
    flat_map { |p| p.max_group_depth }.
    max

  @max_group_depth = total + (nested_total || 0)
end

Private Instance Methods

inspect_children(builder, indent_level:) click to toggle source
# File lib/prettyrb/document.rb, line 87
def inspect_children(builder, indent_level:)
  if builder.respond_to?(:parts)
    children = if builder.parts
      builder.parts.map do |p|
        inspect_children(p, indent_level: indent_level + 1)
      end.join("\n")
    end

    "  " * indent_level + "(#{builder.class}\n#{"  "*indent_level}#{children}\n #{"  " * indent_level})"
  else
    "  " * indent_level + builder.inspect
  end
end