Class: RailroadDiagrams::Comment

Inherits:
DiagramItem show all
Defined in:
lib/railroad_diagrams/comment.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, #walk, #write_svg

Constructor Details

#initialize(text, href = nil, title = nil, cls: '') ⇒ Comment

Returns a new instance of Comment.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/railroad_diagrams/comment.rb', line 5

def initialize(text, href = nil, title = nil, cls: '')
  super('g', attrs: { 'class' => "non-terminal #{cls}" })
  @text = text
  @href = href
  @title = title
  @cls = cls
  @width = (text.length * COMMENT_CHAR_WIDTH) + 10
  @up = 8
  @down = 8
  @needs_space = true
end

Instance Method Details

#format(x, y, _width) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/railroad_diagrams/comment.rb', line 21

def format(x, y, _width)
  left_gap, right_gap = determine_gaps(width, @width)

  # Hook up the two sides if self is narrower than its stated width.
  Path.new(x, y).h(left_gap).add(self)
  Path.new(x + left_gap + @width, y).h(right_gap).add(self)

  text = DiagramItem.new(
    'text',
    attrs: { 'x' => x + left_gap + (@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
  self
end

#text_diagramObject



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

def text_diagram
  # NOTE: href, title, and cls are ignored for text diagrams.
  TextDiagram.new(0, 0, [@text])
end

#to_sObject



17
18
19
# File 'lib/railroad_diagrams/comment.rb', line 17

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