Module: HexaPDF::DictionaryFields::DictionaryConverter

Defined in:
lib/hexapdf/dictionary_fields.rb

Overview

Converter module for fields of type Dictionary and its subclasses.

The first class in the type array of the field is used for the conversion. Symbol names for classes may also be used since they are automatically resolved.

Class Method Summary collapse

Class Method Details

.additional_typesObject

Dictionary fields can also contain simple hashes.



201
202
203
# File 'lib/hexapdf/dictionary_fields.rb', line 201

def self.additional_types
  Hash
end

.convert(data, type, document) ⇒ Object

Wraps the given data value in the PDF specific type class if it can be converted. Otherwise returns nil.



207
208
209
210
211
212
# File 'lib/hexapdf/dictionary_fields.rb', line 207

def self.convert(data, type, document)
  return if data.kind_of?(type.first) ||
    !(data.kind_of?(Hash) || data.kind_of?(HexaPDF::Dictionary)) ||
    (type.first <= HexaPDF::Stream && (data.kind_of?(Hash) || data.data.stream.nil?))
  document.wrap(data, type: type.first)
end

.usable_for?(type) ⇒ Boolean

This converter is used when either a Symbol is provided as type (for lazy loading) or when the type is a class derived from the Dictionary class.

Returns:



195
196
197
198
# File 'lib/hexapdf/dictionary_fields.rb', line 195

def self.usable_for?(type)
  type.kind_of?(Symbol) ||
    (type.respond_to?(:ancestors) && type.ancestors.include?(HexaPDF::Dictionary))
end