Class: Pdfrb::Model::Object

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

Overview

Base wrapper for every PDF value (direct or indirect). Carries the indirect-reference metadata (oid, gen) and a back-reference to the owning Document for lazy resolution.

The wrapped value is the raw Ruby representation:

Boolean  -> true / false
Integer  -> Integer
Real     -> Float
Name     -> Symbol
String   -> String (UTF-8 for text, BINARY for bytes)
Null     -> nil
Array    -> Array or PdfArray
Dict     -> Hash or Dictionary
Stream   -> Stream (subclass of Dictionary)
Ref      -> Reference

Direct Known Subclasses

Cos::Dictionary, PdfArray

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, oid: 0, gen: 0, document: nil) ⇒ Object

Returns a new instance of Object.



23
24
25
26
27
28
# File 'lib/pdfrb/model/object.rb', line 23

def initialize(value, oid: 0, gen: 0, document: nil)
  @value = value
  @oid = oid.to_i
  @gen = gen.to_i
  @document = document
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



21
22
23
# File 'lib/pdfrb/model/object.rb', line 21

def document
  @document
end

#genObject (readonly)

Returns the value of attribute gen.



21
22
23
# File 'lib/pdfrb/model/object.rb', line 21

def gen
  @gen
end

#oidObject (readonly)

Returns the value of attribute oid.



21
22
23
# File 'lib/pdfrb/model/object.rb', line 21

def oid
  @oid
end

#valueObject (readonly)

Returns the value of attribute value.



21
22
23
# File 'lib/pdfrb/model/object.rb', line 21

def value
  @value
end

Class Method Details

.define_type(type) ⇒ Object

Statically declare the /Type value for this class. Read via pdf_type. Used by Document#wrap to dispatch.



73
74
75
# File 'lib/pdfrb/model/object.rb', line 73

def define_type(type)
  @pdf_type = type
end

.pdf_typeObject



77
78
79
# File 'lib/pdfrb/model/object.rb', line 77

def pdf_type
  defined?(@pdf_type) ? @pdf_type : nil
end

Instance Method Details

#==(other) ⇒ Object



63
64
65
66
67
68
# File 'lib/pdfrb/model/object.rb', line 63

def ==(other)
  return false unless other.is_a?(Pdfrb::Model::Object)
  return object_id == other.object_id if indirect? && other.indirect?

  @value == other.value
end

#derefObject

Resolve a Reference; pass-through for everything else.



49
50
51
# File 'lib/pdfrb/model/object.rb', line 49

def deref
  self
end

#indirect?Boolean

True when this object is referenced indirectly (oid > 0).

Returns:

  • (Boolean)


38
39
40
# File 'lib/pdfrb/model/object.rb', line 38

def indirect?
  @oid > 0
end

#must_be_indirect?Boolean

Overridable. PDF types that must be indirect (Catalog, Pages, ...) override to return true.

Returns:

  • (Boolean)


44
45
46
# File 'lib/pdfrb/model/object.rb', line 44

def must_be_indirect?
  false
end

#pdf_typeObject

The PDF /Type name (e.g. :Catalog). Subclasses override via define_type. Falls back to value for raw dicts.



55
56
57
58
59
60
61
# File 'lib/pdfrb/model/object.rb', line 55

def pdf_type
  own = self.class.pdf_type
  return own if own
  return nil unless @value.is_a?(Hash)

  @value[:Type]
end

#refObject

The indirect reference denoting this object. The single seam for "a reference to this" — callers never construct Reference.new(obj.oid, obj.gen) by hand.



33
34
35
# File 'lib/pdfrb/model/object.rb', line 33

def ref
  Pdfrb::Model::Reference.new(oid, gen)
end