Class: RailroadDiagrams::OptionalSequence
- Inherits:
-
DiagramMultiContainer
- Object
- DiagramMultiContainer
- RailroadDiagrams::OptionalSequence
- Defined in:
- lib/railroad_diagrams/optional_sequence.rb,
sig/generated/railroad_diagrams/optional_sequence.rbs
Overview
Draws a sequence whose items may be skipped individually.
Class Method Summary collapse
Instance Method Summary collapse
- #child_nodes ⇒ Object
- #format(x, y, width) ⇒ OptionalSequence
-
#initialize(*items, id: nil, cls: nil, attrs: {}) ⇒ OptionalSequence
constructor
A new instance of OptionalSequence.
- #measure(context) ⇒ Object
- #path(x, y, arc = AR) {|data| ... } ⇒ Object
- #render_svg(context, x, y, width) ⇒ Object
- #render_text(context) ⇒ Object
- #render_text_diagram(parts, context) ⇒ Object
- #text_diagram ⇒ TextDiagram
- #to_s ⇒ String
Constructor Details
#initialize(*items, id: nil, cls: nil, attrs: {}) ⇒ OptionalSequence
Returns a new instance of OptionalSequence.
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 |
# File 'lib/railroad_diagrams/optional_sequence.rb', line 19 def initialize(*items, id: nil, cls: nil, attrs: {}) super('g', items, nil, nil, id: id, cls: cls, data_attrs: attrs) @needs_space = false @width = 0 @up = 0 @height = @items.sum(&:height) @down = @items.first.down height_so_far = 0.0 @items.each_with_index do |item, i| @up = [@up, [AR * 2, item.up + VS].max - height_so_far].max height_so_far += item.height if i.positive? @down = [ @height + @down, height_so_far + [AR * 2, item.down + VS].max ].max - @height end item_width = item.width + (item.needs_space ? 10 : 0) @width += if i.zero? AR + [item_width, AR].max else (AR * 2) + [item_width, AR].max + AR end end end |
Class Method Details
.new(*items, id: nil, cls: nil, attrs: {}) ⇒ OptionalSequence, Sequence
11 12 13 14 15 |
# File 'lib/railroad_diagrams/optional_sequence.rb', line 11 def self.new(*items, id: nil, cls: nil, attrs: {}) return Sequence.new(*items, id: id, cls: cls, attrs: attrs) if items.size <= 1 super end |
Instance Method Details
#child_nodes ⇒ Object
213 214 215 |
# File 'lib/railroad_diagrams/optional_sequence.rb', line 213 def child_nodes @items.dup end |
#format(x, y, width) ⇒ OptionalSequence
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 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 133 134 135 136 137 138 |
# File 'lib/railroad_diagrams/optional_sequence.rb', line 59 def format(x, y, width) left_gap, right_gap = determine_gaps(width, @width) Path.new(x, y).right(left_gap).add(self) Path.new(x + left_gap + @width, y + @height).right(right_gap).add(self) x += left_gap upper_line_y = y - @up last = @items.size - 1 @items.each_with_index do |item, i| item_space = item.needs_space ? 10 : 0 item_width = item.width + item_space if i.zero? # Upper skip Path.new(x, y) .arc('se') .up(y - upper_line_y - (AR * 2)) .arc('wn') .right(item_width - AR) .arc('ne') .down(y + item.height - upper_line_y - (AR * 2)) .arc('ws') .add(self) # Straight line Path.new(x, y).right(item_space + AR).add(self) item.format(x + item_space + AR, y, item.width).add(self) x += item_width + AR y += item.height elsif i < last # Upper skip Path.new(x, upper_line_y) .right((AR * 2) + [item_width, AR].max + AR) .arc('ne') .down(y - upper_line_y + item.height - (AR * 2)) .arc('ws') .add(self) # Straight line Path.new(x, y).right(AR * 2).add(self) item.format(x + (AR * 2), y, item.width).add(self) Path.new(x + item.width + (AR * 2), y + item.height) .right(item_space + AR) .add(self) # Lower skip Path.new(x, y) .arc('ne') .down(item.height + [item.down + VS, AR * 2].max - (AR * 2)) .arc('ws') .right(item_width - AR) .arc('se') .up(item.down + VS - (AR * 2)) .arc('wn') .add(self) x += (AR * 2) + [item_width, AR].max + AR y += item.height else # Straight line Path.new(x, y).right(AR * 2).add(self) item.format(x + (AR * 2), y, item.width).add(self) Path.new(x + (AR * 2) + item.width, y + item.height) .right(item_space + AR) .add(self) # Lower skip Path.new(x, y) .arc('ne') .down(item.height + [item.down + VS, AR * 2].max - (AR * 2)) .arc('ws') .right(item_width - AR) .arc('se') .up(item.down + VS - (AR * 2)) .arc('wn') .add(self) end end self end |
#measure(context) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/railroad_diagrams/optional_sequence.rb', line 140 def measure(context) arc = context..arc_radius separation = context..vertical_separation children = @items.map { |item| context.metrics(item) } height = children.sum(&:height) width = 0 up = 0 down = children.first.down height_so_far = 0.0 children.each_with_index do |item, index| up = [up, [arc * 2, item.up + separation].max - height_so_far].max height_so_far += item.height down = [height + down, height_so_far + [arc * 2, item.down + separation].max].max - height if index.positive? item_width = item.width + (item.needs_space ? 10 : 0) width += index.zero? ? arc + [item_width, arc].max : (arc * 2) + [item_width, arc].max + arc end Metrics.new(width: width, up: up, height: height, down: down, needs_space: false) end |
#path(x, y, arc = AR) {|data| ... } ⇒ Object
224 225 226 227 228 |
# File 'lib/railroad_diagrams/optional_sequence.rb', line 224 def path(x, y, arc = AR) data = Svg::PathData.new(x, y, arc_radius: arc) yield data Svg::Element.new('path', { 'd' => data }, self_closing: true) end |
#render_svg(context, x, y, width) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/railroad_diagrams/optional_sequence.rb', line 160 def render_svg(context, x, y, width) metrics = context.metrics(self) arc = context..arc_radius separation = context..vertical_separation left_gap, right_gap = context.gaps(width, metrics.width) group = Svg::Element.new('g', @attrs.dup) group << path(x, y) { |p| p.h([0, left_gap].max) } group << path(x + left_gap + metrics.width, y + metrics.height) { |p| p.h([0, right_gap].max) } x += left_gap upper_line_y = y - metrics.up last = @items.size - 1 @items.each_with_index do |item, index| child = context.metrics(item) item_space = child.needs_space ? 10 : 0 item_width = child.width + item_space if index.zero? group << path(x, y, arc) do |p| p.arc('se').v(-[0, y - upper_line_y - (arc * 2)].max).arc('wn') .h([0, item_width - arc].max).arc('ne') .v([0, y + child.height - upper_line_y - (arc * 2)].max).arc('ws') end group << path(x, y) { |p| p.h([0, item_space + arc].max) } group << context.render_svg(item, x + item_space + arc, y, child.width) x += item_width + arc y += child.height next end if index < last group << path(x, upper_line_y, arc) do |p| p.h([0, (arc * 2) + [item_width, arc].max + arc].max).arc('ne') .v([0, y - upper_line_y + child.height - (arc * 2)].max).arc('ws') end end group << path(x, y) { |p| p.h(arc * 2) } group << context.render_svg(item, x + (arc * 2), y, child.width) group << path(x + (arc * 2) + child.width, y + child.height) { |p| p.h([0, item_space + arc].max) } group << path(x, y, arc) do |p| p.arc('ne').v([0, child.height + [child.down + separation, arc * 2].max - (arc * 2)].max) .arc('ws').h([0, item_width - arc].max) .arc('se').v(-[0, child.down + separation - (arc * 2)].max).arc('wn') end x += (arc * 2) + [item_width, arc].max + arc y += child.height end group end |
#render_text(context) ⇒ Object
209 210 211 |
# File 'lib/railroad_diagrams/optional_sequence.rb', line 209 def render_text(context) render_text_diagram(context.parts, context) end |
#render_text_diagram(parts, context) ⇒ Object
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/railroad_diagrams/optional_sequence.rb', line 230 def render_text_diagram(parts, context) line, line_vertical, roundcorner_bot_left, roundcorner_bot_right, roundcorner_top_left, roundcorner_top_right = parts.values_at( 'line', 'line_vertical', 'roundcorner_bot_left', 'roundcorner_bot_right', 'roundcorner_top_left', 'roundcorner_top_right' ) # Format all the child items, so we can know the maximum entry. item_tds = @items.map { |item| context ? item.render_text(context) : item.text_diagram } # diagramEntry: distance from top to lowest entry, aka distance from top to diagram entry, aka final diagram entry and exit. diagram_entry = item_tds.map(&:entry).max # SOILHeight: distance from top to lowest entry before rightmost item, aka distance from skip-over-items line to rightmost entry, aka SOIL height. soil_height = item_tds[0...-1].map(&:entry).max # topToSOIL: distance from top to skip-over-items line. top_to_soil = diagram_entry - soil_height # The diagram starts with a line from its entry up to the skip-over-items line: lines = [' '] * top_to_soil lines += [roundcorner_top_left + line] lines += ["#{line_vertical} "] * soil_height lines += [roundcorner_bot_right + line] diagram_td = TextDiagram.new(lines.size - 1, lines.size - 1, lines) item_tds.each_with_index do |item_td, i| if i.positive? # All items except the leftmost start with a line from their entry down to their skip-under-item line, # with a joining-line across at the skip-over-items line: lines = ([' '] * top_to_soil) + [line * 2] + ([' '] * (diagram_td.exit - top_to_soil - 1)) + [line + roundcorner_top_right] + ([" #{line_vertical}"] * (item_td.height - item_td.entry - 1)) + [" #{roundcorner_bot_left}"] skip_down_td = TextDiagram.new(diagram_td.exit, diagram_td.exit, lines) diagram_td = diagram_td.append_right(skip_down_td, '') # All items except the leftmost next have a line from skip-over-items line down to their entry, # with joining-lines at their entry and at their skip-under-item line: lines = ([' '] * top_to_soil) + [line + roundcorner_top_right + # All such items except the rightmost also have a continuation of the skip-over-items line: (i < item_tds.size - 1 ? line : ' ')] + ([" #{line_vertical} "] * (diagram_td.exit - top_to_soil - 1)) + [line + roundcorner_bot_left + line] + ([' ' * 3] * (item_td.height - item_td.entry - 1)) + [line * 3] entry_td = TextDiagram.new(diagram_td.exit, diagram_td.exit, lines) diagram_td = diagram_td.append_right(entry_td, '') end part_td = TextDiagram.new(0, 0, []) if i < item_tds.size - 1 # All items except the rightmost have a segment of the skip-over-items line at the top, # followed by enough blank lines to push their entry down to the previous item's exit: lines = [line * item_td.width] + ([' ' * item_td.width] * (soil_height - item_td.entry)) soil_segment = TextDiagram.new(0, 0, lines) part_td = part_td.append_below(soil_segment, []) end part_td = part_td.append_below(item_td, [], move_entry: true, move_exit: true) if i.positive? # All items except the leftmost have their skip-under-item line at the bottom. soil_segment = TextDiagram.new(0, 0, [line * item_td.width]) part_td = part_td.append_below(soil_segment, []) end diagram_td = diagram_td.append_right(part_td, '') next unless i.positive? # All items except the leftmost have a line from their skip-under-item line to their exit: lines = ([' '] * top_to_soil) + # All such items except the rightmost also have a joining-line across at the skip-over-items line: [(i < item_tds.size - 1 ? line * 2 : ' ')] + ([' '] * (diagram_td.exit - top_to_soil - 1)) + [line + roundcorner_top_left] + ([" #{line_vertical}"] * (part_td.height - part_td.exit - 2)) + [line + roundcorner_bot_right] skip_up_td = TextDiagram.new(diagram_td.exit, diagram_td.exit, lines) diagram_td = diagram_td.append_right(skip_up_td, '') end diagram_td end |
#text_diagram ⇒ TextDiagram
218 219 220 |
# File 'lib/railroad_diagrams/optional_sequence.rb', line 218 def text_diagram render_text_diagram(Context.legacy.parts, nil) end |
#to_s ⇒ String
50 51 52 53 |
# File 'lib/railroad_diagrams/optional_sequence.rb', line 50 def to_s items = @items.map(&:to_s).join(', ') "OptionalSequence(#{items})" end |