Class: RailroadDiagrams::NonTerminal
- Inherits:
-
DiagramItem
- Object
- DiagramItem
- RailroadDiagrams::NonTerminal
- Defined in:
- lib/railroad_diagrams/non_terminal.rb
Instance Attribute Summary
Attributes inherited from DiagramItem
#attrs, #children, #down, #height, #needs_space, #up, #width
Instance Method Summary collapse
- #format(x, y, width) ⇒ Object
-
#initialize(text, href = nil, title = nil, cls: '') ⇒ NonTerminal
constructor
A new instance of NonTerminal.
- #text_diagram ⇒ Object
- #to_s ⇒ Object
Methods inherited from DiagramItem
#add, #to_str, #walk, #write_svg
Constructor Details
#initialize(text, href = nil, title = nil, cls: '') ⇒ NonTerminal
Returns a new instance of NonTerminal.
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/railroad_diagrams/non_terminal.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 * CHAR_WIDTH) + 20 @up = 11 @down = 11 @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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/railroad_diagrams/non_terminal.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) DiagramItem.new( 'rect', attrs: { 'x' => x + left_gap, 'y' => y - 11, 'width' => @width, 'height' => @up + @down } ).add(self) text = DiagramItem.new( 'text', attrs: { 'x' => x + left_gap + (@width / 2), 'y' => y + 4 }, 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_diagram ⇒ Object
62 63 64 65 |
# File 'lib/railroad_diagrams/non_terminal.rb', line 62 def text_diagram # NOTE: href, title, and cls are ignored for text diagrams. TextDiagram.rect(@text) end |
#to_s ⇒ Object
17 18 19 |
# File 'lib/railroad_diagrams/non_terminal.rb', line 17 def to_s "NonTerminal(#{@text}, href=#{@href}, title=#{@title}, cls=#{@cls})" end |