class-metrix 0.1.2 → 1.0.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.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/.editorconfig +48 -0
  3. data/.vscode/README.md +72 -0
  4. data/.vscode/extensions.json +28 -0
  5. data/.vscode/launch.json +32 -0
  6. data/.vscode/settings.json +88 -0
  7. data/.vscode/tasks.json +99 -0
  8. data/CHANGELOG.md +71 -4
  9. data/README.md +41 -7
  10. data/docs/ARCHITECTURE.md +501 -0
  11. data/examples/README.md +161 -114
  12. data/examples/basic_usage.rb +88 -0
  13. data/examples/debug_levels_demo.rb +65 -0
  14. data/examples/debug_mode_demo.rb +75 -0
  15. data/examples/inheritance_and_modules.rb +155 -0
  16. data/lib/class_metrix/extractor.rb +106 -11
  17. data/lib/class_metrix/extractors/constants_extractor.rb +155 -21
  18. data/lib/class_metrix/extractors/methods_extractor.rb +186 -21
  19. data/lib/class_metrix/extractors/multi_type_extractor.rb +6 -5
  20. data/lib/class_metrix/formatters/components/footer_component.rb +1 -1
  21. data/lib/class_metrix/formatters/components/table_component/column_width_calculator.rb +56 -0
  22. data/lib/class_metrix/formatters/components/table_component/row_processor.rb +138 -0
  23. data/lib/class_metrix/formatters/components/table_component/table_data_extractor.rb +54 -0
  24. data/lib/class_metrix/formatters/components/table_component/table_renderer.rb +55 -0
  25. data/lib/class_metrix/formatters/components/table_component.rb +30 -244
  26. data/lib/class_metrix/formatters/shared/markdown_table_builder.rb +10 -5
  27. data/lib/class_metrix/formatters/shared/table_builder.rb +84 -21
  28. data/lib/class_metrix/formatters/shared/value_processor.rb +72 -16
  29. data/lib/class_metrix/utils/debug_logger.rb +159 -0
  30. data/lib/class_metrix/version.rb +1 -1
  31. metadata +17 -9
  32. data/examples/advanced/error_handling.rb +0 -199
  33. data/examples/advanced/hash_expansion.rb +0 -180
  34. data/examples/basic/01_simple_constants.rb +0 -56
  35. data/examples/basic/02_simple_methods.rb +0 -99
  36. data/examples/basic/03_multi_type_extraction.rb +0 -116
  37. data/examples/components/configurable_reports.rb +0 -201
  38. data/examples/csv_output_demo.rb +0 -237
  39. data/examples/real_world/microservices_audit.rb +0 -312
data/examples/README.md CHANGED
@@ -1,155 +1,202 @@
1
1
  # ClassMetrix Examples
2
2
 
3
- This directory contains comprehensive examples demonstrating all features of the ClassMetrix gem, organized from basic to advanced real-world scenarios.
3
+ This directory contains examples demonstrating ClassMetrix features, from basic usage to advanced inheritance and module analysis.
4
4
 
5
5
  ## 🎯 Quick Start
6
6
 
7
- If you're new to ClassMetrix, start with the basic examples:
7
+ New to ClassMetrix? Start with these examples:
8
8
 
9
9
  ```bash
10
- # Run basic examples in order
11
- ruby examples/basic/01_simple_constants.rb
12
- ruby examples/basic/02_simple_methods.rb
13
- ruby examples/basic/03_multi_type_extraction.rb
10
+ ruby examples/basic_usage.rb # Basic constants and methods
11
+ ruby examples/inheritance_and_modules.rb # Inheritance and module analysis
14
12
  ```
15
13
 
16
- ## 📁 Directory Structure
14
+ ## 📁 Available Examples
17
15
 
18
- ### `/basic/` - Getting Started
19
- Perfect for learning the core concepts and API.
16
+ ### Core Examples
20
17
 
21
- - **`01_simple_constants.rb`** - Extract and compare constants across classes
22
- - Basic constant extraction
23
- - Filtering with regex and strings
24
- - Understanding the output format
18
+ - **`basic_usage.rb`** - Basic constant and method extraction
19
+ - Simple class comparison
20
+ - Filtering and multi-type extraction
21
+ - Hash expansion basics
25
22
 
26
- - **`02_simple_methods.rb`** - Extract and compare class methods
27
- - Class method extraction
28
- - Handling different return types (strings, numbers, booleans, arrays)
29
- - Method filtering techniques
23
+ - **`inheritance_and_modules.rb`** - Inheritance and module analysis
24
+ - Class inheritance support
25
+ - Module inclusion analysis
26
+ - Combined inheritance and module extraction
30
27
 
