Class: RailroadDiagrams::End

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

Overview

The end marker of a diagram.

Examples:

End.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: {}) ⇒ End

Returns a new instance of End.

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
# File 'lib/railroad_diagrams/end.rb', line 12

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

Instance Method Details

#child_nodes ⇒ Object

Returns:

  • (Object)


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

def child_nodes
  []
end

#format(x, y, _width) ⇒ End

Parameters:

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

Returns:



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/railroad_diagrams/end.rb', line 33

def format(x, y, _width)
  path = legacy_path(x, y, @width)
  if @label
    DiagramItem.new('path', attrs: { 'd' => path }).add(self)
    DiagramItem.new('text', attrs: { 'x' => x + @width, 'y' => y - 15, 'style' => 'text-anchor:end' },
                            text: @label).add(self)
  else
    @attrs['d'] = path
  end
  self
end

#legacy_path(x, y, width) ⇒ Object

Parameters:

  • x (Object)
  • y (Object)
  • width (Object)

Returns:

  • (Object)


82
83
84
# File 'lib/railroad_diagrams/end.rb', line 82

def legacy_path(x, y, width)
  @type == 'simple' ? "M #{x} #{y} h #{width} m -10 -10 v 20 m 10 -20 v 20" : "M #{x} #{y} h #{width} m 0 -10 v 20"
end

#measure(context) ⇒ Object

Parameters:

  • context (Object)

Returns:

  • (Object)


51
52
53
54
# File 'lib/railroad_diagrams/end.rb', line 51

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)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/railroad_diagrams/end.rb', line 56

def render_svg(context, x, y, _width)
  width = context.metrics(self).width
  data = if context.options.precision || context.options.optimize_paths
           path = Svg::PathData.new(x, y).h(width)
           @type == 'simple' ? path.m(-10, -10).v(20).m(10, -20).v(20) : path.m(0, -10).v(20)
         else
           legacy_path(x, y, width)
         end
  path = Svg::Element.new('path', { 'd' => data })
  return path unless @label

  text = Svg::Element.new('text', { 'x' => x + width, 'y' => y - 15, 'style' => 'text-anchor:end' })
  Svg::Element.new('g', @attrs.dup) << path << (text << Svg::TextNode.new(@label))
end

#render_text(context) ⇒ Object

Parameters:

  • context (Object)

Returns:

  • (Object)


71
72
73
74
# File 'lib/railroad_diagrams/end.rb', line 71

def render_text(context)
  cross, line, tee_left = context.parts.values_at('cross', 'line', 'tee_left')
  text_with_label(cross, line, tee_left)
end

#text_diagram ⇒ TextDiagram

Returns:

  • (TextDiagram)


46
47
48
49
# File 'lib/railroad_diagrams/end.rb', line 46

def text_diagram
  cross, line, tee_left = TextDiagram.get_parts(%w[cross line tee_left])
  text_with_label(cross, line, tee_left)
end

#text_with_label(cross, line, tee_left) ⇒ Object

Parameters:

  • cross (Object)
  • line (Object)
  • tee_left (Object)

Returns:

  • (Object)


86
87
88
89
90
91
92
93
# File 'lib/railroad_diagrams/end.rb', line 86

def text_with_label(cross, line, tee_left)
  end_node = @type == 'simple' ? line + cross + tee_left : line + tee_left
  return TextDiagram.new(0, 0, [end_node]) unless @label

  width = TextDiagram.max_width(@label, end_node)
  label = TextDiagram.pad_l(@label, width, ' ')
  TextDiagram.new(1, 1, [label, TextDiagram.pad_l(end_node, width, line)])
end

#to_s ⇒ String

Returns:

  • (String)


23
24
25
26
27
# File 'lib/railroad_diagrams/end.rb', line 23

def to_s
  return "End(type=#{@type})" unless @label

  "End(type=#{@type}, label=#{@label})"
end