-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_usage.rb
More file actions
232 lines (194 loc) · 6.68 KB
/
basic_usage.rb
File metadata and controls
232 lines (194 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
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
139
140
141
142
143
144
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env ruby
# frozen_string_literal: true
# Basic Usage Examples for Rails Flow Map
#
# This file demonstrates the most common usage patterns for Rails Flow Map.
# Run with: ruby examples/basic_usage.rb
require_relative '../lib/rails_flow_map'
class BasicUsageExamples
def self.run_all
puts "🚀 Rails Flow Map - Basic Usage Examples"
puts "=" * 50
# Create a sample graph for demonstration
graph = create_sample_graph
example_1_basic_export(graph)
example_2_multiple_formats(graph)
example_3_file_output(graph)
example_4_configuration(graph)
example_5_error_handling(graph)
puts "\n✅ All examples completed successfully!"
end
# Example 1: Basic graph analysis and export
def self.example_1_basic_export(graph)
puts "\n📋 Example 1: Basic Export"
puts "-" * 30
# Generate Mermaid diagram
mermaid_output = RailsFlowMap.export(graph, format: :mermaid)
puts "Generated Mermaid diagram:"
puts mermaid_output[0..200] + "..." # Show first 200 characters
puts "✓ Mermaid export successful"
end
# Example 2: Multiple output formats
def self.example_2_multiple_formats(graph)
puts "\n🎨 Example 2: Multiple Formats"
puts "-" * 30
formats = [:mermaid, :plantuml, :erd, :metrics]
formats.each do |format|
begin
output = RailsFlowMap.export(graph, format: format)
puts "✓ #{format.to_s.capitalize} format: #{output.length} characters"
rescue => e
puts "✗ #{format.to_s.capitalize} format failed: #{e.message}"
end
end
end
# Example 3: Saving to files
def self.example_3_file_output(graph)
puts "\n💾 Example 3: File Output"
puts "-" * 30
# Create temporary directory for examples
output_dir = File.join(Dir.pwd, 'tmp', 'flow_map_examples')
FileUtils.mkdir_p(output_dir)
files = {
mermaid: File.join(output_dir, 'architecture.md'),
d3js: File.join(output_dir, 'interactive.html'),
openapi: File.join(output_dir, 'api_spec.yaml'),
metrics: File.join(output_dir, 'metrics.md')
}
files.each do |format, filepath|
begin
RailsFlowMap.export(graph, format: format, output: filepath)
if File.exist?(filepath)
file_size = File.size(filepath)
puts "✓ #{format}: #{filepath} (#{file_size} bytes)"
else
puts "✗ #{format}: File not created"
end
rescue => e
puts "✗ #{format}: Export failed - #{e.message}"
end
end
puts "\n📁 Files saved to: #{output_dir}"
end
# Example 4: Using configuration
def self.example_4_configuration(graph)
puts "\n⚙️ Example 4: Configuration"
puts "-" * 30
# Configure Rails Flow Map
RailsFlowMap.configure do |config|
# These would normally be set based on your Rails app
config.output_dir = 'tmp/flow_maps'
config.excluded_paths = ['vendor/', 'tmp/']
end
# Export with configuration applied
output = RailsFlowMap.export(graph, format: :mermaid)
puts "✓ Configuration applied successfully"
puts "✓ Output directory: #{RailsFlowMap.configuration.output_dir}"
puts "✓ Excluded paths: #{RailsFlowMap.configuration.excluded_paths.join(', ')}"
end
# Example 5: Error handling
def self.example_5_error_handling(graph)
puts "\n🛡️ Example 5: Error Handling"
puts "-" * 30
# Test invalid format
begin
RailsFlowMap.export(graph, format: :invalid_format)
rescue RailsFlowMap::InvalidInputError => e
puts "✓ Caught expected error: #{e.class.name}"
puts " Message: #{e.message}"
puts " Error code: #{e.error_code}"
end
# Test invalid output path
begin
RailsFlowMap.export(graph, format: :mermaid, output: '/etc/passwd')
rescue RailsFlowMap::SecurityError => e
puts "✓ Caught security error: #{e.class.name}"
puts " Message: #{e.message}"
end
# Test with invalid graph
begin
RailsFlowMap.export("not a graph", format: :mermaid)
rescue RailsFlowMap::InvalidInputError => e
puts "✓ Caught validation error: #{e.class.name}"
puts " Context: #{e.context}"
end
end
private
# Create a sample graph for demonstration
def self.create_sample_graph
graph = RailsFlowMap::FlowGraph.new
# Add sample nodes
user_model = RailsFlowMap::FlowNode.new(
id: 'user',
name: 'User',
type: :model,
attributes: {
associations: ['has_many :posts', 'has_many :comments'],
validations: ['validates :email, presence: true']
}
)
post_model = RailsFlowMap::FlowNode.new(
id: 'post',
name: 'Post',
type: :model,
attributes: {
associations: ['belongs_to :user', 'has_many :comments']
}
)
comment_model = RailsFlowMap::FlowNode.new(
id: 'comment',
name: 'Comment',
type: :model,
attributes: {
associations: ['belongs_to :user', 'belongs_to :post']
}
)
users_controller = RailsFlowMap::FlowNode.new(
id: 'users_controller',
name: 'UsersController',
type: :controller,
attributes: {
actions: ['index', 'show', 'create', 'update', 'destroy']
}
)
posts_controller = RailsFlowMap::FlowNode.new(
id: 'posts_controller',
name: 'PostsController',
type: :controller,
attributes: {
actions: ['index', 'show', 'create', 'update', 'destroy']
}
)
# Add route nodes
users_route = RailsFlowMap::FlowNode.new(
id: 'users_route',
name: 'GET /api/v1/users',
type: :route,
attributes: {
path: '/api/v1/users',
verb: 'GET',
controller: 'api/v1/users',
action: 'index'
}
)
# Add nodes to graph
[user_model, post_model, comment_model, users_controller, posts_controller, users_route].each do |node|
graph.add_node(node)
end
# Add edges (relationships)
edges = [
RailsFlowMap::FlowEdge.new(from: 'post', to: 'user', type: :belongs_to),
RailsFlowMap::FlowEdge.new(from: 'comment', to: 'user', type: :belongs_to),
RailsFlowMap::FlowEdge.new(from: 'comment', to: 'post', type: :belongs_to),
RailsFlowMap::FlowEdge.new(from: 'users_controller', to: 'user', type: :uses_model),
RailsFlowMap::FlowEdge.new(from: 'posts_controller', to: 'post', type: :uses_model),
RailsFlowMap::FlowEdge.new(from: 'users_route', to: 'users_controller', type: :routes_to)
]
edges.each { |edge| graph.add_edge(edge) }
graph
end
end
# Run examples if this file is executed directly
if __FILE__ == $0
BasicUsageExamples.run_all
end