Class: HexaPDF::PDFArray
- 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
Instance Attribute Summary
Attributes inherited from Object
#data, #document, #must_be_indirect
Instance Method Summary collapse
-
#<<(data) ⇒ Object
Append a value to the array.
- #[](arg1, arg2 = nil) ⇒ Object
-
#[]=(index, data) ⇒ Object
Stores the data under the given index in the array.
-
#delete(object) ⇒ Object
Deletes all values from the PDFArray that are equal to the given object.
-
#delete_at(index) ⇒ Object
Deletes the value at the given index.
-
#each ⇒ Object
:call-seq: array.each {|value| block} -> array array.each -> Enumerator.
-
#empty? ⇒ Boolean
Returns
trueif the array has no elements. -
#index(*obj, &block) ⇒ Object
:call-seq: array.index(obj) -> int or nil array.index {|item| block } -> int or nil array.index -> Enumerator.
-
#insert(index, *objects) ⇒ Object
Insert one or more values into the array at the given index.
-
#length ⇒ Object
(also: #size)
Returns the number of elements in the array.
-
#reject! ⇒ Object
:call-seq: array.reject! {|item| block } -> array or nil array.reject! -> Enumerator.
-
#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.
-
#to_ary ⇒ Object
Returns an array containing the preprocessed values (like in #[]).
-
#values_at(*indices) ⇒ Object
Returns the values at the given indices.
Methods inherited from Object
#<=>, #==, #cache, #cached?, #clear_cache, 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].instance_of?(HexaPDF::Object) && !data.kind_of?(HexaPDF::Object) && !data.kind_of?(HexaPDF::Reference) value[index].value = data else value[index] = data end end |
#delete(object) ⇒ Object
Deletes all values from the PDFArray that are equal to the given object.
Returns the last deleted item, or nil if no matching item is found.
119 120 121 |
# File 'lib/hexapdf/pdf_array.rb', line 119 def delete(object) value.delete(object) 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 |
#each ⇒ Object
: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 #[].
178 179 180 181 182 |
# File 'lib/hexapdf/pdf_array.rb', line 178 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.
167 168 169 |
# File 'lib/hexapdf/pdf_array.rb', line 167 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.
156 157 158 |
# File 'lib/hexapdf/pdf_array.rb', line 156 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 |
#length ⇒ Object Also known as: size
Returns the number of elements in the array.
161 162 163 |
# File 'lib/hexapdf/pdf_array.rb', line 161 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.
145 146 147 |
# File 'lib/hexapdf/pdf_array.rb', line 145 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.
130 131 132 133 134 135 136 137 |
# File 'lib/hexapdf/pdf_array.rb', line 130 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_ary ⇒ Object
Returns an array containing the preprocessed values (like in #[]).
185 186 187 |
# File 'lib/hexapdf/pdf_array.rb', line 185 def to_ary each.to_a 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 |