|
| 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 |
0 commit comments