31
- - **`03_multi_type_extraction.rb`** - Combine constants and methods in one table
32
- - Multi-type extraction with Type column
33
- - Comparing constants vs methods with similar names
34
- - Understanding behavior differences
28
+ ### Advanced Examples
35
29
 
36
- ### `/advanced/` - Powerful Features
37
- Advanced features for complex scenarios.
30
+ - **`csv_output_demo.rb`** - CSV output format demonstration
31
+ - **`/advanced/`** - Complex hash expansion scenarios
32
+ - **`/basic/`** - Step-by-step learning examples
33
+ - **`/real_world/`** - Production use case examples
38
34
 
39
- - **`hash_expansion.rb`** - Hash value expansion into sub-rows
40
- - Normal vs expanded hash display
41
- - Hash key comparison across classes
42
- - Mixed data type handling
43
- - Multi-type extraction with expansion
35
+ ## 🚀 Core Functionality
44
36
 
45
- - **`error_handling.rb`** - Graceful error handling
46
- - Methods that raise exceptions
47
- - Missing constants and methods
48
- - Boolean and nil value processing
49
- - Error indicators and recovery
37
+ ### Basic Extraction
50
38
 
51
- ### `/real_world/` - Production Examples
52
- Real-world scenarios you might encounter.
39
+ ```ruby
40
+ require_relative "../lib/class_metrix"
53
41
 
54
- - **`microservices_audit.rb`** - Complete microservices configuration audit
55
- - Service metadata comparison
56
- - Feature flag consistency analysis
57
- - Database and cache configuration audits
58
- - Performance metrics comparison
59
- - Security configuration analysis
42
+ # Extract constants from multiple classes
43
+ ClassMetrix.extract(:constants)
44
+ .from([User, Admin])
45
+ .to_markdown
46
+
47
+ # Extract class methods
48
+ ClassMetrix.extract(:class_methods)
49
+ .from([DatabaseService, CacheService])
50
+ .to_markdown
51
+
52
+ # Combined extraction
53
+ ClassMetrix.extract(:constants, :class_methods)
54
+ .from([Class1, Class2])
55
+ .filter(/config/)
56
+ .to_markdown
57
+ ```
60
58
 
61
- ## 🚀 Running Examples
59
+ ### Inheritance & Module Analysis
62
60
 
63
- Each example is self-contained and can be run independently:
61
+ ```ruby
62
+ # Include inherited constants and methods
63
+ ClassMetrix.extract(:constants, :class_methods)
64
+ .from([ChildClass])
65
+ .include_inherited
66
+ .to_markdown
67
+
68
+ # Include module constants and methods
69
+ ClassMetrix.extract(:constants, :class_methods)
70
+ .from([ClassWithModules])
71
+ .include_modules
72
+ .to_markdown
73
+
74
+ # Complete analysis: own + inherited + modules
75
+ ClassMetrix.extract(:constants, :class_methods)
76
+ .from([ComplexClass])
77
+ .include_all
78
+ .to_markdown
79
+ ```
64
80
 
65
- ```bash
66
- # Run any example directly
67
- ruby examples/basic/01_simple_constants.rb
68
- ruby examples/advanced/hash_expansion.rb
69
- ruby examples/real_world/microservices_audit.rb
81
+ ### Advanced Features
82
+
83
+ ```ruby
84
+ # Hash expansion for detailed analysis
85
+ ClassMetrix.extract(:constants, :class_methods)
86
+ .from([ConfigClass])
87
+ .expand_hashes
88
+ .to_markdown
89
+
90
+ # Hash expansion (main rows only - default)
91
+ ClassMetrix.extract(:class_methods)
92
+ .from([ServiceClass])
93
+ .expand_hashes
94
+ .to_markdown
95
+
96
+ # Show only key rows (detailed view)
97
+ ClassMetrix.extract(:class_methods)
98
+ .from([ServiceClass])
99
+ .expand_hashes
100
+ .show_only_keys
101
+ .to_markdown
102
+
103
+ # Show both main and key rows
104
+ ClassMetrix.extract(:class_methods)
105
+ .from([ServiceClass])
106
+ .expand_hashes
107
+ .show_expanded_details
108
+ .to_markdown
109
+
110
+ # Error handling for robust extraction
111
+ ClassMetrix.extract(:class_methods)
112
+ .from([ClassWithErrors])
113
+ .handle_errors
114
+ .to_markdown
115
+
116
+ # Save to file
117
+ ClassMetrix.extract(:constants)
118
+ .from([Class1, Class2])
119
+ .to_markdown("analysis.md")
70
120
  ```
71
121
 
72
122
  ## 📊 Example Output
73
123
 
74
- All examples generate markdown tables that look like this:
75
-
124
+ ### Basic Comparison
76
125
  ```markdown
