Skip to content

Commit 74f28ab

Browse files
committed
Set :json format as default
1 parent 6789fa7 commit 74f28ab

File tree

5 files changed

+234
-4
lines changed

5 files changed

+234
-4
lines changed

.reek.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,12 @@ detectors:
3333
- Skunk::Command::Compare#analyse_modified_files
3434
- Skunk::Command::Compare#build_details_path
3535
- Skunk::Command::Shareable#sharing?
36+
- Skunk::Configuration#supported_format?
37+
- Skunk::Configuration#supported_formats
38+
ManualDispatch:
39+
exclude:
40+
- Skunk::Config#self.method_missing
41+
- Skunk::Config#self.respond_to_missing?
42+
BooleanParameter:
43+
exclude:
44+
- Skunk::Config#self.respond_to_missing?

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Skunk is a RubyCritic extension to calculate a SkunkScore for a file or project.
1414
* [Help commands](#help-commands)
1515
* [Generate the SkunkCore for your project](#generate-the-skunkcore-for-your-project)
1616
* [Comparing Feature Branches](#comparing-feature-branches)
17+
* [Configuration](#configuration)
18+
* [Setting Output Formats](#setting-output-formats)
1719
* [Sharing your SkunkScore](#sharing-your-skunkscore)
1820
* [Contributing](#contributing)
1921
* [Sponsorship](#sponsorship)
@@ -152,6 +154,36 @@ Score: 340.3
152154

153155
This should give you an idea if you're moving in the direction of maintaining the code quality or not. In this case, the feature branch is decreasing the code quality because it has a higher SkunkScore than the main branch.
154156

157+
## Configuration
158+
159+
### Setting Output Formats
160+
161+
Skunk provides a simple configuration class to control output formats programmatically. You can use `Skunk::Config` to set which formats should be generated when running Skunk.
162+
163+
**Supported formats:**
164+
- `:json` - JSON report (default)
165+
- `:html` - HTML report with visual charts and tables
166+
167+
```ruby
168+
require 'skunk/config'
169+
170+
# Set multiple formats
171+
Skunk::Config.formats = [:json, :html]
172+
173+
# Add a format to the existing list
174+
Skunk::Config.add_format(:html)
175+
176+
# Remove a format
177+
Skunk::Config.remove_format(:json)
178+
179+
# Check supported formats
180+
Skunk::Config.supported_formats # => [:json, :html]
181+
Skunk::Config.supported_format?(:json) # => true
182+
183+
# Reset to defaults
184+
Skunk::Config.reset
185+
```
186+
155187
## Sharing your SkunkScore
156188

157189
If you want to share the results of your Skunk report with the Ruby community, run:

lib/skunk/config.rb

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# frozen_string_literal: true
2+
3+
module Skunk
4+
# Utility module for format validation
5+
module FormatValidator
6+
# Supported output formats
7+
SUPPORTED_FORMATS = %i[json html].freeze
8+
9+
# Check if a format is supported
10+
# @param format [Symbol] Format to check
11+
# @return [Boolean] True if format is supported
12+
def self.supported_format?(format)
13+
SUPPORTED_FORMATS.include?(format)
14+
end
15+
16+
# Get all supported formats
17+
# @return [Array<Symbol>] All supported formats
18+
def self.supported_formats
19+
SUPPORTED_FORMATS.dup
20+
end
21+
end
22+
23+
# Configuration class for Skunk that supports formats
24+
# Similar to RubyCritic::Configuration but focused only on Skunk's needs
25+
class Configuration
26+
# Default format
27+
DEFAULT_FORMAT = :json
28+
29+
def initialize
30+
@formats = [DEFAULT_FORMAT]
31+
end
32+
33+
def set(options = {})
34+
self.formats = options[:formats] if options.key?(:formats)
35+
end
36+
37+
# Get the configured formats
38+
# @return [Array<Symbol>] Array of format symbols
39+
def formats
40+
@formats
41+
end
42+
43+
# Set the formats with validation
44+
# @param format_list [Array<Symbol>, Symbol] Format(s) to set
45+
def formats=(format_list)
46+
format_array = Array(format_list)
47+
@formats = format_array.select { |format| FormatValidator.supported_format?(format) }
48+
@formats = [DEFAULT_FORMAT] if @formats.empty?
49+
end
50+
51+
# Add a format to the existing list
52+
# @param format [Symbol] Format to add
53+
def add_format(format)
54+
return unless FormatValidator.supported_format?(format)
55+
56+
@formats << format unless @formats.include?(format)
57+
end
58+
59+
# Remove a format from the list
60+
# @param format [Symbol] Format to remove
61+
def remove_format(format)
62+
@formats.delete(format)
63+
@formats = [DEFAULT_FORMAT] if @formats.empty?
64+
end
65+
66+
# Check if a format is supported
67+
# @param format [Symbol] Format to check
68+
# @return [Boolean] True if format is supported
69+
def supported_format?(format)
70+
FormatValidator.supported_format?(format)
71+
end
72+
73+
# Get all supported formats
74+
# @return [Array<Symbol>] All supported formats
75+
def supported_formats
76+
FormatValidator.supported_formats
77+
end
78+
79+
# Reset to default configuration
80+
def reset
81+
@formats = [DEFAULT_FORMAT]
82+
end
83+
end
84+
85+
# Config module that delegates to Configuration instance
86+
# Similar to RubyCritic::Config pattern
87+
module Config
88+
def self.configuration
89+
@configuration ||= Configuration.new
90+
end
91+
92+
def self.set(options = {})
93+
configuration.set(options)
94+
end
95+
96+
def self.method_missing(method, *args, &block)
97+
if configuration.respond_to?(method)
98+
configuration.public_send(method, *args, &block)
99+
else
100+
super
101+
end
102+
end
103+
104+
def self.respond_to_missing?(symbol, include_private = false)
105+
configuration.respond_to?(symbol, include_private) || super
106+
end
107+
end
108+
end

lib/skunk/reporter.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
# frozen_string_literal: true
22

3+
require "skunk/config"
4+
35
module Skunk
46
# Pick the right report generator based on the format specified in the
57
# configuration. If the format is not supported, it will default to ConsoleReport.
68
module Reporter
7-
REPORT_GENERATOR_CLASS_FORMATS = %i[json html].freeze
8-
99
def self.generate_report(analysed_modules)
10-
RubyCritic::Config.formats.uniq.each do |format|
10+
Config.formats.uniq.each do |format|
1111
report_generator_class(format).new(analysed_modules).generate_report
1212
end
1313
end
1414

1515
def self.report_generator_class(config_format)
16-
return if REPORT_GENERATOR_CLASS_FORMATS.none? { |format| format == config_format }
16+
return unless Config.supported_format?(config_format)
1717

1818
require "skunk/generators/#{config_format}_report"
1919
Generator.const_get("#{config_format.capitalize}Report")

test/lib/skunk/config_test.rb

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# frozen_string_literal: true
2+
3+
require "minitest/autorun"
4+
require_relative "../../../lib/skunk/config"
5+
6+
module Skunk
7+
class ConfigTest < Minitest::Test
8+
def setup
9+
Config.reset
10+
end
11+
12+
def test_default_format
13+
assert_equal [:json], Config.formats
14+
end
15+
16+
def test_set_formats_with_array
17+
Config.formats = [:html, :json]
18+
assert_equal [:html, :json], Config.formats
19+
end
20+
21+
def test_set_formats_with_single_format
22+
Config.formats = :html
23+
assert_equal [:html], Config.formats
24+
end
25+
26+
def test_set_formats_filters_unsupported_formats
27+
Config.formats = [:html, :json, :unsupported, :xml]
28+
assert_equal [:html, :json], Config.formats
29+
end
30+
31+
def test_set_formats_with_empty_array_defaults_to_json
32+
Config.formats = []
33+
assert_equal [:json], Config.formats
34+
end
35+
36+
def test_add_format
37+
Config.add_format(:html)
38+
assert_equal [:json, :html], Config.formats
39+
end
40+
41+
def test_add_format_ignores_duplicates
42+
Config.add_format(:html)
43+
Config.add_format(:html)
44+
assert_equal [:json, :html], Config.formats
45+
end
46+
47+
def test_add_format_ignores_unsupported_formats
48+
Config.add_format(:unsupported)
49+
assert_equal [:json], Config.formats
50+
end
51+
52+
def test_remove_format
53+
Config.formats = [:json, :html]
54+
Config.remove_format(:html)
55+
assert_equal [:json], Config.formats
56+
end
57+
58+
def test_remove_format_defaults_to_json_when_empty
59+
Config.remove_format(:json)
60+
assert_equal [:json], Config.formats
61+
end
62+
63+
def test_supported_format
64+
assert Config.supported_format?(:json)
65+
assert Config.supported_format?(:html)
66+
refute Config.supported_format?(:xml)
67+
refute Config.supported_format?(:unsupported)
68+
end
69+
70+
def test_supported_formats
71+
expected = [:json, :html]
72+
assert_equal expected, Config.supported_formats
73+
end
74+
75+
def test_reset
76+
Config.formats = [:html]
77+
Config.reset
78+
assert_equal [:json], Config.formats
79+
end
80+
end
81+
end

0 commit comments

Comments
 (0)