Module: Vectory::Conversion

Defined in:
lib/vectory/conversion.rb,
lib/vectory/conversion/strategy.rb,
lib/vectory/conversion/inkscape_strategy.rb,
lib/vectory/conversion/ghostscript_strategy.rb

Overview

Conversion module provides strategy-based conversion interface

This module encapsulates different conversion strategies for converting between vector formats using external tools like Inkscape and Ghostscript.

Examples:

Convert SVG to EPS using Inkscape

Vectory::Conversion.convert(svg_content, from: :svg, to: :eps)

Get available strategies for a conversion

Vectory::Conversion.strategies_for(:svg, :eps)

Defined Under Namespace

Classes: GhostscriptStrategy, InkscapeStrategy, Strategy

Class Method Summary collapse

Class Method Details

.convert(content, from:, to:, **options) ⇒ Vectory::Vector, String

Convert content from one format to another

Automatically selects the appropriate strategy based on the input/output formats.

Parameters:

  • content (String)

    the content to convert

  • from (Symbol)

    the input format

  • to (Symbol)

    the output format

  • options (Hash)

    additional options passed to the strategy

Returns:

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vectory/conversion.rb', line 30

def convert(content, from:, to:, **options)
  strategy = find_strategy(from, to)

  unless strategy
    supported = supported_conversions.map do |a, b|
      "#{a}#{b}"
    end.join(", ")
    raise Vectory::ConversionError,
          "No strategy found for #{from}#{to} conversion. " \
          "Supported: #{supported}"
  end

  strategy.convert(content, input_format: from, output_format: to,
                            **options)
end

.strategiesArray<Vectory::Conversion::Strategy>

Get all available strategies

Returns:



49
50
51
52
53
54
# File 'lib/vectory/conversion.rb', line 49

def strategies
  @strategies ||= [
    InkscapeStrategy.new,
    GhostscriptStrategy.new,
  ]
end

.strategies_for(input_format, output_format) ⇒ Array<Vectory::Conversion::Strategy>

Get strategies that support a specific conversion

Parameters:

  • input_format (Symbol)

    the input format

  • output_format (Symbol)

    the output format

Returns:



61
62
63
# File 'lib/vectory/conversion.rb', line 61

def strategies_for(input_format, output_format)
  strategies.select { |s| s.supports?(input_format, output_format) }
end

.supported_conversionsArray<Array<Symbol>>

Get all supported conversions

Returns:

  • (Array<Array<Symbol>>)

    array of [input, output] format pairs



77
78
79
# File 'lib/vectory/conversion.rb', line 77

def supported_conversions
  @supported_conversions ||= strategies.flat_map(&:supported_conversions).uniq
end

.supports?(input_format, output_format) ⇒ Boolean

Check if a conversion is supported

Parameters:

  • input_format (Symbol)

    the input format

  • output_format (Symbol)

    the output format

Returns:

  • (Boolean)

    true if any strategy supports this conversion



70
71
72
# File 'lib/vectory/conversion.rb', line 70

def supports?(input_format, output_format)
  strategies_for(input_format, output_format).any?
end

.tool_available?(tool) ⇒ Boolean

Check if a specific tool is available

Parameters:

  • tool (Symbol, String)

    the tool name (:inkscape, :ghostscript, etc.)

Returns:

  • (Boolean)

    true if the tool is available



85
86
87
88
# File 'lib/vectory/conversion.rb', line 85

def tool_available?(tool)
  strategy = strategies.find { |s| s.tool_name == tool.to_s.downcase }
  strategy&.available? || false
end