Class: RailroadDiagrams::Sequence

Inherits:
DiagramMultiContainer show all
Defined in:
lib/railroad_diagrams/sequence.rb

Instance Attribute Summary

Attributes inherited from DiagramItem

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

Instance Method Summary collapse

Methods inherited from DiagramMultiContainer

#to_str, #walk

Methods inherited from DiagramItem

#add, #to_str, #walk, #write_svg

Constructor Details

#initialize(*items) ⇒ Sequence

Returns a new instance of Sequence.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/railroad_diagrams/sequence.rb', line 5

def initialize(*items)
  super('g', items)
  @needs_space = false
  @up = 0
  @down = 0
  @height = 0
  @width = 0
  @items.each do |item|
    @width += item.width + (item.needs_space ? 20 : 0)
    @up = [@up, item.up - @height].max
    @height += item.height
    @down = [@down - item.height, item.down].max
  end
  @width -= 10 if @items[0].needs_space
  @width -= 10 if @items[-1].needs_space
end

Instance Method Details

#format(x, y, width) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/railroad_diagrams/sequence.rb', line 27

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
  @items.each_with_index do |item, i|
    if item.needs_space && i.positive?
      Path.new(x, y).h(10).add(self)
      x += 10
    end
    item.format(x, y, item.width).add(self)
    x += item.width
    y += item.height
    if item.needs_space && i < @items.length - 1
      Path.new(x, y).h(10).add(self)
      x += 10
    end
  end
  self
end

#text_diagramObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/railroad_diagrams/sequence.rb', line 48

def text_diagram
  separator, = TextDiagram.get_parts(['separator'])
  diagram_td = TextDiagram.new(0, 0, [''])
  @items.each do |item|
    item_td = item.text_diagram
    item_td = item_td.expand(1, 1, 0, 0) if item.needs_space
    diagram_td = diagram_td.append_right(item_td, separator)
  end
  diagram_td
end

#to_sObject



22
23
24
25
# File 'lib/railroad_diagrams/sequence.rb', line 22

def to_s
  items = @items.map(&:to_s).join(', ')
  "Sequence(#{items})"
end