Class: Pdfrb::Importer

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/importer.rb

Overview

Cross-document object importer. Deep-copies a value (and any references it transitively reaches) from a source document into this document, allocating fresh oids and rewriting references.

Cycle-safe: a "currently being imported" set guards against infinite recursion when objects reference each other.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_document) ⇒ Importer

Returns a new instance of Importer.



13
14
15
16
17
# File 'lib/pdfrb/importer.rb', line 13

def initialize(target_document)
  @target = target_document
  @mappings = {}        # source_oid -> target Pdfrb::Model::Object
  @in_progress = {}     # source_oid -> half-built target obj
end

Instance Attribute Details

#mappingsObject (readonly)

Returns the value of attribute mappings.



11
12
13
# File 'lib/pdfrb/importer.rb', line 11

def mappings
  @mappings
end

#targetObject (readonly)

Returns the value of attribute target.



11
12
13
# File 'lib/pdfrb/importer.rb', line 11

def target
  @target
end

Instance Method Details

#import(source_value, source_document) ⇒ Object

Import source_value (resolved against source_document) into the target. Returns the imported value (NOT wrapped in an Object; the caller decides oid assignment via Document#add).



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pdfrb/importer.rb', line 22

def import(source_value, source_document)
  case source_value
  when Pdfrb::Model::Reference
    import_reference(source_value, source_document)
  when ::Hash
    import_hash(source_value, source_document)
  when ::Array
    source_value.map { |v| import(v, source_document) }
  when Pdfrb::Model::Object
    import(source_value.value, source_document)
  else
    source_value
  end
end