Class: SequenceDiagram::Diagram
- Inherits:
-
Object
- Object
- SequenceDiagram::Diagram
- Defined in:
- lib/yard-sd/sequence_diagram/diagram.rb
Defined Under Namespace
Constant Summary collapse
- @@uid =
0
Instance Attribute Summary collapse
-
#uid ⇒ Object
readonly
Returns the value of attribute uid.
Instance Method Summary collapse
- #error(kind, information) ⇒ Object
-
#initialize ⇒ Diagram
constructor
A new instance of Diagram.
- #parse(s) ⇒ Object
- #to_png(options = {}) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ Diagram
Returns a new instance of Diagram.
12 13 14 15 16 |
# File 'lib/yard-sd/sequence_diagram/diagram.rb', line 12 def initialize @participants = [] @calls = [] @uid = @@uid += 1 end |
Instance Attribute Details
#uid ⇒ Object (readonly)
Returns the value of attribute uid.
11 12 13 |
# File 'lib/yard-sd/sequence_diagram/diagram.rb', line 11 def uid @uid end |
Instance Method Details
#error(kind, information) ⇒ Object
74 75 76 |
# File 'lib/yard-sd/sequence_diagram/diagram.rb', line 74 def error(kind, information) raise ParseError, [kind, information].inspect end |
#parse(s) ⇒ Object
18 19 20 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 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/yard-sd/sequence_diagram/diagram.rb', line 18 def parse(s) last_calls = Hash.new {|h, k| h[k] = []} blocks = [Block.new(0, nil, nil)] last_indent = 0 s.each_line do |line| line.chomp! next if line.empty? case line when /^(participant|thread)(?:\[(\d+)\])? ("?)(.+?)\3(?: as (.+))?$/ distance = $2 && $2.to_i name = $4 aliasing = $5 || name thread = $1 == "thread" @participants << Participant.new(name, aliasing, distance, thread) when /^(\s*)block "([^"]+)" "([^"]+)"/ if $1.size < last_indent blocks.pop end blocks << Block.new(blocks.size, $2, $3) when /^(\s*)(.+?)\s*(->|-->)\s*(.+?):(?: (.+))?$/ p1 = @participants.find {|p| p.aliasing == $2} p2 = @participants.find {|p| p.aliasing == $4} error(:unknown_participant, line) if p1.nil? error(:unknown_participant, line) if p2.nil? = $5.to_s if $1.size < last_indent blocks.pop end last_indent = $1.size case $3 when "->" call = Call.new(p1, p2, , nil, blocks.last) last_calls[p1] << call @calls << call when "-->" last_calls[p2].pop.return = .to_s @calls << :pop end when /^\s*noreturn$/ @calls << :pop else error(:cannot_parse, line) end end self end |
#to_png(options = {}) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/yard-sd/sequence_diagram/diagram.rb', line 134 def to_png( = {}) graph = self.to_s tex = Template.sub("%%EMBED\n", graph.sub(/\n+$/, "")) dir = Dir.mktmpdir image = nil Dir.chdir(dir) do # File.open("/tmp/debug.tex", "w"){|f| f.print tex} IO.popen("pdflatex -halt-on-error", "w+") do |pipe| pipe.write tex pipe.close_write pipe.read end if $?.exitstatus != 0 error("Couldn't compile latex", nil) end args = ["-depth", "4", "-quality", "95", "-colorspace", "RGB", "-density", "500x500", "-flatten"] args << "-resize" << [:size] if [:size] image = `convert #{args.join(" ")} texput.pdf png:-` end FileUtils.rm_rf(dir) return image end |
#to_s ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/yard-sd/sequence_diagram/diagram.rb', line 78 def to_s s = "\\begin{sequencediagram}\n" @participants.each do |p| s << p.to_s << "\n" end last_callee = nil prev_block_level = 0 open = [] indent = 0 @calls.each do |call| if call == :pop indent -= 1 s << " " * indent << "\\end{#{open.pop}}\n" next end if call.block.level > prev_block_level s << " " * indent << "\\begin{sdblock}{#{call.block.name}}{#{call.block.description}}\n" indent += 1 elsif call.block.level < prev_block_level indent -= 1 s << " " * indent << "\\end{sdblock}\n" end prev_block_level = call.block.level if !call.return.nil? if call.p1 == call.p2 s << " " * indent << "\\begin{callself}{#{call.p1.uid}}{#{SequenceDiagram.latex_escape(call.)}}{#{SequenceDiagram.latex_escape(call.return)}}\n" open << :callself else s << " " * indent << "\\begin{call}{#{call.p1.uid}}{#{SequenceDiagram.latex_escape(call.)}}{#{call.p2.uid}}{#{SequenceDiagram.latex_escape(call.return)}}\n" open << :call end else s << " "* indent << "\\begin{messcall}{#{call.p1.uid}}{#{SequenceDiagram.latex_escape(call.)}}{#{call.p2.uid}}\n" open << :messcall end indent += 1 end while close = open.pop s << " " * indent << "\\end{#{close}}\n" indent -= 1 end while prev_block_level > 0 indent -= 1 s << " " * indent << "\\end{sdblock}\n" prev_block_level -= 1 end s << "\\end{sequencediagram}" end |