Class: HexaPDF::Importer
- Inherits:
-
Object
- Object
- HexaPDF::Importer
- Defined in:
- lib/hexapdf/importer.rb
Overview
The Importer class manages the process of copying objects from one Document to another.
It may seem unnecessary using an importer containing state for the task. However, by retaining some information about the already copied objects we can make sure that already imported objects don’t get imported again.
Two types of indirect objects are never imported from one document to another: the catalog and page tree nodes. If the catalog was imported, the whole source document would be imported. And if one page tree node would imported, the whole page tree would be imported.
See: Document#import
Defined Under Namespace
Classes: NullableWeakRef
Instance Attribute Summary collapse
-
#destination ⇒ Object
readonly
:nodoc:.
-
#source ⇒ Object
readonly
:nodoc:.
Class Method Summary collapse
-
.for(source:, destination:) ⇒ Object
Returns the Importer object for copying objects from the
source
to thedestination
document.
Instance Method Summary collapse
-
#import(object) ⇒ Object
Imports the given
object
from the source to the destination object and returns the imported object. -
#initialize(source:, destination:) ⇒ Importer
constructor
Initializes a new importer that can import objects from the
source
document to thedestination
document.
Constructor Details
#initialize(source:, destination:) ⇒ Importer
Initializes a new importer that can import objects from the source
document to the destination
document.
74 75 76 77 78 |
# File 'lib/hexapdf/importer.rb', line 74 def initialize(source:, destination:) @source = source @destination = destination @mapper = {} end |
Instance Attribute Details
#destination ⇒ Object (readonly)
:nodoc:
70 71 72 |
# File 'lib/hexapdf/importer.rb', line 70 def destination @destination end |
#source ⇒ Object (readonly)
:nodoc:
70 71 72 |
# File 'lib/hexapdf/importer.rb', line 70 def source @source end |
Class Method Details
.for(source:, destination:) ⇒ Object
Returns the Importer object for copying objects from the source
to the destination
document.
60 61 62 63 64 65 66 |
# File 'lib/hexapdf/importer.rb', line 60 def self.for(source:, destination:) @map ||= {} @map.keep_if {|_, v| v.source.weakref_alive? && v.destination.weakref_alive?} source = NullableWeakRef.new(source) destination = NullableWeakRef.new(destination) @map[[source.hash, destination.hash]] ||= new(source: source, destination: destination) end |
Instance Method Details
#import(object) ⇒ Object
Imports the given object
from the source to the destination object and returns the imported object.
Note: Indirect objects are automatically added to the destination document but direct or simple objects are not.
An error is raised if the object doesn’t belong to the source
document.
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/hexapdf/importer.rb', line 87 def import(object) mapped_object = @mapper[object.data] if object.kind_of?(HexaPDF::Object) if object.kind_of?(HexaPDF::Object) && object.document? && @source != object.document raise HexaPDF::Error, "Import error: Incorrect document object for importer" elsif mapped_object && mapped_object == @destination.object(mapped_object) mapped_object else duplicate(object) end end |