Class: RailroadDiagrams::Start

Inherits:
DiagramItem show all
Defined in:
lib/railroad_diagrams/start.rb,
sig/generated/railroad_diagrams/start.rbs

Overview

The start marker of a diagram.

Examples:

Start.new('simple')

Instance Attribute Summary

Attributes inherited from DiagramItem

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

Instance Method Summary collapse

Methods inherited from DiagramItem

#==, #add, #apply_user_attributes, #container_element?, #determine_gaps, #each_node, #hash, inherited, #inspect, #structural_state, #to_str, #walk, #wrap_string, #write_children, #write_opening_tag, #write_standalone, #write_svg

Constructor Details

#initialize(type = 'simple', label: nil, id: nil, cls: nil, attrs: {}) ⇒ Start

Returns a new instance of Start.

Parameters:

  • type (String) (defaults to: 'simple')
  • label: (String, nil) (defaults to: nil)
  • id: (Object) (defaults to: nil)
  • cls: (Object) (defaults to: nil)
  • attrs: (Object) (defaults to: {})


12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/railroad_diagrams/start.rb', line 12

def initialize(type = 'simple', label: nil, id: nil, cls: nil, attrs: {})
  super('g', id: id, cls: cls, data_attrs: attrs)
  label = label.to_s if label
  @width =
    if label
      [20, (Unicode::DisplayWidth.of(label) * CHAR_WIDTH) + 10].max
    else
      20
    end
  @up = 10
  @down = 10
  @type = type
  @label = label
end

Instance Method Details

#child_nodes ⇒ Object

Returns:

  • (Object)


106
107
108
# File 'lib/railroad_diagrams/start.rb', line 106

def child_nodes
  []
end

#format(x, y, _width) ⇒ Start

Parameters:

  • x (Numeric)
  • y (Numeric)
  • _width (Object)

Returns:



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

def format(x, y, _width)
  path = Path.new(x, y - 10)
  if @type == 'complex'
    path.down(20).m(0, -10).right(@width).add(self)
  else
    path.down(20).m(10, -20).down(20).m(-10, -10).right(@width).add(self)
  end
  if @label
    DiagramItem.new(
      'text',
      attrs: {
        'x' => x,
        'y' => y - 15,
        'style' => 'text-anchor:start'
      },
      text: @label
    ).add(self)
  end
  self
end

#measure(context) ⇒ Object

Parameters:

  • context (Object)

Returns:

  • (Object)


76
77
78
79
# File 'lib/railroad_diagrams/start.rb', line 76

def measure(context)
  width = @label ? [20, context.text_width(@label, :label) + 10].max : 20
  Metrics.new(width: width, up: 10, height: 0, down: 10, needs_space: false)
end

#render_svg(context, x, y, _width) ⇒ Object

Parameters:

  • context (Object)
  • x (Object)
  • y (Object)
  • _width (Object)

Returns:

  • (Object)


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/railroad_diagrams/start.rb', line 81

def render_svg(context, x, y, _width)
  width = context.metrics(self).width
  path = Svg::PathData.new(x, y - 10, arc_radius: context.options.arc_radius).v(20)
  if @type == 'complex'
    path.m(0, -10).h(width)
  else
    path.m(10, -20).v(20).m(-10, -10).h(width)
  end
  group = Svg::Element.new('g', @attrs.dup)
  group << Svg::Element.new('path', { 'd' => path }, self_closing: true)
  if @label
    text = Svg::Element.new('text', { 'x' => x, 'y' => y - 15, 'style' => 'text-anchor:start' })
    group << (text << Svg::TextNode.new(@label))
  end
  group
end

#render_text(context) ⇒ Object

Parameters:

  • context (Object)

Returns:

  • (Object)


98
99
100
101
102
103
104
# File 'lib/railroad_diagrams/start.rb', line 98

def render_text(context)
  cross, line, tee_right = context.parts.values_at('cross', 'line', 'tee_right')
  start = @type == 'simple' ? tee_right + cross + line : tee_right + line
  label = TextDiagram.new(0, 0, @label ? [@label] : [])
  start = TextDiagram.pad_r(start, [label.width, Unicode::DisplayWidth.of(start)].max, line) if @label
  label.append_below(TextDiagram.new(0, 0, [start]), [], move_entry: true, move_exit: true)
end

#text_diagram ⇒ TextDiagram

Returns:

  • (TextDiagram)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/railroad_diagrams/start.rb', line 58

def text_diagram
  cross, line, tee_right = TextDiagram.get_parts(%w[cross line tee_right])
  start =
    if @type == 'simple'
      tee_right + cross + line
    else
      tee_right + line
    end

  label_td = TextDiagram.new(0, 0, [])
  if @label
    label_td = TextDiagram.new(0, 0, [@label])
    start = TextDiagram.pad_r(start, [label_td.width, Unicode::DisplayWidth.of(start)].max, line)
  end
  start_td = TextDiagram.new(0, 0, [start])
  label_td.append_below(start_td, [], move_entry: true, move_exit: true)
end

#to_s ⇒ String

Returns:

  • (String)


28
29
30
# File 'lib/railroad_diagrams/start.rb', line 28

def to_s
  "Start(#{@type}, label=#{@label})"
end