Class: RailroadDiagrams::Group

Inherits:
DiagramItem show all
Defined in:
lib/railroad_diagrams/group.rb

Instance Attribute Summary

Attributes inherited from DiagramItem

#attrs, #children, #down, #height, #needs_space, #up, #width

Instance Method Summary collapse

Methods inherited from DiagramItem

#add, #to_str, #write_svg

Constructor Details

#initialize(item, label = nil) ⇒ Group

Returns a new instance of Group.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/railroad_diagrams/group.rb', line 5

def initialize(item, label = nil)
  super('g')
  @item = wrap_string(item)

  @label =
    if label.is_a?(DiagramItem)
      label
    elsif label
      Comment.new(label)
    end

  item_width = @item.width + (@item.needs_space ? 20 : 0)
  label_width = @label ? @label.width : 0
  @width = [item_width, label_width, AR * 2].max

  @height = @item.height

  @box_up = [@item.up + VS, AR].max
  @up = @box_up
  @up += @label.up + @label.height + @label.down if @label

  @down = [@item.down + VS, AR].max

  @needs_space = true
end

Instance Method Details

#format(x, y, width) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/railroad_diagrams/group.rb', line 35

def format(x, y, width)
  left_gap, right_gap = determine_gaps(width, @width)
  Path.new(x, y).h(left_gap).add(self)
  Path.new(x + left_gap + @width, y + @height).h(right_gap).add(self)
  x += left_gap

  DiagramItem.new(
    'rect',
    attrs: {
      'x' => x,
      'y' => y - @box_up,
      'width' => @width,
      'height' => @height + @box_up + @down,
      'rx' => AR,
      'ry' => AR,
      'class' => 'group-box'
    }
  ).add(self)

  @item.format(x, y, @width).add(self)
  @label&.format(x, y - (@box_up + @label.down + @label.height), @width)&.add(self)

  self
end

#text_diagramObject



66
67
68
69
70
71
72
73
# File 'lib/railroad_diagrams/group.rb', line 66

def text_diagram
  diagram_td = TextDiagram.round_rect(@item.text_diagram, dashed: true)
  if @label
    label_td = @label.text_diagram
    diagram_td = label_td.append_below(diagram_td, [], move_entry: true, move_exit: true).expand(0, 0, 1, 0)
  end
  diagram_td
end

#to_sObject



31
32
33
# File 'lib/railroad_diagrams/group.rb', line 31

def to_s
  "Group(#{@item}, label=#{@label})"
end

#walk(callback) ⇒ Object



60
61
62
63
64
# File 'lib/railroad_diagrams/group.rb', line 60

def walk(callback)
  callback.call(self)
  item.walk(callback)
  label&.walk(callback)
end