Class: RailroadDiagrams::DiagramItem
- Inherits:
-
Object
- Object
- RailroadDiagrams::DiagramItem
- Defined in:
- lib/railroad_diagrams/diagram_item.rb
Direct Known Subclasses
Comment, DiagramMultiContainer, End, Group, NonTerminal, OneOrMore, Skip, Start, Terminal
Instance Attribute Summary collapse
-
#attrs ⇒ Object
readonly
Returns the value of attribute attrs.
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#down ⇒ Object
readonly
Returns the value of attribute down.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#needs_space ⇒ Object
readonly
Returns the value of attribute needs_space.
-
#up ⇒ Object
readonly
Returns the value of attribute up.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #add(parent) ⇒ Object
- #format(x, y, width) ⇒ Object
-
#initialize(name, attrs: {}, text: nil) ⇒ DiagramItem
constructor
A new instance of DiagramItem.
- #text_diagram ⇒ Object
- #to_str ⇒ Object
- #walk(_callback) ⇒ Object
- #write_svg(write) ⇒ Object
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
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
5 6 7 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 5 def attrs @attrs end |
#children ⇒ Object (readonly)
Returns the value of attribute children.
5 6 7 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 5 def children @children end |
#down ⇒ Object (readonly)
Returns the value of attribute down.
5 6 7 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 5 def down @down end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
5 6 7 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 5 def height @height end |
#needs_space ⇒ Object (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 |
#up ⇒ Object (readonly)
Returns the value of attribute up.
5 6 7 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 5 def up @up end |
#width ⇒ Object (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
18 19 20 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 18 def format(x, y, width) raise NotImplementedError end |
#text_diagram ⇒ Object
22 23 24 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 22 def text_diagram raise NotImplementedError 'Virtual' end |
#to_str ⇒ Object
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 |