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.

Class Method Summary collapse

Class Method Details

.additional_typesObject

Dictionary fields can also contain simple hashes.



186
187
188
# File 'lib/hexapdf/dictionary_fields.rb', line 186

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.



192
193
194
195
196
197
# File 'lib/hexapdf/dictionary_fields.rb', line 192

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:



180
181
182
183
# File 'lib/hexapdf/dictionary_fields.rb', line 180

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