ClassMetrix
ClassMetrix is a Ruby gem that extracts and compares class behaviors (constants, class methods, and more) across multiple classes, generating clean markdown reports for analysis, documentation, and compliance auditing.
Why "Metrix"? Short for "metrics" - measuring and analyzing class behaviors.
โจ Features
- ๐ Multi-Type Extraction: Constants, class methods, and more
- ๐๏ธ Comprehensive by Default: Includes inheritance and modules automatically
-
๐ฏ Flexible Scanning: Use
strict()
for class-only orwith_private()
for private members - ๐ Hash Expansion: Expand hash values into readable sub-rows
- ๐ Rich Reports: Professional markdown and CSV output with full configuration
- ๐ Type Safe: Full RBS type annotations with Steep type checking
๐ Quick Start
Installation
gem 'class-metrix'
Basic Usage
# Extract constants from multiple classes (comprehensive by default)
ClassMetrix.extract(:constants)
.from([User, Admin, Guest])
.to_markdown
# Class-only scanning (exclude inheritance & modules)
ClassMetrix.extract(:constants)
.from([User, Admin, Guest])
.strict
.to_markdown
# Advanced example with multiple features
ClassMetrix.extract(:constants, :class_methods)
.from([DatabaseConfig, RedisConfig, S3Config])
.with_private
.filter(/config$/)
.expand_hashes
.handle_errors
.to_markdown("audit_report.md",
title: "Service Configuration Audit",
show_missing_summary: true
)
๐ API Reference
Core Methods
-
.extract(types)
- Extract constants, class_methods, or both -
.from(classes)
- Specify which classes to analyze -
.strict
- Class-only scanning (exclude inheritance & modules) -
.with_private
- Include private methods and constants -
.filter(patterns)
- Filter behaviors by regex patterns -
.expand_hashes
- Expand hash values into sub-rows -
.handle_errors
- Graceful error handling -
.debug(level)
- Enable debug logging (:basic
,:detailed
,:verbose
)
Output Formats
# Markdown output
.to_markdown("report.md", title: "Custom Title", footer_style: :detailed)
# CSV output
.to_csv("data.csv", flatten_hashes: true, separator: ";")
๐ฏ Common Use Cases
Configuration Audit
# Compare configuration across microservices
services = [DatabaseService, RedisService, S3Service]
ClassMetrix.extract(:constants, :class_methods)
.from(services)
.filter(/CONFIG/, /timeout/)
.expand_hashes
.to_markdown("microservices_audit.md")
API Compatibility Check
# Check API compatibility across versions
apis = [V1::UsersAPI, V2::UsersAPI, V3::UsersAPI]
ClassMetrix.extract(:class_methods)
.from(apis)
.filter(/^endpoint_/, /^validate_/)
.handle_errors
.to_markdown("api_compatibility.md", show_missing_summary: true)
Policy Comparison
# Compare authorization policies
ClassMetrix.extract(:constants)
.from([UserPolicy, AdminPolicy, ModeratorPolicy])
.filter(/^PERMISSION_/, /^ROLE_/)
.to_markdown("policy_comparison.md")
๐ Documentation
- Examples - Comprehensive usage examples
- Configuration Guide - Detailed configuration options
- Architecture - Technical architecture overview
- Changelog - Version history and changes
๐งช Development
# Clone and setup
git clone https://github.com/patrick204nqh/class-metrix.git
cd class-metrix
bundle install
# Run tests and type checking
bundle exec rspec
bundle exec steep check
bundle exec rubocop
๐ Requirements
- Ruby 3.1+ (Required for full RBS and Steep support)
- No runtime dependencies (Pure Ruby implementation)
๐ค Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -am 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
๐ License
This gem is available as open source under the terms of the MIT License.