Class: Diagrams::FlowchartDiagram
- Defined in:
- lib/diagrams/flowchart_diagram.rb
Overview
Represents a flowchart diagram consisting of nodes and edges connecting them.
Instance Attribute Summary collapse
-
#edges ⇒ Object
readonly
Returns the value of attribute edges.
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
Attributes inherited from Base
Class Method Summary collapse
-
.from_h(data_hash, version:, checksum:) ⇒ FlowchartDiagram
Class method to create a FlowchartDiagram from a hash.
Instance Method Summary collapse
-
#add_edge(edge) ⇒ Element::Edge
Adds an edge to the diagram.
-
#add_node(node) ⇒ Element::Node
Adds a node to the diagram.
-
#find_node(node_id) ⇒ Element::Node?
Finds a node by its ID.
-
#identifiable_elements ⇒ Hash{Symbol => Array<Diagrams::Elements::Node | Diagrams::Elements::Edge>}
Returns a hash mapping element types to their collections for diffing.
-
#initialize(nodes: [], edges: [], version: 1) ⇒ FlowchartDiagram
constructor
Initializes a new FlowchartDiagram.
-
#to_h_content ⇒ Hash{Symbol => Array<Hash>}
Returns the specific content of the flowchart diagram as a hash.
Methods inherited from Base
#diff, from_hash, from_json, #to_h, #to_json
Constructor Details
#initialize(nodes: [], edges: [], version: 1) ⇒ FlowchartDiagram
Initializes a new FlowchartDiagram.
13 14 15 16 17 18 19 |
# File 'lib/diagrams/flowchart_diagram.rb', line 13 def initialize(nodes: [], edges: [], version: 1) super(version:) @nodes = Array(nodes) @edges = Array(edges) validate_elements! update_checksum! end |
Instance Attribute Details
#edges ⇒ Object (readonly)
Returns the value of attribute edges.
6 7 8 |
# File 'lib/diagrams/flowchart_diagram.rb', line 6 def edges @edges end |
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
6 7 8 |
# File 'lib/diagrams/flowchart_diagram.rb', line 6 def nodes @nodes end |
Class Method Details
.from_h(data_hash, version:, checksum:) ⇒ FlowchartDiagram
Class method to create a FlowchartDiagram from a hash. Used by the deserialization factory in ‘Diagrams::Base`.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/diagrams/flowchart_diagram.rb', line 87 def self.from_h(data_hash, version:, checksum:) nodes_data = data_hash[:nodes] || data_hash['nodes'] || [] edges_data = data_hash[:edges] || data_hash['edges'] || [] nodes = nodes_data.map do |node_h| Diagrams::Elements::Node.new(node_h.transform_keys(&:to_sym)) end edges = edges_data.map do |edge_h| Diagrams::Elements::Edge.new(edge_h.transform_keys(&:to_sym)) end diagram = new(nodes:, edges:, version:) # Optional: Verify checksum if provided if checksum && diagram.checksum != checksum warn "Checksum mismatch for loaded FlowchartDiagram (version: #{version}). Expected #{checksum}, got #{diagram.checksum}." # Or raise an error: raise "Checksum mismatch..." end diagram end |
Instance Method Details
#add_edge(edge) ⇒ Element::Edge
Adds an edge to the diagram.
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/diagrams/flowchart_diagram.rb', line 40 def add_edge(edge) raise ArgumentError, 'Edge must be a Diagrams::Element::Edge' unless edge.is_a?(Diagrams::Elements::Edge) unless find_node(edge.source_id) && find_node(edge.target_id) raise ArgumentError, "Edge refers to non-existent node IDs ('#{edge.source_id}' or '#{edge.target_id}')" end @edges << edge update_checksum! edge end |
#add_node(node) ⇒ Element::Node
Adds a node to the diagram.
26 27 28 29 30 31 32 33 |
# File 'lib/diagrams/flowchart_diagram.rb', line 26 def add_node(node) raise ArgumentError, 'Node must be a Diagrams::Element::Node' unless node.is_a?(Diagrams::Elements::Node) raise ArgumentError, "Node with ID '#{node.id}' already exists" if find_node(node.id) @nodes << node update_checksum! node end |
#find_node(node_id) ⇒ Element::Node?
Finds a node by its ID.
55 56 57 |
# File 'lib/diagrams/flowchart_diagram.rb', line 55 def find_node(node_id) @nodes.find { |n| n.id == node_id } end |
#identifiable_elements ⇒ Hash{Symbol => Array<Diagrams::Elements::Node | Diagrams::Elements::Edge>}
Returns a hash mapping element types to their collections for diffing.
73 74 75 76 77 78 |
# File 'lib/diagrams/flowchart_diagram.rb', line 73 def identifiable_elements { nodes: @nodes, edges: @edges } end |
#to_h_content ⇒ Hash{Symbol => Array<Hash>}
Returns the specific content of the flowchart diagram as a hash. Called by ‘Diagrams::Base#to_h`.
63 64 65 66 67 68 |
# File 'lib/diagrams/flowchart_diagram.rb', line 63 def to_h_content { nodes: @nodes.map(&:to_h), edges: @edges.map(&:to_h) } end |