Module: Fontisan::Converters::ConversionStrategy::ClassMethods

Defined in:
lib/fontisan/converters/conversion_strategy.rb

Overview

Class methods mixed into including classes via included.

Instance Method Summary collapse

Instance Method Details

#declared_optionsArray<Option> Also known as: supported_options

All options declared by this class.

Returns:



70
71
72
# File 'lib/fontisan/converters/conversion_strategy.rb', line 70

def declared_options
  @declared_options ||= []
end

#default_optionsHash{Symbol => Object}

Default values for every declared option.

Returns:

  • (Hash{Symbol => Object})


88
89
90
# File 'lib/fontisan/converters/conversion_strategy.rb', line 88

def default_options
  supported_options.to_h { |o| [o.name, o.default] }
end

#option(name, type:, default:, cli:, desc:, range: nil, values: nil) ⇒ void

This method returns an undefined value.

Declare an option this strategy accepts.

Parameters:

  • name (Symbol)

    Option name (the hash key used in convert)

  • type (Symbol)

    One of :integer, :boolean, :string

  • default (Object)

    Default value when the caller omits the option

  • cli (String)

    CLI flag shape (for help text generation)

  • desc (String)

    Human-readable description

  • range (Range, nil) (defaults to: nil)

    For :integer; valid range

  • values (Array, nil) (defaults to: nil)

    For :string; allowed values



60
61
62
63
64
65
# File 'lib/fontisan/converters/conversion_strategy.rb', line 60

def option(name, type:, default:, cli:, desc:, range: nil, values: nil)
  declared_options << Option.new(
    name: name, type: type, default: default, cli: cli, desc: desc,
    range: range, allowed_values: values
  )
end

#option_for(name) ⇒ Option?

Find the option schema for a given key (symbol or string).

Parameters:

  • name (Symbol, String)

Returns:



81
82
83
# File 'lib/fontisan/converters/conversion_strategy.rb', line 81

def option_for(name)
  supported_options.find { |o| o.name == name.to_sym }
end

#validate_options!(user_options) ⇒ void

This method returns an undefined value.

Validate a user-provided options hash against this strategy's schema.

Raises ArgumentError for any unknown key, wrong type, or out-of-range value. Called by FormatConverter after strategy selection, so cross-format misuse (e.g., --zlib-level on a WOFF2 conversion) is caught here.

Parameters:

  • user_options (Hash{Symbol, String => Object})

Raises:

  • (ArgumentError)

    if any key is unknown or any value is invalid



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/fontisan/converters/conversion_strategy.rb', line 102

def validate_options!(user_options)
  user_options.each_key do |key|
    opt = option_for(key)
    next if opt

    names = supported_options.map(&:name)
    list = names.empty? ? "(none)" : names.join(", ")
    raise ArgumentError,
          "Unknown option #{key.inspect} for #{name}. " \
          "Supported: #{list}"
  end

  user_options.each do |key, value|
    opt = option_for(key)
    validate_option_value!(opt, value) unless value.nil?
  end
end