Class: Prettyrb::Document::Builder

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/prettyrb/document.rb

Direct Known Subclasses

Concat, Dedent, Group, Hardline, Indent, Join, Softline

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Builder

Returns a new instance of Builder.



42
43
44
# File 'lib/prettyrb/document.rb', line 42

def initialize(*args)
  @parts = args
end

Instance Attribute Details

#partsObject (readonly)

Returns the value of attribute parts.



38
39
40
# File 'lib/prettyrb/document.rb', line 38

def parts
  @parts
end

Instance Method Details

#eachObject



46
47
48
# File 'lib/prettyrb/document.rb', line 46

def each
  @parts.each
end

#groupsObject



58
59
60
61
62
# 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?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/prettyrb/document.rb', line 54

def has_group_part?
  parts.any? { |p| p.is_a?(Group) }
end

#inspectObject



50
51
52
# File 'lib/prettyrb/document.rb', line 50

def inspect
  inspect_children(self, indent_level: 0)
end

#max_group_depthObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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