77
- | Constant | UserService | OrderService | PaymentService |
78
- |--------------|-------------|--------------|----------------|
79
- | SERVICE_NAME | user-service| order-service| payment-service|
80
- | VERSION | 2.1.0 | 1.8.5 | 3.0.1 |
81
- | PORT | 8001 | 8002 | 8003 |
126
+ | Constant | User | Admin |
127
+ |----------|---------|---------|
128
+ | ROLE | user | admin |
129
+ | TIMEOUT | 3600 | 7200 |
82
130
  ```
83
131
 
84
- With hash expansion:
132
+ ### With Inheritance
85
133
  ```markdown
86
- | Method | UserService | OrderService | PaymentService |
87
- |----------------|-------------|--------------|----------------|
88
- | database_config| 📋 7 keys | 📋 8 keys | 📋 8 keys |
89
- | └─ host | user-db... | order-db... | payment-db... |
90
- | └─ port | 5432 | 5432 | 5432 |
91
- | └─ ssl | true | true | true |
134
+ | Constant | DatabaseService | CacheService |
135
+ |---------------|-----------------|--------------|
136
+ | SERVICE_NAME | database | cache |
137
+ | SERVICE_VERSION| 1.0 | 1.0 |
138
+ | DEFAULT_TIMEOUT| 30 | 30 |
92
139
  ```
93
140
 
94
- ## 🛠️ Building Your Own Examples
95
-
96
- Use these examples as templates for your own analysis:
97
-
98
- ```ruby
99
- require_relative "../lib/class_metrix"
100
-
101
- # Define your classes...
102
- class MyService
103
- CONFIG_SETTING = "value"
104
-
105
- def self.some_method
106
- "result"
107
- end
108
- end
109
-
110
- # Extract and compare
111
- result = ClassMetrix.extract(:constants, :class_methods)
112
- .from([MyService, AnotherService])
113
- .filter(/CONFIG|method/)
114
- .expand_hashes
115
- .handle_errors
116
- .to_markdown("my_analysis.md")
117
-
118
- puts result
141
+ ### Hash Expansion
142
+ ```markdown
143
+ | Method | Service1 | Service2 |
144
+ |---------------|-----------------|-----------------|
145
+ | config | {...} | {...} |
146
+ | config.host | localhost | production.com |
147
+ | config.port | 3000 | 443 |
148
+ | config.ssl | ❌ | ✅ |
119
149
  ```
120
150
 
121
- ## 📝 Generated Reports
122
-
123
- Many examples save their output to markdown files:
124
-
125
- - `config_analysis_expanded.md` - Hash expansion demo output
126
- - `error_analysis_report.md` - Error handling demo output
127
- - `microservices_audit_report.md` - Complete microservices audit
128
- - `service_analysis_report.md` - Service comparison report
151
+ ## 🛠️ API Reference
152
+
153
+ ### Extraction Types
154
+ - `:constants` - Class constants
155
+ - `:class_methods` - Class methods
156
+
157
+ ### Options
158
+ - `.from(classes)` - Classes to analyze (array)
159
+ - `.filter(pattern)` - Filter by name (regex or string)
160
+ - `.include_inherited` - Include parent class behaviors
161
+ - `.include_modules` - Include module behaviors
162
+ - `.include_all` - Include inherited + modules
163
+ - `.expand_hashes` - Expand hash values (shows main rows by default)
164
+ - `.show_only_main` - Show only main rows (collapsed hashes) - **Default**
165
+ - `.show_only_keys` - Show only key rows (expanded details)
166
+ - `.show_expanded_details` - Show both main and key rows
167
+ - `.handle_errors` - Graceful error handling
168
+ - `.to_markdown(file)` - Generate markdown output
169
+ - `.to_csv(file)` - Generate CSV output
170
+
171
+ ## 💡 Common Use Cases
172
+
173
+ - **Configuration Analysis** - Compare settings across classes
174
+ - **Inheritance Review** - Analyze class hierarchies
175
+ - **Module Usage Audit** - Track module inclusion patterns
176
+ - **API Comparison** - Compare method signatures
177
+ - **Feature Flag Analysis** - Ensure consistency
178
+ - **Service Configuration** - Microservices governance
129
179
 
130
180
  ## 🎓 Learning Path
