Module: Fontisan::Converters::ConversionStrategy
- Included in:
- OutlineConverter, SvgGenerator, TableCopier, Type1Converter, Woff2Encoder, WoffWriter
- Defined in:
- lib/fontisan/converters/conversion_strategy.rb
Overview
Interface module and declarative options DSL for conversion strategies
ConversionStrategy
defines the contract that all conversion strategy classes must implement,
plus a declarative DSL for declaring the options each strategy accepts.
Why a declarative options DSL
Each format has its own spec-mandated knobs (WOFF: zlib level, WOFF2: Brotli quality, etc.). Letting each strategy declare its own options keeps the schema with the code that consumes it (encapsulation), and makes adding a new format a pure additive change (OCP): write a new strategy class, declare its options, done — no edits to central option lists, the CLI option parser, or ConversionOptions.
The strategy is also the sole validator of its own options. The
runtime check at FormatConverter#convert calls
strategy.class.validate_options! to enforce the format ↔ option
mapping (e.g., rejecting --zlib-level on a WOFF2 conversion). This
is the MECE guarantee: every option belongs to exactly one strategy,
and a strategy rejects anything it did not declare.
Defined Under Namespace
Modules: ClassMethods Classes: Option
Class Method Summary collapse
-
.included(base) ⇒ Object
Mix ClassMethods into any class that includes this module.
Instance Method Summary collapse
-
#convert(font, options = {}) ⇒ Hash<String, String>
Convert font to target format.
-
#supported_conversions ⇒ Array<Array<Symbol>>
Get list of supported conversions.
-
#supports?(source_format, target_format) ⇒ Boolean
Check if strategy supports a given conversion.
-
#validate(font, target_format) ⇒ Boolean
Validate that conversion is possible.
Class Method Details
.included(base) ⇒ Object
Mix ClassMethods into any class that includes this module.
162 163 164 |
# File 'lib/fontisan/converters/conversion_strategy.rb', line 162 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#convert(font, options = {}) ⇒ Hash<String, String>
Convert font to target format.
Strategies must implement this. Subclasses should call
self.class.validate_options!(options) first to enforce their schema.
175 176 177 178 |
# File 'lib/fontisan/converters/conversion_strategy.rb', line 175 def convert(font, = {}) raise NotImplementedError, "#{self.class.name} must implement convert(font, options)" end |
#supported_conversions ⇒ Array<Array<Symbol>>
Get list of supported conversions.
184 185 186 187 |
# File 'lib/fontisan/converters/conversion_strategy.rb', line 184 def supported_conversions raise NotImplementedError, "#{self.class.name} must implement supported_conversions" end |
#supports?(source_format, target_format) ⇒ Boolean
Check if strategy supports a given conversion.
206 207 208 |
# File 'lib/fontisan/converters/conversion_strategy.rb', line 206 def supports?(source_format, target_format) supported_conversions.include?([source_format, target_format]) end |
#validate(font, target_format) ⇒ Boolean
Validate that conversion is possible.
196 197 198 199 |
# File 'lib/fontisan/converters/conversion_strategy.rb', line 196 def validate(font, target_format) raise NotImplementedError, "#{self.class.name} must implement validate(font, target_format)" end |