Class: HexaPDF::PDFArray

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/hexapdf/pdf_array.rb

Overview

Implementation of the PDF array type.

This is mainly done to provide automatic resolution of indirect object references when using the #[] method. Therefore not all Array methods are implemented - use the #value directly if other methods are needed.

See: PDF1.7 s7.3.6

Direct Known Subclasses

Rectangle

Constant Summary

Constants inherited from Object

Object::NOT_DUPLICATABLE_CLASSES

Instance Attribute Summary

Attributes inherited from Object

#data, #document, #must_be_indirect

Instance Method Summary collapse

Methods inherited from Object

#<=>, #==, deep_copy, #deep_copy, #document?, #eql?, #gen, #gen=, #hash, #indirect?, #initialize, #inspect, #must_be_indirect?, #null?, #oid, #oid=, #type, #validate, #value, #value=

Constructor Details

This class inherits a constructor from HexaPDF::Object

Instance Method Details

#<<(data) ⇒ Object

Append a value to the array.



102
103
104
# File 'lib/hexapdf/pdf_array.rb', line 102

def <<(data)
  value << data
end

#[](arg1, arg2 = nil) ⇒ Object

:call-seq:

array[index]             -> obj or nil
array[start, length]     -> new_array or nil
array[range]             -> new_array or nil

Returns the value at the given index, or a subarray using the given start and length, or a subarray specified by range.

This method should be used instead of direct access to a value because it provides some advantages:

  • References are automatically resolved.

  • Returns the native Ruby object for values with class HexaPDF::Object. However, all subclasses of HexaPDF::Object are returned as is (it makes no sense, for example, to return the hash that describes the Catalog instead of the Catalog object).



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/hexapdf/pdf_array.rb', line 68

def [](arg1, arg2 = nil)
  data = value[arg1, *arg2]
  return if data.nil?

  if arg2 || arg1.kind_of?(Range)
    index = (arg2 ? arg1 : arg1.begin)
    data.map! {|item| process_entry(item, index).tap { index += 1 } }
  else
    process_entry(data, arg1)
  end
end

#[]=(index, data) ⇒ Object

Stores the data under the given index in the array.

If the current value for this index has the class HexaPDF::Object (and only this, no subclasses) and the given data has not (including subclasses), the data is stored inside the HexaPDF::Object.



85
86
87
88
89
90
91
92
# File 'lib/hexapdf/pdf_array.rb', line 85

def []=(index, data)
  if value[index].class == HexaPDF::Object && !data.kind_of?(HexaPDF::Object) &&
      !data.kind_of?(HexaPDF::Reference)
    value[index].value = data
  else
    value[index] = data
  end
end

#delete_at(index) ⇒ Object

Deletes the value at the given index.



112
113
114
# File 'lib/hexapdf/pdf_array.rb', line 112

def delete_at(index)
  value.delete_at(index)
end

#eachObject

:call-seq:

array.each {|value| block}    -> array
array.each                    -> Enumerator

Calls the given block once for every value of the array.

Note that the yielded value is already preprocessed like in #[].



171
172
173
174
175
# File 'lib/hexapdf/pdf_array.rb', line 171

def each
  return to_enum(__method__) unless block_given?
  value.each_index {|index| yield(self[index]) }
  self
end

#empty?Boolean

Returns true if the array has no elements.

Returns:

  • (Boolean)


160
161
162
# File 'lib/hexapdf/pdf_array.rb', line 160

def empty?
  value.empty?
end

#index(*obj, &block) ⇒ Object

:call-seq:

array.index(obj)              -> int or nil
array.index {|item| block }   -> int or nil
array.index                   -> Enumerator

Returns the index of the first object such that object is == to obj, or, if a block is given, the index of the first object for which the block returns true.



149
150
151
# File 'lib/hexapdf/pdf_array.rb', line 149

def index(*obj, &block)
  find_index(*obj, &block)
end

#insert(index, *objects) ⇒ Object

Insert one or more values into the array at the given index.



107
108
109
# File 'lib/hexapdf/pdf_array.rb', line 107

def insert(index, *objects)
  value.insert(index, *objects)
end

#lengthObject Also known as: size

Returns the number of elements in the array.



154
155
156
# File 'lib/hexapdf/pdf_array.rb', line 154

def length
  value.length
end

#reject!Object

:call-seq:

array.reject! {|item| block }   -> array or nil
array.reject!                   -> Enumerator

Deletes all elements from the array for which the block returns true. If no changes were done, returns nil.



138
139
140
# File 'lib/hexapdf/pdf_array.rb', line 138

def reject!
  value.reject! {|item| yield(process_entry(item)) }
end

#slice!(arg1, arg2 = nil) ⇒ Object

:call-seq:

array.slice!(index)             -> obj or nil
array.slice!(start, length)     -> new_array or nil
array.slice!(range)             -> new_array or nil

Deletes the element(s) given by an index (and optionally a length) or by a range, and returns them or nil if the index is out of range.



123
124
125
126
127
128
129
130
# File 'lib/hexapdf/pdf_array.rb', line 123

def slice!(arg1, arg2 = nil)
  data = value.slice!(arg1, *arg2)
  if arg2 || arg1.kind_of?(Range)
    data.map! {|item| process_entry(item) }
  else
    process_entry(data)
  end
end

#to_aryObject

Returns a duplicate of the underlying array.



178
179
180
# File 'lib/hexapdf/pdf_array.rb', line 178

def to_ary
  value.dup
end

#values_at(*indices) ⇒ Object

Returns the values at the given indices.

See #[] for details



97
98
99
# File 'lib/hexapdf/pdf_array.rb', line 97

def values_at(*indices)
  indices.map! {|index| self[index] }
end