131
181
 
132
- 1. **Start with Basic Examples** - Learn the core API and concepts
133
- 2. **Try Advanced Features** - Explore hash expansion and error handling
134
- 3. **Study Real-World Examples** - See practical applications
135
- 4. **Build Your Own** - Apply ClassMetrix to your specific use cases
136
-
137
- ## 💡 Use Case Ideas
182
+ 1. Run `basic_usage.rb` to understand core concepts
183
+ 2. Try `inheritance_and_modules.rb` for advanced features
184
+ 3. Explore `/basic/` examples for step-by-step learning
185
+ 4. Check `/real_world/` for production scenarios
186
+ 5. Build custom analysis for your use cases
138
187
 
139
- - **Configuration Management**: Compare settings across environments
140
- - **API Analysis**: Compare method signatures and return types
141
- - **Feature Flag Audits**: Ensure consistency across services
142
- - **Class Architecture Review**: Compare similar classes for consistency
143
- - **Microservices Governance**: Standardize configurations across services
144
- - **Legacy Code Analysis**: Understand differences between old and new implementations
188
+ ## 📝 Running Examples
145
189
 
146
- ## 🤝 Contributing Examples
190
+ ```bash
191
+ # Basic functionality
192
+ ruby examples/basic_usage.rb
147
193
 
148
- Have a great ClassMetrix use case? Consider contributing an example!
194
+ # Inheritance and modules
195
+ ruby examples/inheritance_and_modules.rb
149
196
 
150
- 1. Create a new file in the appropriate directory
151
- 2. Follow the existing example structure
152
- 3. Include clear comments and output explanations
153
- 4. Add an entry to this README
197
+ # Advanced features
198
+ ruby examples/advanced/hash_expansion.rb
154
199
 
155
- Happy analyzing! 🎉
200
+ # Real-world scenarios
201
+ ruby examples/real_world/microservices_audit.rb
202
+ ```
@@ -0,0 +1,88 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../lib/class_metrix"
5
+
6
+ # Basic Usage Example
7
+ puts "ClassMetrix Basic Usage"
8
+ puts "=" * 30
9
+
10
+ # Define some simple classes for comparison
11
+ class User
12
+ ROLE = "user"
13
+ MAX_LOGIN_ATTEMPTS = 3
14
+
15
+ def self.permissions
16
+ ["read"]
17
+ end
18
+
19
+ def self.session_timeout
20
+ 3600
21
+ end
22
+ end
23
+
24
+ class Admin
25
+ ROLE = "admin"
26
+ MAX_LOGIN_ATTEMPTS = 5
27
+
28
+ def self.permissions
29
+ ["read", "write", "admin"]
30
+ end
31
+
32
+ def self.session_timeout
33
+ 7200
34
+ end
35
+ end
36
+
37
+ classes = [User, Admin]
38
+
39
+ # 1. Extract constants
40
+ puts "\n1. Constants Comparison"
41
+ puts "-" * 25
42
+ result = ClassMetrix.extract(:constants)
43
+ .from(classes)
44
+ .to_markdown
45
+ puts result
46
+
47
+ # 2. Extract class methods
48
+ puts "\n2. Class Methods Comparison"
49
+ puts "-" * 25
50
+ result = ClassMetrix.extract(:class_methods)
51
+ .from(classes)
52
+ .to_markdown
53
+ puts result
54
+
55
+ # 3. Combined extraction
56
+ puts "\n3. Combined Analysis"
57
+ puts "-" * 25
58
+ result = ClassMetrix.extract(:constants, :class_methods)
59
+ .from(classes)
60
+ .to_markdown
61
+ puts result
62
+
63
+ # 4. Filtered analysis
64
+ puts "\n4. Filtered Analysis (ROLE and permissions only)"
65
+ puts "-" * 25
66
+ result = ClassMetrix.extract(:constants, :class_methods)
67
+ .from(classes)
68
+ .filter(/ROLE|permissions/)
69
+ .to_markdown
70
+ puts result
71
+
72
+ # 5. Hash expansion
73
+ puts "\n5. Hash Expansion Example"
74
+ puts "-" * 25
75
+
76
+ class ConfigExample
77
+ SETTINGS = { timeout: 30, retries: 3, ssl: true }.freeze
78
+
79
+ def self.database_config
80
+ { host: "localhost", port: 5432, pool_size: 5 }
81
+ end
82
+ end
83
+
84
+ result = ClassMetrix.extract(:constants, :class_methods)
85
+ .from([ConfigExample])
86
+ .expand_hashes
87
+ .to_markdown
88
+ puts result
@@ -0,0 +1,65 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # Debug Levels Demonstration
5
+ # Shows the different levels of debug output
6
+
7
+ require_relative '../lib/class_metrix'
8
+
9
+ # Example classes
10
+ class ServiceA
11
+ CONFIG = { host: 'localhost', port: 3000, timeout: 30 }
12
+
13
+ def self.get_settings
14
+ { cache: true, retries: 3 }
15
+ end
16
+ end
17
+
18
+ class ServiceB
19
+ CONFIG = { host: 'remote', port: 8080 }
20
+
21
+ def self.get_settings
22
+ { cache: false, retries: 1 }
23
+ end
24
+ end
25
+
26
+ puts "=== ClassMetrix Debug Levels Demo ==="
27
+ puts
28
+
29
+ # Test different debug levels
30
+ levels = [:basic, :detailed, :verbose]
31
+
32
+ levels.each do |level|
33
+ puts "\n" + "="*50
34
+ puts "DEBUG LEVEL: #{level.upcase}"
35
+ puts "="*50
36
+
37
+ begin
38
+ result = ClassMetrix.extract(:constants, :class_methods)
39
+ .from([ServiceA, ServiceB])
40
+ .expand_hashes
41
+ .debug(level) # Set debug level
42
+ .to_markdown
43
+
44
+ puts "\n--- Result Summary ---"
45
+ puts "Report generated successfully! ✅"
46
+ puts "Lines in output: #{result.lines.count}"
47
+
48
+ rescue => e
49
+ puts "Error: #{e.class}: #{e.message}"
50
+ end
51
+
52
+ puts "\n"
53
+ end
54
+
55
+ puts "\n" + "="*50
56
+ puts "DEBUG LEVEL SUMMARY"
57
+ puts "="*50
58
+ puts
59
+ puts ":basic - Key decisions and summaries only"
60
+ puts ":detailed - More context and intermediate steps"
61
+ puts ":verbose - Full details including individual value processing"
62
+ puts
63
+ puts "Use :basic for general troubleshooting"
64
+ puts "Use :detailed for understanding data flow"
65
+ puts "Use :verbose for deep debugging of specific values"
@@ -0,0 +1,75 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # Debug Mode Demonstration
5
+ # Shows how ClassMetrix handles problematic objects safely
6
+
7
+ require_relative '../lib/class_metrix'
8
+
9
+ # Example classes with different types of values
10
+ class SafeService
11
+ CONFIG = { host: 'localhost', port: 3000 }
12
+
13
+ def self.timeout_config
14
+ { connect: 30, read: 60 }
15
+ end
16
+ end
17
+
18
+ class ProblematicService
19
+ # This constant is a Class object (not a Hash)
20
+ CONFIG_CLASS = SafeService
21
+
22
+ def self.service_config
23
+ SafeService::CONFIG
24
+ end
25
+ end
26
+
27
+ # Mock object that has problematic method behavior
28
+ class ProblematicObject
29
+ def inspect
30
+ raise "Inspect not allowed!"
31
+ end
32
+
33
+ def to_s
34
+ raise "To_s not allowed!"
35
+ end
36
+
37
+ def class
38
+ raise "Class not allowed!"
39
+ end
40
+ end
41
+
42
+ class WeirdService
43
+ BROKEN_OBJECT = ProblematicObject.new
44
+
45
+ def self.get_config
46
+ { normal: 'value', broken: ProblematicObject.new }
47
+ end
48
+ end
49
+
50
+ puts "=== ClassMetrix Debug Mode Demo ==="
51
+ puts
52
+
53
+ puts "This demo shows how ClassMetrix safely handles:"
54
+ puts "1. Class objects in constants"
55
+ puts "2. Objects with broken inspect/to_s methods"
56
+ puts "3. Hash expansion with mixed value types"
57
+ puts
58
+
59
+ begin
60
+ result = ClassMetrix.extract(:constants, :class_methods)
61
+ .from([SafeService, ProblematicService, WeirdService])
62
+ .expand_hashes
63
+ .handle_errors
64
+ .debug # Enable debug mode
65
+ .to_markdown
66
+
67
+ puts "\n=== Generated Report ==="
68
+ puts result[0..500] + "..." if result.length > 500
69
+ puts "\nReport generated successfully! ✅"
70
+ puts "Note: Debug output above shows how problematic objects were handled safely."
71
+
72
+ rescue => e
73
+ puts "Error: #{e.class}: #{e.message}"
74
+ puts "This should not happen with the safety improvements!"
75
+ end