Class: Vectory::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/vectory/configuration.rb

Overview

Configuration for Vectory

Provides centralized configuration for tool paths, timeouts, caching, etc. Can be loaded from environment variables or a configuration file.

Examples:

Set custom Inkscape path

Vectory::Configuration.instance.inkscape_path = "/path/to/inkscape"

Load from environment variables

# Set VECTORY_INKSCAPE_PATH environment variable
Vectory::Configuration.load_from_environment

Constant Summary collapse

DEFAULT_TIMEOUT =

Default timeout for external tool execution (seconds)

120
DEFAULT_CACHE_TTL =

Default cache TTL for memoized values (seconds)

300
DEFAULT_TEMP_DIR =

Default temporary directory

nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Initialize configuration with default values



43
44
45
46
47
48
49
50
51
# File 'lib/vectory/configuration.rb', line 43

def initialize
  @timeout = DEFAULT_TIMEOUT
  @cache_ttl = DEFAULT_CACHE_TTL
  @cache_enabled = true
  @temp_dir = DEFAULT_TEMP_DIR
  @verbose_logging = false
  @inkscape_path = nil
  @ghostscript_path = nil
end

Instance Attribute Details

#cache_enabledObject

Use system default



25
26
27
# File 'lib/vectory/configuration.rb', line 25

def cache_enabled
  @cache_enabled
end

#cache_ttlObject

Use system default



25
26
27
# File 'lib/vectory/configuration.rb', line 25

def cache_ttl
  @cache_ttl
end

#ghostscript_pathObject

Use system default



25
26
27
# File 'lib/vectory/configuration.rb', line 25

def ghostscript_path
  @ghostscript_path
end

#inkscape_pathObject

Use system default



25
26
27
# File 'lib/vectory/configuration.rb', line 25

def inkscape_path
  @inkscape_path
end

#temp_dirObject

Use system default



25
26
27
# File 'lib/vectory/configuration.rb', line 25

def temp_dir
  @temp_dir
end

#timeoutObject

Use system default



25
26
27
# File 'lib/vectory/configuration.rb', line 25

def timeout
  @timeout
end

#verbose_loggingObject

Use system default



25
26
27
# File 'lib/vectory/configuration.rb', line 25

def verbose_logging
  @verbose_logging
end

Class Method Details

.instanceVectory::Configuration

Get the singleton instance

Returns:



31
32
33
# File 'lib/vectory/configuration.rb', line 31

def self.instance
  @instance ||= new
end

.load_from_environmentself

Load configuration from environment variables

Supported environment variables:

  • VECTORY_INKSCAPE_PATH: Path to Inkscape executable

  • VECTORY_GHOSTSCRIPT_PATH: Path to Ghostscript executable

  • VECTORY_TIMEOUT: Timeout for external tools (default: 120)

  • VECTORY_CACHE_TTL: Cache TTL in seconds (default: 300)

  • VECTORY_CACHE_ENABLED: Enable/disable caching (default: true)

  • VECTORY_TEMP_DIR: Temporary directory path

  • VECTORY_VERBOSE: Enable verbose logging (default: false)

Returns:

  • (self)

    the configuration instance



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/vectory/configuration.rb', line 65

def self.load_from_environment
  config = instance

  config.inkscape_path = ENV["VECTORY_INKSCAPE_PATH"] if ENV["VECTORY_INKSCAPE_PATH"]
  config.ghostscript_path = ENV["VECTORY_GHOSTSCRIPT_PATH"] if ENV["VECTORY_GHOSTSCRIPT_PATH"]
  config.timeout = ENV["VECTORY_TIMEOUT"]&.to_i || config.timeout
  config.cache_ttl = ENV["VECTORY_CACHE_TTL"]&.to_i || config.cache_ttl
  config.cache_enabled = ENV["VECTORY_CACHE_ENABLED"] != "false"
  config.temp_dir = ENV["VECTORY_TEMP_DIR"] if ENV["VECTORY_TEMP_DIR"]
  config.verbose_logging = ENV["VECTORY_VERBOSE"] == "true"

  config
