dsl-graph 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 15f6e7b0b4ee9295d215a4756c95a7c917e1f9fc708826fb4df7e815fa05f055
4
+ data.tar.gz: c0214d18168d4e8e9ba1f1522e0553608cf51d1a9736048aa38f8aab067b22b6
5
+ SHA512:
6
+ metadata.gz: bf4a00bf704d9ddcbe77021cbeb3c7e138af271e02accd01aa35f6980af32ff393135723ee4695737c99059de3b3d509fd112aeab5fa5de35613ee130478aea6
7
+ data.tar.gz: 1a10df96eaf114ea84b4f301c6499ac720e974add568fede6d41e4a5d24aee695538172bd0c71a32080f82f2bead02c3604edc8387ad6fba5212a28acd693921
data/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.2.0] - 2026-04-21
4
+
5
+ - Add features: export (pdf, yaml), load (yaml)
6
+
7
+ ## [0.1.0] - 2026-04-19
8
+
9
+ - Initial release
10
+ - Add features: create, debug
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 David Vargas
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,17 @@
1
+ # Dsl::Graph
2
+
3
+ DSL para crear grafos.
4
+
5
+ ## Instalación
6
+
7
+ * Instalar Ruby
8
+ * Instalar Graphviz: `sudo zypper install graphviz`
9
+ * `gem install dsl-graph`
10
+
11
+ ## Modo de uso
12
+
13
+ > Consultar los [ejemplos](./examples/)
14
+
15
+ ## Contributing
16
+
17
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dvarrui/dsl-graph.
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ require "standard/rake"
13
+
14
+ task default: %i[test standard]
data/data/.gitkeep ADDED
File without changes
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "graphviz"
4
+
5
+ # Jedi
6
+ # / | \
7
+ # Yoda Obiwan Ankin
8
+
9
+ # Crear los nodos
10
+ graph = Graphviz::Graph.new("Jedi")
11
+
12
+ n0 = graph.add_node("Jedi")
13
+ n1 = graph.add_node("Yoda")
14
+ n2 = graph.add_node("Obiwan")
15
+ n3 = graph.add_node("Anakin")
16
+
17
+ # Conectar los nodos
18
+ e1 = n0.connect(n1)
19
+ e2 = n0.connect(n2)
20
+ e3 = n0.connect(n3)
21
+
22
+ # Crear pdf
23
+ Graphviz::output(graph, :path => "data/test.pdf")
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative "../lib/dsl/graph"
3
+
4
+ puts "==> Create"
5
+
6
+ graph do
7
+ n1 = add_node("Obiwan")
8
+ n2 = add_node("Anakin")
9
+ n3 = add_node("Yoda")
10
+
11
+ add_edge(n1, n2, "maestro_de")
12
+ add_edge(n3, n1, "maestro_de")
13
+ end
14
+
15
+ puts "==> Debug"
16
+ graph.debug
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative "../lib/dsl/graph"
3
+
4
+ graph do
5
+ n1 = add_node("Obiwan")
6
+ n2 = add_node("Anakin")
7
+ n3 = add_node("Yoda")
8
+
9
+ add_edge(n1, n2, "maestro_de")
10
+ add_edge(n3, n1, "maestro_de")
11
+ end
12
+
13
+ puts "==> Export"
14
+
15
+ graph.export("data/2-graph.pdf")
16
+ graph.export("data/2-graph.yaml")
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative "../lib/dsl/graph"
3
+
4
+ puts "==> Create"
5
+
6
+ g1 = graph "demo1" do
7
+ n1 = add_node("Obiwan")
8
+ n2 = add_node("Anakin")
9
+ n3 = add_node("Yoda")
10
+
11
+ add_edge(n1, n2, "maestro_de")
12
+ add_edge(n3, n1, "maestro_de")
13
+ end
14
+
15
+ g1.debug
16
+ puts "==> Export"
17
+ g1.export("data/3-graph.yaml")
18
+
19
+ puts "==> Load"
20
+ g2 = graph.load("data/3-graph.yaml")
21
+ g2.label = "demo2"
22
+ g2.debug
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dsl
4
+ module Graph
5
+ class Edge
6
+ attr_reader :id
7
+ attr_accessor :from
8
+ attr_accessor :to
9
+ attr_accessor :label
10
+
11
+ def initialize(id, from, to, label="")
12
+ @id = id
13
+ @from = from
14
+ @to = to
15
+ @label = label
16
+ end
17
+
18
+ def to_s
19
+ @label
20
+ end
21
+ end
22
+ end
23
+ end
24
+
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+ require "graphviz"
3
+ require "yaml"
4
+
5
+ module Dsl::Graph
6
+ class ExportGraph
7
+ def initialize(graph, filename)
8
+ @graph = graph
9
+ @filename = filename
10
+ end
11
+
12
+ def call
13
+ ext = File.extname(@filename.downcase)
14
+
15
+ case ext
16
+ when ".pdf"
17
+ export_to_pdf
18
+ when ".yaml", ".yml"
19
+ export_to_yaml
20
+ else
21
+ warn "[ERROR] Unkown export format: (#{ext})"
22
+ exit 1
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def export_to_pdf
29
+ graph = @graph
30
+ filename = @filename
31
+ viz = Graphviz::Graph.new(graph.label)
32
+ nodes = {}
33
+ graph.nodes.each do |k,v|
34
+ nodes[v.label] = viz.add_node(v.label)
35
+ end
36
+
37
+ graph.edges.each do |k,v|
38
+ n1 = nodes[v.from.label]
39
+ n2 = nodes[v.to.label]
40
+ n1.connect(n2)
41
+ end
42
+
43
+ Graphviz::output(viz, :path => filename)
44
+ end
45
+
46
+ def export_to_yaml
47
+ data = {
48
+ "graph" => {
49
+ "label" => @graph.label,
50
+ "nodes" => @graph.nodes.values.map { |n| { "label" => n.label } },
51
+ "edges" => @graph.edges.values.map do |e|
52
+ {
53
+ "from" => e.from.label,
54
+ "label" => e.label,
55
+ "to" => e.to.label
56
+ }
57
+ end
58
+ }
59
+ }
60
+
61
+ File.write(@filename, data.to_yaml)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "edge"
4
+ require_relative "export"
5
+ require_relative "load"
6
+ require_relative "node"
7
+
8
+ module Dsl
9
+ module Graph
10
+ class Graph
11
+ attr_accessor :label
12
+ attr_reader :nodes
13
+ attr_reader :edges
14
+
15
+ def initialize(label="graph")
16
+ @label = label
17
+ @next = { node: 0, edge: 0}
18
+ @nodes = {}
19
+ @edges = {}
20
+ end
21
+
22
+ def add_node(label)
23
+ id = (@next[:node] += 1)
24
+ node = Node.new(id, label)
25
+ @nodes[id] = node
26
+ node
27
+ end
28
+
29
+ def add_edge(node1, node2, label)
30
+ id = (@next[:edge] += 1)
31
+ edge = Edge.new(id, node1, node2, label)
32
+ @edges[id] = edge
33
+ edge
34
+ end
35
+
36
+ def debug
37
+ puts "graph: #{@label}"
38
+ puts "> nodes (#{@nodes.size})"
39
+ @nodes.each do |id, node|
40
+ puts " - node(#{node.id}): #{node} "
41
+ end
42
+ puts "> edges (#{@edges.size})"
43
+ @edges.each do |id, edge|
44
+ puts " - edge(#{edge.id}): #{edge.from} --(#{edge})--> #{edge.to}"
45
+ end
46
+ end
47
+
48
+ def export(filename=nil)
49
+ if filename.nil?
50
+ filename = "#{graph.label}.yaml"
51
+ filename = "graph.yaml" if graph.label.nil? || graph.label.empty?
52
+ end
53
+ ExportGraph.new(self, filename).call
54
+ end
55
+
56
+ def load(filename=nil)
57
+ if filename.nil?
58
+ filename = "#{graph.label}.yaml"
59
+ filename = "graph.yaml" if graph.label.nil? || graph.label.empty?
60
+ end
61
+ LoadGraph.new(filename).call
62
+ end
63
+
64
+ def run(&block)
65
+ instance_eval(&block)
66
+ self
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,34 @@
1
+
2
+ # frozen_string_literal: true
3
+
4
+ require "yaml"
5
+
6
+ module Dsl::Graph
7
+ class LoadGraph
8
+ def initialize(filename)
9
+ @filename = filename
10
+ end
11
+
12
+ def call
13
+ data = YAML.load_file(@filename)
14
+ graph_data = data["graph"]
15
+
16
+ graph = Graph.new(graph_data["label"])
17
+ nodes_by_label = {}
18
+
19
+ graph_data["nodes"].each do |node_data|
20
+ label = node_data["label"]
21
+ nodes_by_label[label] = graph.add_node(label)
22
+ end
23
+
24
+ graph_data["edges"].each do |edge_data|
25
+ from_node = nodes_by_label[edge_data["from"]]
26
+ to_node = nodes_by_label[edge_data["to"]]
27
+ label = edge_data["label"]
28
+ graph.add_edge(from_node, to_node, label)
29
+ end
30
+
31
+ graph
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dsl::Graph
4
+ class Node
5
+ attr_reader :id
6
+ attr_accessor :label
7
+
8
+ def initialize(id, label)
9
+ @id = id
10
+ @label = label
11
+ end
12
+
13
+ def to_s
14
+ @label
15
+ end
16
+ end
17
+ end
18
+
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dsl
4
+ module Graph
5
+ VERSION = "0.2.0"
6
+ end
7
+ end
data/lib/dsl/graph.rb ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "graph/graph"
4
+ require_relative "graph/version"
5
+ require "debug"
6
+
7
+ def graph(label=nil, &block)
8
+ $GRAPH ||= Dsl::Graph::Graph.new(label)
9
+ return $GRAPH if block.nil?
10
+
11
+ $GRAPH.run(&block)
12
+ $GRAPH
13
+ end
data/sig/dsl/graph.rbs ADDED
@@ -0,0 +1,6 @@
1
+ module Dsl
2
+ module Graph
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dsl-graph
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - David Vargas Ruiz
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: graphviz
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.3'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '1.3'
26
+ description: DSL para crear grafos.
27
+ email:
28
+ - dvarrui@proton.me
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - CHANGELOG.md
34
+ - LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - data/.gitkeep
38
+ - docs/graphviz-example.rb
39
+ - examples/1-create.rb
40
+ - examples/2-export.rb
41
+ - examples/3.load.rb
42
+ - lib/dsl/graph.rb
43
+ - lib/dsl/graph/edge.rb
44
+ - lib/dsl/graph/export.rb
45
+ - lib/dsl/graph/graph.rb
46
+ - lib/dsl/graph/load.rb
47
+ - lib/dsl/graph/node.rb
48
+ - lib/dsl/graph/version.rb
49
+ - sig/dsl/graph.rbs
50
+ homepage: https://github.com/dvarrui/dsl-graph
51
+ licenses: []
52
+ metadata:
53
+ homepage_uri: https://github.com/dvarrui/dsl-graph
54
+ source_code_uri: https://github.com/dvarrui/dsl-graph
55
+ changelog_uri: https://github.com/dvarrui/dsl-graph/blob/main/CHANGELOG.md
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 3.2.0
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 4.0.10
71
+ specification_version: 4
72
+ summary: DSL para crear grafos.
73
+ test_files: []