Module: SimpleCovMcp::Formatters
- Defined in:
- lib/simplecov_mcp/formatters.rb,
lib/simplecov_mcp/formatters/source_formatter.rb
Defined Under Namespace
Classes: SourceFormatter
Constant Summary collapse
- FORMATTERS =
Maps format symbols to their formatter lambdas Following the rexe pattern for simple, extensible formatting
{ table: ->(obj) { obj }, # Pass through - table formatting handled elsewhere json: lambda(&:to_json), pretty_json: ->(obj) { JSON.pretty_generate(obj) }, yaml: ->(obj) { require 'yaml' obj.to_yaml }, awesome_print: ->(obj) { require 'awesome_print' obj.ai } }.freeze
- FORMAT_REQUIRES =
Maps format symbols to their required libraries Only loaded when the format is actually used
{ yaml: 'yaml', awesome_print: 'awesome_print' }.freeze
Class Method Summary collapse
-
.ensure_requirements_for(format) ⇒ Object
Ensures required libraries are loaded for the given format.
-
.format(obj, format) ⇒ Object
Formats an object using the specified format.
-
.formatter_for(format) ⇒ Object
Returns the formatter lambda for the given format.
Class Method Details
.ensure_requirements_for(format) ⇒ Object
Ensures required libraries are loaded for the given format
36 37 38 39 |
# File 'lib/simplecov_mcp/formatters.rb', line 36 def self.ensure_requirements_for(format) requirement = FORMAT_REQUIRES[format] require requirement if requirement end |
.format(obj, format) ⇒ Object
Formats an object using the specified format
42 43 44 45 46 47 48 49 |
# File 'lib/simplecov_mcp/formatters.rb', line 42 def self.format(obj, format) ensure_requirements_for(format) formatter_for(format).call(obj) rescue LoadError => e gem_name = e.[/-- (\S+)/, 1] || 'required gem' raise LoadError, "The #{format} format requires the '#{gem_name}' gem. " \ "Install it with: gem install #{gem_name}" end |
.formatter_for(format) ⇒ Object
Returns the formatter lambda for the given format
31 32 33 |
# File 'lib/simplecov_mcp/formatters.rb', line 31 def self.formatter_for(format) FORMATTERS[format] or raise ArgumentError, "Unknown format: #{format}" end |