Class: HexaPDF::Reference

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/hexapdf/reference.rb

Overview

A reference to an indirect object.

The PDF syntax allows for references to existing and non-existing indirect objects. Such references are represented with objects of this class.

Note that after initialization changing the object or generation numbers is not possible anymore!

The methods #hash and #eql? are implemented so that objects of this class can be used as hash keys. Furthermore the implementation is compatible to the one of Object, i.e. the hash of a Reference object is the same as the hash of an indirect Object.

See: PDF1.7 s7.3.10, Object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(oid, gen = 0) ⇒ Reference

Creates a new Reference with the given object number and, optionally, generation number.



62
63
64
65
# File 'lib/hexapdf/reference.rb', line 62

def initialize(oid, gen = 0)
  @oid = Integer(oid)
  @gen = Integer(gen)
end

Instance Attribute Details

#genObject (readonly)

Returns the generation number of the referenced indirect object.



59
60
61
# File 'lib/hexapdf/reference.rb', line 59

def gen
  @gen
end

#oidObject (readonly)

Returns the object number of the referenced indirect object.



56
57
58
# File 'lib/hexapdf/reference.rb', line 56

def oid
  @oid
end

Instance Method Details

#<=>(other) ⇒ Object

Compares this object to another object.

If the other object does not respond to oid or gen, nil is returned. Otherwise objects are ordered first by object number and then by generation number.



71
72
73
74
# File 'lib/hexapdf/reference.rb', line 71

def <=>(other)
  return nil unless other.respond_to?(:oid) && other.respond_to?(:gen)
  (oid == other.oid ? gen <=> other.gen : oid <=> other.oid)
end

#==(other) ⇒ Object

Returns true if the other object is a Reference and has the same object and generation numbers.



78
79
80
# File 'lib/hexapdf/reference.rb', line 78

def ==(other)
  other.kind_of?(Reference) && oid == other.oid && gen == other.gen
end

#eql?(other) ⇒ Boolean

Returns true if the other object references the same PDF object as this reference object.

Returns:

  • (Boolean)


83
84
85
# File 'lib/hexapdf/reference.rb', line 83

def eql?(other)
  other.respond_to?(:oid) && oid == other.oid && other.respond_to?(:gen) && gen == other.gen
end

#hashObject

Computes the hash value based on the object and generation numbers.



88
89
90
# File 'lib/hexapdf/reference.rb', line 88

def hash
  oid.hash ^ gen.hash
end

#inspectObject

:nodoc:



92
93
94
# File 'lib/hexapdf/reference.rb', line 92

def inspect #:nodoc:
  "#<#{self.class.name} [#{oid}, #{gen}]>"
end