Class: RailroadDiagrams::Stack
- Inherits:
-
DiagramMultiContainer
- Object
- DiagramMultiContainer
- RailroadDiagrams::Stack
- Defined in:
- lib/railroad_diagrams/stack.rb,
sig/generated/railroad_diagrams/stack.rbs
Overview
Draws a long sequence on stacked rows.
Instance Method Summary collapse
- #add_final_paths(x, y, right_gap) ⇒ void
- #add_loop_back_path(x, y, x_initial, inner_width, item, index) ⇒ [ Numeric, Numeric ]
- #add_svg_path(group, path_data) ⇒ Object
- #calculate_dimensions ⇒ void
- #child_nodes ⇒ Object
- #format(x, y, width) ⇒ Stack
- #format_stacked_items(x, y, x_initial, inner_width) ⇒ [ Numeric, Numeric ]
-
#initialize(*items, id: nil, cls: nil, attrs: {}) ⇒ Stack
constructor
A new instance of Stack.
- #measure(context) ⇒ Object
- #render_svg(context, x, y, width) ⇒ Object
- #render_text(context) ⇒ Object
- #setup_initial_path(x, y) ⇒ [ Numeric, Numeric ]
- #text_diagram ⇒ TextDiagram
- #to_s ⇒ String
Constructor Details
#initialize(*items, id: nil, cls: nil, attrs: {}) ⇒ Stack
Returns a new instance of Stack.
11 12 13 14 15 16 17 |
# File 'lib/railroad_diagrams/stack.rb', line 11 def initialize(*items, id: nil, cls: nil, attrs: {}) raise InvalidArgument, 'Stack requires at least one item' if items.empty? super('g', items, nil, nil, id: id, cls: cls, data_attrs: attrs) @needs_space = true calculate_dimensions end |
Instance Method Details
#add_final_paths(x, y, right_gap) ⇒ void
This method returns an undefined value.
275 276 277 278 279 280 281 |
# File 'lib/railroad_diagrams/stack.rb', line 275 def add_final_paths(x, y, right_gap) if @items.size > 1 Path.new(x, y).h(AR).add(self) x += AR end Path.new(x, y).h(right_gap).add(self) end |
#add_loop_back_path(x, y, x_initial, inner_width, item, index) ⇒ [ Numeric, Numeric ]
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/railroad_diagrams/stack.rb', line 253 def add_loop_back_path(x, y, x_initial, inner_width, item, index) next_item = @items[index + 1] Path.new(x, y) .arc('ne') .down([0, item.down + VS - (AR * 2)].max) .arc('es') .left(inner_width) .arc('nw') .down([0, next_item.up + VS - (AR * 2)].max) .arc('ws') .add(self) y += [item.down + VS, AR * 2].max + [next_item.up + VS, AR * 2].max x = x_initial + AR [x, y] end |
#add_svg_path(group, path_data) ⇒ Object
197 198 199 |
# File 'lib/railroad_diagrams/stack.rb', line 197 def add_svg_path(group, path_data) group << Svg::Element.new('path', { 'd' => path_data }, self_closing: true) end |
#calculate_dimensions ⇒ void
This method returns an undefined value.
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/railroad_diagrams/stack.rb', line 202 def calculate_dimensions @width = @items.map { |item| item.width + (item.needs_space ? 20 : 0) }.max @width += AR * 2 if @items.size > 1 @up = @items.first.up @down = @items.last.down @height = 0 last_index = @items.size - 1 @items.each_with_index do |item, i| @height += item.height @height += [AR * 2, item.up + VS].max if i.positive? @height += [AR * 2, item.down + VS].max if i < last_index end end |
#child_nodes ⇒ Object
191 192 193 |
# File 'lib/railroad_diagrams/stack.rb', line 191 def child_nodes @items.dup end |
#format(x, y, width) ⇒ Stack
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/railroad_diagrams/stack.rb', line 29 def format(x, y, width) left_gap, right_gap = determine_gaps(width, @width) Path.new(x, y).h(left_gap).add(self) x += left_gap x_initial = x inner_width, x = setup_initial_path(x, y) x, y = format_stacked_items(x, y, x_initial, inner_width) add_final_paths(x, y, right_gap) self end |
#format_stacked_items(x, y, x_initial, inner_width) ⇒ [ Numeric, Numeric ]
233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/railroad_diagrams/stack.rb', line 233 def format_stacked_items(x, y, x_initial, inner_width) @items.each_with_index do |item, i| item.format(x, y, inner_width).add(self) x += inner_width y += item.height next if i == @items.size - 1 x, y = add_loop_back_path(x, y, x_initial, inner_width, item, i) end [x, y] end |
#measure(context) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/railroad_diagrams/stack.rb', line 83 def measure(context) item_metrics = @items.map { |item| context.metrics(item) } arc = context..arc_radius separation = context..vertical_separation width = item_metrics.map { |metrics| metrics.width + (metrics.needs_space ? 20 : 0) }.max width += arc * 2 if item_metrics.size > 1 height = 0 item_metrics.each_with_index do |metrics, index| height += metrics.height height += [arc * 2, metrics.up + separation].max if index.positive? height += [arc * 2, metrics.down + separation].max if index < item_metrics.size - 1 end Metrics.new(width: width, up: item_metrics.first.up, height: height, down: item_metrics.last.down, needs_space: true) end |
#render_svg(context, x, y, width) ⇒ Object
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 139 140 141 142 143 |
# File 'lib/railroad_diagrams/stack.rb', line 100 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') add_svg_path(group, Svg::PathData.new(x, y, arc_radius: arc).h(left_gap)) x += left_gap x_initial = x inner_width = metrics.width if @items.size > 1 add_svg_path(group, Svg::PathData.new(x, y, arc_radius: arc).h(arc)) x += arc inner_width -= arc * 2 end @items.each_with_index do |item, index| item_metrics = context.metrics(item) group << context.render_svg(item, x, y, inner_width) x += inner_width y += item_metrics.height next if index == @items.size - 1 next_metrics = context.metrics(@items[index + 1]) loop_back = Svg::PathData.new(x, y, arc_radius: arc) .arc('ne') .v([0, item_metrics.down + separation - (arc * 2)].max) .arc('es').h(-inner_width).arc('nw') .v([0, next_metrics.up + separation - (arc * 2)].max) .arc('ws') add_svg_path(group, loop_back) y += [item_metrics.down + separation, arc * 2].max + [next_metrics.up + separation, arc * 2].max x = x_initial + arc end if @items.size > 1 add_svg_path(group, Svg::PathData.new(x, y, arc_radius: arc).h(arc)) x += arc end add_svg_path(group, Svg::PathData.new(x, y, arc_radius: arc).h(right_gap)) group end |
#render_text(context) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 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 |
# File 'lib/railroad_diagrams/stack.rb', line 145 def render_text(context) corner_bot_left, corner_bot_right, corner_top_left, corner_top_right, line, line_vertical = context.parts.values_at(*%w[corner_bot_left corner_bot_right corner_top_left corner_top_right line line_vertical]) item_tds = @items.map { |item| item.render_text(context) } max_width = item_tds.map(&:width).max left_lines = [] right_lines = [] = [] item_tds.each_with_index do |item_td, index| if index.zero? left_lines << (line * 2) left_lines.concat([' ' * 2] * (item_td.height - item_td.entry - 1)) else left_lines << (corner_top_left + line) left_lines.concat(["#{line_vertical} "] * item_td.entry) left_lines << (corner_bot_left + line) left_lines.concat([' ' * 2] * (item_td.height - item_td.entry - 1)) right_lines.concat([' ' * 2] * item_td.exit) end if index < item_tds.size - 1 right_lines << (line + corner_top_right) right_lines.concat([" #{line_vertical}"] * (item_td.height - item_td.exit - 1)) right_lines << (line + corner_bot_right) else right_lines << (line * 2) end left_pad, = context.gaps(max_width, item_td.width) left_pad = left_pad.floor right_pad = max_width - item_td.width - left_pad lines = item_td.lines.each_with_index.map do |text, row| left = (row == item_td.entry ? line : ' ') * left_pad right = (row == item_td.exit ? line : ' ') * right_pad "#{left}#{text}#{right}" end << TextDiagram.new(item_td.entry, item_td.exit, lines) end middle = TextDiagram.new(.first.entry, .first.exit, Text::Builder.stack(, [line * max_width])) left = TextDiagram.new(0, 0, left_lines) right = TextDiagram.new(0, right_lines.size - 1, right_lines) left.append_right(middle, '').append_right(right, '') end |
#setup_initial_path(x, y) ⇒ [ Numeric, Numeric ]
221 222 223 224 225 226 |
# File 'lib/railroad_diagrams/stack.rb', line 221 def setup_initial_path(x, y) return [@width, x] if @items.size == 1 Path.new(x, y).h(AR).add(self) [@width - (AR * 2), x + AR] end |
#text_diagram ⇒ TextDiagram
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 73 74 75 76 77 78 79 80 81 |
# File 'lib/railroad_diagrams/stack.rb', line 43 def text_diagram corner_bot_left, corner_bot_right, corner_top_left, corner_top_right, line, line_vertical = TextDiagram.get_parts( %w[corner_bot_left corner_bot_right corner_top_left corner_top_right line line_vertical] ) item_tds = @items.map(&:text_diagram) max_width = item_tds.map(&:width).max left_lines = [] right_lines = [] separator_td = TextDiagram.new(0, 0, [line * max_width]) diagram_td = nil item_tds.each_with_index do |item_td, item_num| if item_num.zero? left_lines += [line * 2] left_lines += [' ' * 2] * (item_td.height - item_td.entry - 1) else diagram_td = diagram_td.append_below(separator_td, []) left_lines += [corner_top_left + line] left_lines += ["#{line_vertical} "] * item_td.entry left_lines += [corner_bot_left + line] left_lines += [' ' * 2] * (item_td.height - item_td.entry - 1) right_lines += [' ' * 2] * item_td.exit end if item_num < item_tds.size - 1 right_lines += [line + corner_top_right] right_lines += [" #{line_vertical}"] * (item_td.height - item_td.exit - 1) right_lines += [line + corner_bot_right] else right_lines += [line * 2] end left_pad, right_pad = TextDiagram.gaps(max_width, item_td.width) item_td = item_td.(left_pad, right_pad, 0, 0) diagram_td = item_num.zero? ? item_td : diagram_td.append_below(item_td, []) end left_td = TextDiagram.new(0, 0, left_lines) diagram_td = left_td.append_right(diagram_td, '') right_td = TextDiagram.new(0, right_lines.size - 1, right_lines) diagram_td.append_right(right_td, '') end |
#to_s ⇒ String
20 21 22 23 |
# File 'lib/railroad_diagrams/stack.rb', line 20 def to_s items = @items.map(&:to_s).join(', ') "Stack(#{items})" end |