Class: RailroadDiagrams::Comment

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

Overview

Plain explanatory text on a diagram path.

Examples:

Comment.new('optional whitespace')

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(text, legacy_href = Deprecation::UNSET, legacy_title = Deprecation::UNSET, href: Deprecation::UNSET, title: Deprecation::UNSET, cls: '', id: nil, attrs: {}) ⇒ Comment

rubocop:disable-next Metrics/ParameterLists

Parameters:

  • text (String)
  • legacy_href (Object) (defaults to: Deprecation::UNSET)
  • legacy_title (Object) (defaults to: Deprecation::UNSET)
  • href: (String, nil) (defaults to: Deprecation::UNSET)
  • title: (String, nil) (defaults to: Deprecation::UNSET)
  • cls: (String) (defaults to: '')
  • id: (Object) (defaults to: nil)
  • attrs: (Object) (defaults to: {})


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/railroad_diagrams/comment.rb', line 15

def initialize(text, legacy_href = Deprecation::UNSET, legacy_title = Deprecation::UNSET,
               href: Deprecation::UNSET, title: Deprecation::UNSET, cls: '', id: nil, attrs: {})
  super('g', attrs: { 'class' => "comment non-terminal #{cls}" }, id: id, data_attrs: attrs)
  @text = text.to_s
  @href = Deprecation.positional_argument('href', legacy_href, href)
  @title = Deprecation.positional_argument('title', legacy_title, title)
  @cls = cls
  @width = (Unicode::DisplayWidth.of(@text) * COMMENT_CHAR_WIDTH) + 10
  @up = 8
  @down = 8
  @needs_space = true
end

Instance Method Details

#add_connecting_paths(x, y, left_gap, right_gap) ⇒ void

This method returns an undefined value.

Parameters:

  • x (Numeric)
  • y (Numeric)
  • left_gap (Numeric)
  • right_gap (Numeric)


88
89
90
91
# File 'lib/railroad_diagrams/comment.rb', line 88

def add_connecting_paths(x, y, left_gap, right_gap)
  Path.new(x, y).h(left_gap).add(self)
  Path.new(x + left_gap + @width, y).h(right_gap).add(self)
end

#add_text_element(x, y) ⇒ void

This method returns an undefined value.

Parameters:

  • x (Numeric)
  • y (Numeric)


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/railroad_diagrams/comment.rb', line 96

def add_text_element(x, y)
  text = DiagramItem.new(
    'text',
    attrs: { 'x' => x + (@width / 2), 'y' => y + 4, 'class' => 'comment' },
    text: @text
  )

  if @href
    a = DiagramItem.new('a', attrs: { 'xlink:href' => @href }, text: text).add(self)
    text.add(a)
  else
    text.add(self)
  end

  DiagramItem.new('title', attrs: {}, text: @title).add(self) if @title
end

#child_nodes ⇒ Object

Returns:

  • (Object)


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

def child_nodes
  []
end

#format(x, y, width) ⇒ Comment

Parameters:

  • x (Numeric)
  • y (Numeric)
  • width (Numeric)

Returns:



37
38
39
40
41
42
# File 'lib/railroad_diagrams/comment.rb', line 37

def format(x, y, width)
  left_gap, right_gap = determine_gaps(width, @width)
  add_connecting_paths(x, y, left_gap, right_gap)
  add_text_element(x + left_gap, y)
  self
end

#measure(context) ⇒ Object

Parameters:

  • context (Object)

Returns:

  • (Object)


49
50
51
52
# File 'lib/railroad_diagrams/comment.rb', line 49

def measure(context)
  Metrics.new(width: context.text_width(@text, :comment) + 10,
              up: 8, height: 0, down: 8, needs_space: true)
end

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

Parameters:

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

Returns:

  • (Object)


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

def render_svg(context, x, y, width)
  metrics = context.metrics(self)
  left_gap, right_gap = context.gaps(width, metrics.width)
  group = Svg::Element.new('g', @attrs.dup)
  group << (Svg::Element.new('title') << Svg::TextNode.new(@title)) if @title
  group << Svg::Element.new('path', { 'd' => Svg::PathData.new(x, y).h(left_gap) }, self_closing: true)
  group << Svg::Element.new('path', { 'd' => Svg::PathData.new(x + left_gap + metrics.width, y).h(right_gap) }, self_closing: true)
  text = Svg::Element.new('text', { 'x' => x + left_gap + (metrics.width / 2), 'y' => y + 4, 'class' => 'comment' })
  text << Svg::TextNode.new(@text)
  if (attrs = context.link_attrs(@href))
    link = Svg::Element.new('a', attrs)
    link << text
    group << link
  else
    group << text
  end
  group
end

#render_text(_context) ⇒ Object

Parameters:

  • _context (Object)

Returns:

  • (Object)


73
74
75
# File 'lib/railroad_diagrams/comment.rb', line 73

def render_text(_context)
  TextDiagram.new(0, 0, [@text])
end

#text_diagram ⇒ TextDiagram

Returns:

  • (TextDiagram)


45
46
47
# File 'lib/railroad_diagrams/comment.rb', line 45

def text_diagram
  TextDiagram.new(0, 0, [@text])
end

#to_s ⇒ String

Returns:

  • (String)


29
30
31
# File 'lib/railroad_diagrams/comment.rb', line 29

def to_s
  "Comment(#{@text}, href=#{@href}, title=#{@title}, cls=#{@cls})"
end