Class: RailroadDiagrams::DiagramItem

Inherits:
Object
  • Object
show all
Defined in:
lib/railroad_diagrams/diagram_item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, attrs: {}, text: nil) ⇒ DiagramItem

Returns a new instance of DiagramItem.



7
8
9
10
11
12
13
14
15
16
# File 'lib/railroad_diagrams/diagram_item.rb', line 7

def initialize(name, attrs: {}, text: nil)
  @name = name
  @up = 0
  @height = 0
  @down = 0
  @width = 0
  @needs_space = false
  @attrs = attrs || {}
  @children = text ? [text] : []
end

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs.



5
6
7
# File 'lib/railroad_diagrams/diagram_item.rb', line 5

def attrs
  @attrs
end

#childrenObject (readonly)

Returns the value of attribute children.



5
6
7
# File 'lib/railroad_diagrams/diagram_item.rb', line 5

def children
  @children
end

#downObject (readonly)

Returns the value of attribute down.



5
6
7
# File 'lib/railroad_diagrams/diagram_item.rb', line 5

def down
  @down
end

#heightObject (readonly)

Returns the value of attribute height.



5
6
7
# File 'lib/railroad_diagrams/diagram_item.rb', line 5

def height
  @height
end

#needs_spaceObject (readonly)

Returns the value of attribute needs_space.



5
6
7
# File 'lib/railroad_diagrams/diagram_item.rb', line 5

def needs_space
  @needs_space
end

#upObject (readonly)

Returns the value of attribute up.



5
6
7
# File 'lib/railroad_diagrams/diagram_item.rb', line 5

def up
  @up
end

#widthObject (readonly)

Returns the value of attribute width.



5
6
7
# File 'lib/railroad_diagrams/diagram_item.rb', line 5

def width
  @width
end

Instance Method Details

#add(parent) ⇒ Object



26
27
28
29
# File 'lib/railroad_diagrams/diagram_item.rb', line 26

def add(parent)
  parent.children.push self
  self
end

#format(x, y, width) ⇒ Object

Raises:

  • (NotImplementedError)


18
19
20
# File 'lib/railroad_diagrams/diagram_item.rb', line 18

def format(x, y, width)
  raise NotImplementedError
end

#text_diagramObject



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

def text_diagram
  raise NotImplementedError 'Virtual'
end

#to_strObject



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

def to_str
  "DiagramItem(#{@name}, #{@attrs}, #{@children})"
end

#walk(_callback) ⇒ Object



48
49
50
# File 'lib/railroad_diagrams/diagram_item.rb', line 48

def walk(_callback)
  callback(self)
end

#write_svg(write) ⇒ Object



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

def write_svg(write)
  write.call("<#{@name}")
  @attrs.sort.each do |name, value|
    write.call(" #{name}=\"#{RailroadDiagrams.escape_attr(value)}\"")
  end
  write.call('>')
  write.call("\n") if %w[g svg].include?(@name)
  @children.each do |child|
    if child.is_a?(DiagramItem) || child.is_a?(Path) || child.is_a?(Style)
      child.write_svg(write)
    else
      write.call(RailroadDiagrams.escape_html(child))
    end
  end
  write.call("</#{@name}>")
end