end

.load_from_file(path) ⇒ self

Load configuration from a YAML file

Parameters:

  • path (String)

    path to the YAML configuration file

Returns:

  • (self)

    the configuration instance

Raises:

  • (Errno::ENOENT)

    if the file doesn’t exist



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/vectory/configuration.rb', line 84

def self.load_from_file(path)
  require "yaml"
  config_data = YAML.load_file(path)

  config = instance
  config.inkscape_path = config_data["inkscape_path"] if config_data["inkscape_path"]
  config.ghostscript_path = config_data["ghostscript_path"] if config_data["ghostscript_path"]
  config.timeout = config_data["timeout"] || config.timeout
  config.cache_ttl = config_data["cache_ttl"] || config.cache_ttl
  config.cache_enabled = config_data.fetch("cache_enabled",
                                           config.cache_enabled)
  config.temp_dir = config_data["temp_dir"] || config.temp_dir
  config.verbose_logging = config_data.fetch("verbose_logging",
                                             config.verbose_logging)

  config
end

.reset!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reset the configuration to defaults



38
39
40
# File 'lib/vectory/configuration.rb', line 38

def self.reset!
  @instance = new
end

Instance Method Details

#caching_enabled?Boolean

Check if caching is enabled

Returns:

  • (Boolean)

    true if caching is enabled



134
135
136
# File 'lib/vectory/configuration.rb', line 134

def caching_enabled?
  @cache_enabled
end

#effective_ghostscript_pathString?

Get the Ghostscript path (custom or auto-detected)

Returns:

  • (String, nil)

    the configured Ghostscript path or nil if not set



127
128
129
# File 'lib/vectory/configuration.rb', line 127

def effective_ghostscript_path
  @ghostscript_path
end

#effective_inkscape_pathString?

Get the Inkscape path (custom or auto-detected)

Returns:

  • (String, nil)

    the configured Inkscape path or nil if not set



120
121
122
# File 'lib/vectory/configuration.rb', line 120

def effective_inkscape_path
  @inkscape_path
end

#temporary_directoryString

Get the temporary directory

Returns:

  • (String)

    the temporary directory path



141
142
143
# File 'lib/vectory/configuration.rb', line 141

def temporary_directory
  @temp_dir || Dir.tmpdir
end

#to_hHash

Export configuration as a hash

Returns:

  • (Hash)

    the configuration as a hash



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/vectory/configuration.rb', line 105

def to_h
  {
    inkscape_path: @inkscape_path,
    ghostscript_path: @ghostscript_path,
    timeout: @timeout,
    cache_ttl: @cache_ttl,
    cache_enabled: @cache_enabled,
    temp_dir: @temp_dir,
    verbose_logging: @verbose_logging,
  }
end

#validate!Boolean

Validate the configuration

Returns:

  • (Boolean)

    true if configuration is valid

Raises:

  • (ArgumentError)

    if configuration is invalid



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/vectory/configuration.rb', line 156

def validate!
  errors = []

  if @timeout && @timeout <= 0
    errors << "timeout must be positive, got: #{@timeout}"
  end

  if @cache_ttl&.negative?
    errors << "cache_ttl must be non-negative, got: #{@cache_ttl}"
  end

  if @temp_dir && !File.directory?(@temp_dir)
    errors << "temp_dir does not exist: #{@temp_dir}"
  end

  return true if errors.empty?

  raise ArgumentError,
        "Invalid configuration:\n  - #{errors.join("\n  - ")}"
end

#verbose_logging?Boolean

Check if verbose logging is enabled

Returns:

  • (Boolean)

    true if verbose logging is enabled



148
149
150
# File 'lib/vectory/configuration.rb', line 148

def verbose_logging?
  @verbose_logging
end