Class: CircuitBreaker::Visualizer
- Inherits:
-
Object
- Object
- CircuitBreaker::Visualizer
- Defined in:
- lib/circuit_breaker/visualizer.rb
Constant Summary collapse
- TEMPLATES_DIR =
File.join(File.dirname(__FILE__), 'templates')
Class Method Summary collapse
- .save(token_class, format:, filename:) ⇒ Object
- .to_dot(token_class) ⇒ Object
- .to_html(token_class, engine = :mermaid) ⇒ Object
- .to_markdown(token_class) ⇒ Object
- .to_mermaid(token_class) ⇒ Object
- .to_plantuml(token_class) ⇒ Object
Class Method Details
.save(token_class, format:, filename:) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/circuit_breaker/visualizer.rb', line 153 def save(token_class, format:, filename:) content = case format when :mermaid to_mermaid(token_class) when :dot to_dot(token_class) when :plantuml to_plantuml(token_class) when :html to_html(token_class) when :markdown to_markdown(token_class) else raise ArgumentError, "Unsupported format: #{format}" end File.write(filename, content) end |
.to_dot(token_class) ⇒ Object
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 |
# File 'lib/circuit_breaker/visualizer.rb', line 41 def to_dot(token_class) transitions = token_class.state_transitions validations = token_class.state_validations lines = ["digraph {"] lines << " rankdir=LR;" # Style definitions lines << " node [shape=circle];" lines << " edge [fontsize=10];" # Add states with validation info transitions.keys.concat(transitions.values.flatten).uniq.each do |state| validation = validations[state] ? "with validation" : "no validation" lines << " #{state} [label=\"#{state}\\n#{validation}\"];" end # Add transitions transitions.each do |from, to_states| to_states.each do |to| lines << " #{from} -> #{to};" end end lines << "}" lines.join("\n") end |
.to_html(token_class, engine = :mermaid) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/circuit_breaker/visualizer.rb', line 99 def to_html(token_class, engine = :mermaid) case engine when :mermaid diagram = to_mermaid(token_class) template_path = File.join(TEMPLATES_DIR, 'mermaid.html.erb') when :plantuml diagram = to_plantuml(token_class) template_path = File.join(TEMPLATES_DIR, 'plantuml.html.erb') else raise ArgumentError, "Unsupported engine: #{engine}" end template = ERB.new(File.read(template_path)) template.result(binding) end |
.to_markdown(token_class) ⇒ Object
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 |
# File 'lib/circuit_breaker/visualizer.rb', line 115 def to_markdown(token_class) transitions = token_class.state_transitions states = transitions.keys.concat(transitions.values.flatten).uniq validations = token_class.state_validations lines = ["# #{token_class.name} Workflow\n"] lines << "## States\n" states.each do |state| lines << "### #{state.to_s.capitalize}\n" if validations[state] lines << "**Validations:**\n" lines << "- State-specific validation rules are enforced\n" end lines << "\n" end lines << "## Transitions\n" transitions.each do |from, to_states| to_states.each do |to| lines << "### #{from} → #{to}\n" if token_class.transition_rules[[from, to]] lines << "**Rules:**\n" lines << "- Transition-specific validation rules are enforced\n" end lines << "\n" end end lines << "## Hooks\n" lines << "### Before Transition\n" lines << "- #{token_class.before_transition_hooks.size} hooks registered\n\n" lines << "### After Transition\n" lines << "- #{token_class.after_transition_hooks.size} hooks registered\n\n" lines.join end |
.to_mermaid(token_class) ⇒ Object
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 |
# File 'lib/circuit_breaker/visualizer.rb', line 9 def to_mermaid(token_class) transitions = token_class.state_transitions states = transitions.keys.concat(transitions.values.flatten).uniq validations = token_class.state_validations lines = ["stateDiagram-v2"] # Add states with validation notes states.each do |state| lines << " #{state}" if validations[state] lines << " note right of #{state}" # Get the file path relative to the project root validation_file = validations[state].source_location.first relative_path = validation_file.sub(File.('../../..', __FILE__), '') relative_path = relative_path.start_with?('/') ? relative_path[1..-1] : relative_path lines << " Validations in:" lines << " #{relative_path}" lines << " end note" end end # Add transitions with hooks transitions.each do |from, to_states| to_states.each do |to| lines << " #{from} --> #{to}" end end lines.join("\n") end |
.to_plantuml(token_class) ⇒ Object
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 |
# File 'lib/circuit_breaker/visualizer.rb', line 69 def to_plantuml(token_class) transitions = token_class.state_transitions validations = token_class.state_validations lines = ["@startuml"] lines << "skinparam monochrome true" lines << "skinparam defaultFontName Arial" # Add states transitions.keys.concat(transitions.values.flatten).uniq.each do |state| if validations[state] lines << "state #{state} {" lines << " note right: Has validations" lines << "}" else lines << "state #{state}" end end # Add transitions transitions.each do |from, to_states| to_states.each do |to| lines << "#{from} --> #{to}" end end lines << "@enduml" lines.join("\n") end |