Class: RailroadDiagrams::Diagram
Instance Attribute Summary
Attributes inherited from DiagramItem
#attrs, #children, #down, #height, #needs_space, #up, #width
Instance Method Summary
collapse
#to_str, #walk
Methods inherited from DiagramItem
#add, #to_str, #walk
Constructor Details
#initialize(*items, **kwargs) ⇒ Diagram
Returns a new instance of Diagram.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/railroad_diagrams/diagram.rb', line 5
def initialize(*items, **kwargs)
super('svg', items.to_a, { 'class' => DIAGRAM_CLASS })
@type = kwargs.fetch(:type, 'simple')
if @items.any?
@items.unshift(Start.new(@type)) unless @items.first.is_a?(Start)
@items.push(End.new(@type)) unless @items.last.is_a?(End)
end
@up = 0
@down = 0
@height = 0
@width = 0
@items.each do |item|
next if item.is_a?(Style)
@width += item.width + (item.needs_space ? 20 : 0)
@up = [@up, item.up - @height].max
@height += item.height
@down = [@down - item.height, item.down].max
end
@width -= 10 if @items[0].needs_space
@width -= 10 if @items[-1].needs_space
@formatted = false
end
|
Instance Method Details
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/railroad_diagrams/diagram.rb', line 40
def format(padding_top = 20, padding_right = nil, padding_bottom = nil, padding_left = nil)
padding_right = padding_top if padding_right.nil?
padding_bottom = padding_top if padding_bottom.nil?
padding_left = padding_right if padding_left.nil?
x = padding_left
y = padding_top + @up
g = DiagramItem.new('g')
g.attrs['transform'] = 'translate(.5 .5)' if STROKE_ODD_PIXEL_LENGTH
@items.each do |item|
if item.needs_space
Path.new(x, y).h(10).add(g)
x += 10
end
item.format(x, y, item.width).add(g)
x += item.width
y += item.height
if item.needs_space
Path.new(x, y).h(10).add(g)
x += 10
end
end
@attrs['width'] = (@width + padding_left + padding_right).to_s
@attrs['height'] = (@up + @height + @down + padding_top + padding_bottom).to_s
@attrs['viewBox'] = "0 0 #{@attrs['width']} #{@attrs['height']}"
g.add(self)
@formatted = true
self
end
|
#text_diagram ⇒ Object
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/railroad_diagrams/diagram.rb', line 72
def text_diagram
separator, = TextDiagram.get_parts(['separator'])
diagram_td = @items[0].text_diagram
@items[1..-1].each do |item|
item_td = item.text_diagram
item_td = item_td.expand(1, 1, 0, 0) if item.needs_space
diagram_td = diagram_td.append_right(item_td, separator)
end
diagram_td
end
|
#to_s ⇒ Object
33
34
35
36
37
38
|
# File 'lib/railroad_diagrams/diagram.rb', line 33
def to_s
items = items.map(&:to_s).join(', ')
pieces = items ? [items] : []
pieces.push("type=#{@type}") if @type != 'simple'
"Diagram(#{pieces.join(', ')})"
end
|
#write_standalone(write, css = nil) ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/railroad_diagrams/diagram.rb', line 96
def write_standalone(write, css = nil)
format unless @formatted
css = Style.default_style if css
Style.new(css).add(self)
@attrs['xmlns'] = 'http://www.w3.org/2000/svg'
@attrs['xmlns:xlink'] = 'http://www.w3.org/1999/xlink'
super(write)
@children.pop
@attrs.delete('xmlns')
@attrs.delete('xmlns:xlink')
end
|
#write_svg(write) ⇒ Object
83
84
85
86
87
|
# File 'lib/railroad_diagrams/diagram.rb', line 83
def write_svg(write)
format unless @formatted
super
end
|
#write_text(write) ⇒ Object
89
90
91
92
93
94
|
# File 'lib/railroad_diagrams/diagram.rb', line 89
def write_text(write)
output = text_diagram
output = "#{output.lines.join("\n")}\n"
output = output.gsub('&', '&').gsub('<', '<').gsub('>', '>').gsub('"', '"')
write.call(output)
end
|