Class: HexaPDF::Configuration

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

Overview

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.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Configuration

Creates a new Configuration object using the provided hash argument.



63
64
65
# File 'lib/hexapdf/configuration.rb', line 63

def initialize(options = {})
  @options = options
end

Class Method Details

.with_defaults(values = {}) ⇒ Object

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



57
58
59
# File 'lib/hexapdf/configuration.rb', line 57

def self.with_defaults(values = {})
  DefaultDocumentConfiguration.merge(values)
end

Instance Method Details

#[](name) ⇒ Object

Returns the value for the configuration option name.



74
75
76
# File 'lib/hexapdf/configuration.rb', line 74

def [](name)
  options[name]
end

#[]=(name, value) ⇒ Object

Uses value as the value for the configuration option name.



79
80
81
# File 'lib/hexapdf/configuration.rb', line 79

def []=(name, value)
  options[name] = value
end

#constantize(name, key = :__unset) ⇒ Object

:call-seq:

config.constantize(name, key = nil)                  -> constant
config.constantize(name, key = nil) {|name| block}   -> obj

Returns the constant the option name is referring to. If key is provided and the value of the option name responds to #[], the constant to which key refers 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


107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/hexapdf/configuration.rb', line 107

def constantize(name, key = :__unset)
  data = self[name]
  data = data[key] if key != :__unset && data.respond_to?(:[])
  (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}'" <<
      (key == :__unset ? "" : " and key '#{key}'")
  end
  data
end

#key?(name) ⇒ Boolean Also known as: option?

Returns true if the given option exists.

Returns:

  • (Boolean)


68
69
70
# File 'lib/hexapdf/configuration.rb', line 68

def key?(name)
  options.key?(name)
end

#merge(config) ⇒ Object

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.



88
89
90
91
92
93
# File 'lib/hexapdf/configuration.rb', line 88

def merge(config)
  config = (config.kind_of?(self.class) ? config.options : config)
  self.class.new(options.merge(config) do |_key, old, new|
                   old.kind_of?(Hash) && new.kind_of?(Hash) ? old.merge(new) : new
                 end)
end