class HexaPDF::Configuration

Manages both the global and document specific configuration options for HexaPDF.

Overview

HexaPDF allows detailed control over many aspects of PDF manipulation. If there is a need to use a certain default value somewhere, it is defined as configuration options so that it can easily be changed.

Some options are defined as global options because they are needed on the class level - see HexaPDF::GlobalConfiguration. Other options can be configured for individual documents as they allow to fine-tune some behavior - see HexaPDF::DefaultDocumentConfiguration.

A configuration option name is dot-separted to provide a hierarchy of option names. For example, io.chunk_size.

Attributes

options[R]

Returns the hash with the configuration options.

Public Class Methods

new(options = {}) click to toggle source

Creates a new Configuration object using the provided hash argument.

# File lib/hexapdf/configuration.rb, line 65
def initialize(options = {})
  @options = options
end
with_defaults(values = {}) click to toggle source

Creates a new document specific Configuration object by merging the values into the default configuration object.

# File lib/hexapdf/configuration.rb, line 60
def self.with_defaults(values = {})
  DefaultDocumentConfiguration.merge(values)
end

Public Instance Methods

[](name) click to toggle source

Returns the value for the configuration option name.

# File lib/hexapdf/configuration.rb, line 76
def [](name)
  options[name]
end
[]=(name, value) click to toggle source

Uses value as the value for the configuration option name.

# File lib/hexapdf/configuration.rb, line 81
def []=(name, value)
  options[name] = value
end
constantize(name, *keys) → constant click to toggle source
constantize(name, *keys) {|name| block} → obj

Returns the constant the option name is referring to. If keys are provided and the value of the option name responds to #dig, the constant to which the keys refer is returned.

If no constant can be found and no block is provided, an error is raised. If a block is provided it is called with the option name and its result will be returned.

config.constantize('encryption.aes')      #=> HexaPDF::Encryption::FastAES
config.constantize('filter.map', :Fl)     #=> HexaPDF::Filter::FlateDecode
# File lib/hexapdf/configuration.rb, line 120
def constantize(name, *keys)
  data = self[name]
  data = data.dig(*keys) if data.respond_to?(:dig)
  (data = ::Object.const_get(data) rescue nil) if data.kind_of?(String)
  if data.nil? && block_given?
    data = yield(name)
  elsif data.nil?
    raise HexaPDF::Error, "Error getting constant for configuration option '#{name}'" +
      (keys.empty? ? "" : " and keys '#{keys.join(', ')}'")
  end
  data
end
key?(name) click to toggle source

Returns true if the given option exists.

# File lib/hexapdf/configuration.rb, line 70
def key?(name)
  options.key?(name)
end
Also aliased as: option?
merge(config) click to toggle source

Returns a new Configuration object containing the options from the given configuration object (or hash) and this configuration object.

If a key already has a value in this object, its value is overwritten by the one from config. However, hash values are merged instead of being overwritten. Array values are duplicated.

# File lib/hexapdf/configuration.rb, line 91
def merge(config)
  config = (config.kind_of?(self.class) ? config.options : config)
  merged_config = options.each_with_object({}) do |(key, old), conf|
    new = config[key]
    conf[key] = if old.kind_of?(Hash) && new.kind_of?(Hash)
                  old.merge(new)
                elsif new.kind_of?(Array) || old.kind_of?(Array)
                  (new || old).dup
                elsif config.key?(key)
                  new
                else
                  old
                end
  end
  self.class.new(merged_config)
end
option?(name)
Alias for: key?