Module: Pdfrb::Model::Cos::Fields::DictionaryConverter

Defined in:
lib/pdfrb/model/cos/fields.rb

Overview

---- Converter modules ---- Stateless. usable_for?(type) filters; convert(data, type, document) returns the converted value or nil to fall through.

Class Method Summary collapse

Class Method Details

.additional_typesObject



83
# File 'lib/pdfrb/model/cos/fields.rb', line 83

def additional_types; ::Hash; end

.convert(data, type, document) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/pdfrb/model/cos/fields.rb', line 85

def convert(data, type, document)
  first = type.first
  return if first.nil?
  return if data.is_a?(first) && !upgradeable?(data, first)
  return unless data.is_a?(::Hash) || data.is_a?(Pdfrb::Model::Cos::Dictionary)

  # No `type:` — let document#wrap dispatch via /Type so a
  # field typed as bare Cos::Dictionary still upgrades to a
  # specific Type::* subclass when the value has a /Type.
  document&.wrap(data)
end

.upgradeable?(data, declared) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/pdfrb/model/cos/fields.rb', line 97

def upgradeable?(data, declared)
  # Already-typed subclass of declared base — leave alone.
  return false unless data.is_a?(Pdfrb::Model::Cos::Dictionary)
  return false if data.class == Pdfrb::Model::Cos::Dictionary

  data.class <= declared
end

.usable_for?(type) ⇒ Boolean

Returns:



78
79
80
81
# File 'lib/pdfrb/model/cos/fields.rb', line 78

def usable_for?(type)
  return true if type.is_a?(Symbol)
  type.is_a?(Class) && type <= Pdfrb::Model::Cos::Dictionary
end