Class: RailroadDiagrams::DiagramItem
- Inherits:
-
Object
- Object
- RailroadDiagrams::DiagramItem
- Includes:
- Enumerable, Enumerable[DiagramItem]
- Defined in:
- lib/railroad_diagrams/diagram_item.rb,
sig/generated/railroad_diagrams/diagram_item.rbs
Overview
Base class for diagram nodes and structural traversal.
Direct Known Subclasses
Block, Comment, End, Except, Group, NonTerminal, OneOrMore, Repeat, SeparatedList, Skip, Start, Terminal
Instance Attribute Summary collapse
-
#attrs ⇒ Hash[String, String | Numeric]
readonly
: Hash[String, String | Numeric].
-
#children ⇒ Array[DiagramItem | Path | Style | String]
readonly
: Array[DiagramItem | Path | Style | String].
-
#down ⇒ Numeric
readonly
: Numeric.
-
#height ⇒ Numeric
readonly
: Numeric.
-
#needs_space ⇒ Boolean
readonly
: bool.
-
#up ⇒ Numeric
readonly
: Numeric.
-
#width ⇒ Numeric
readonly
: Numeric.
Class Method Summary collapse
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql?)
Compare node structure and user attributes.
- #add(parent) ⇒ DiagramItem
- #apply_user_attributes(id, data_attrs, cls) ⇒ Object
-
#child_nodes ⇒ Object
Return the node's direct structural children.
- #container_element? ⇒ Boolean
- #determine_gaps(outer, inner) ⇒ [ Numeric, Numeric ]
-
#each_node(&block) ⇒ void
(also: #each)
Visit this node and descendants in preorder.
- #format(x, y, width) ⇒ DiagramItem
-
#hash ⇒ Object
Hash the same structural state used by equality.
-
#initialize(name, attrs: {}, text: nil, id: nil, data_attrs: {}, cls: nil) ⇒ DiagramItem
constructor
A new instance of DiagramItem.
- #inspect ⇒ String
- #structural_state ⇒ Object
- #text_diagram ⇒ TextDiagram
- #to_str ⇒ String
- #walk(callback) ⇒ void
- #wrap_string(value) ⇒ DiagramItem
- #write_children(write) ⇒ void
- #write_opening_tag(write) ⇒ void
- #write_standalone(write, _css = nil) ⇒ void
- #write_svg(write) ⇒ void
Constructor Details
#initialize(name, attrs: {}, text: nil, id: nil, data_attrs: {}, cls: nil) ⇒ DiagramItem
Returns a new instance of DiagramItem.
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 40 def initialize(name, attrs: {}, text: nil, id: nil, data_attrs: {}, cls: nil) @name = name @up = 0 @height = 0 @down = 0 @width = 0 @needs_space = false @attrs = (attrs || {}).dup apply_user_attributes(id, data_attrs, cls) @children = text ? [text] : [] end |
Instance Attribute Details
#attrs ⇒ Hash[String, String | Numeric] (readonly)
: Hash[String, String | Numeric]
33 34 35 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 33 def attrs @attrs end |
#children ⇒ Array[DiagramItem | Path | Style | String] (readonly)
: Array[DiagramItem | Path | Style | String]
34 35 36 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 34 def children @children end |
#down ⇒ Numeric (readonly)
: Numeric
29 30 31 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 29 def down @down end |
#height ⇒ Numeric (readonly)
: Numeric
30 31 32 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 30 def height @height end |
#needs_space ⇒ Boolean (readonly)
: bool
32 33 34 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 32 def needs_space @needs_space end |
#up ⇒ Numeric (readonly)
: Numeric
28 29 30 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 28 def up @up end |
#width ⇒ Numeric (readonly)
: Numeric
31 32 33 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 31 def width @width end |
Class Method Details
.inherited(subclass) ⇒ void
This method returns an undefined value.
23 24 25 26 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 23 def self.inherited(subclass) super subclass.prepend(ResetChildrenOnFormat) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
Compare node structure and user attributes.
109 110 111 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 109 def ==(other) other.class == self.class && structural_state == other.send(:structural_state) end |
#add(parent) ⇒ DiagramItem
67 68 69 70 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 67 def add(parent) parent.children.push self self end |
#apply_user_attributes(id, data_attrs, cls) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 149 def apply_user_attributes(id, data_attrs, cls) if id raise InvalidArgument, 'id must be a valid SVG identifier' unless id.is_a?(String) && /\A[A-Za-z_][\w.-]*\z/.match?(id) @attrs['id'] = id end raise InvalidArgument, 'attrs must be a Hash' unless data_attrs.is_a?(Hash) data_attrs.each do |name, value| raise InvalidArgument, "unsupported SVG attribute: #{name.inspect}" unless name.is_a?(String) && /\Adata-[A-Za-z0-9_-]+\z/.match?(name) @attrs[name] = value.to_s end return if cls.nil? raise InvalidArgument, 'cls must be a String' unless cls.is_a?(String) return if cls.empty? @attrs['class'] = [@attrs['class'], cls].compact.join(' ').strip end |
#child_nodes ⇒ Object
Return the node's direct structural children.
90 91 92 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 90 def child_nodes [] end |
#container_element? ⇒ Boolean
193 194 195 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 193 def container_element? %w[g svg].include?(@name) end |
#determine_gaps(outer, inner) ⇒ [ Numeric, Numeric ]
206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 206 def determine_gaps(outer, inner) diff = outer - inner case INTERNAL_ALIGNMENT when 'left' [0, diff] when 'right' [diff, 0] else half = diff.is_a?(Integer) && diff.odd? ? diff / 2.0 : diff / 2 [half, half] end end |
#each_node(&block) ⇒ void Also known as: each
This method returns an undefined value.
Visit this node and descendants in preorder.
97 98 99 100 101 102 103 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 97 def each_node(&block) return enum_for(:each_node) unless block block.call(self) child_nodes.each { |child| child.each_node(&block) if child.respond_to?(:each_node) } self end |
#format(x, y, width) ⇒ DiagramItem
56 57 58 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 56 def format(x, y, width) raise NotImplementedError end |
#hash ⇒ Object
Hash the same structural state used by equality.
117 118 119 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 117 def hash [self.class, structural_state].hash end |
#inspect ⇒ String
137 138 139 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 137 def inspect Introspection.call(self) end |
#structural_state ⇒ Object
143 144 145 146 147 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 143 def structural_state to_h rescue ParseError [@name, @attrs, @items&.map { |item| item.is_a?(Style) ? item.to_s : item.to_h }] end |
#text_diagram ⇒ TextDiagram
61 62 63 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 61 def text_diagram raise NotImplementedError, "#{self.class}#text_diagram is not implemented" end |
#to_str ⇒ String
129 130 131 132 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 129 def to_str Deprecation.warn('DiagramItem#to_str is deprecated; use #inspect') inspect end |
#walk(callback) ⇒ void
This method returns an undefined value.
83 84 85 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 83 def walk(callback) callback.call(self) end |
#wrap_string(value) ⇒ DiagramItem
199 200 201 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 199 def wrap_string(value) value.is_a?(DiagramItem) ? value : Terminal.new(value) end |
#write_children(write) ⇒ void
This method returns an undefined value.
182 183 184 185 186 187 188 189 190 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 182 def write_children(write) @children.each do |child| if child.respond_to?(:write_svg) child.write_svg(write) else write.call(RailroadDiagrams.escape_html(child)) end end end |
#write_opening_tag(write) ⇒ void
This method returns an undefined value.
171 172 173 174 175 176 177 178 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 171 def write_opening_tag(write) write.call("<#{@name}") @attrs.sort.each do |name, value| write.call(" #{name}=\"#{RailroadDiagrams.escape_attr(value)}\"") end write.call('>') write.call("\n") if container_element? end |
#write_standalone(write, _css = nil) ⇒ void
This method returns an undefined value.
124 125 126 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 124 def write_standalone(write, _css = nil) write_svg(write) end |
#write_svg(write) ⇒ void
This method returns an undefined value.
74 75 76 77 78 79 |
# File 'lib/railroad_diagrams/diagram_item.rb', line 74 def write_svg(write) write = Writer.wrap(write) write_opening_tag(write) write_children(write) write.call("</#{@name}>") end |