Class: PacketGen::Types::Array Abstract
- Inherits:
-
Object
- Object
- PacketGen::Types::Array
- Extended by:
- Forwardable
- Includes:
- Enumerable, Fieldable, LengthFrom
- Defined in:
- lib/packetgen/types/array.rb
Overview
Base class to define set of Fields subclasses.
#record_from_hash
Subclasses should define private method #record_from_hash
. This method is called by #push to add an object to the set.
A default method is defined by Array: it calls constructor of class defined by Array.set_of.
#real_type
Subclasses should define private method #real_type
if Array.set_of type may be subclassed. This method should return real class to use. It takes an only argument, which is of type given by Array.set_of.
Default behaviour of this method is to return argument’s class.
Direct Known Subclasses
Header::DHCP::Options, Header::DHCPv6::Options, Header::DHCPv6::RequestedOptions, Header::DNS::ArrayOfOptions, Header::DNS::Name, Header::DNS::RRSection, Header::Dot11::ArrayOfElements, Header::IGMPv3::GroupRecords, Header::IP::ArrayOfAddr, Header::IP::Options, Header::IPv6::ArrayOfAddr, Header::IPv6::Options, Header::MLDv2::McastAddressRecords, Header::OSPFv2::ArrayOfExternal, Header::OSPFv2::ArrayOfLSA, Header::OSPFv2::ArrayOfLSR, Header::OSPFv2::ArrayOfLink, Header::OSPFv2::ArrayOfTosMetric, Header::OSPFv3::ArrayOfIPv6Prefix, Header::OSPFv3::ArrayOfLSA, Header::OSPFv3::ArrayOfLSR, Header::OSPFv3::ArrayOfLink, Header::SCTP::ArrayOfChunk, Header::SCTP::ArrayOfError, Header::SCTP::ArrayOfParameter, Header::TCP::Options, ArrayOfInt16, ArrayOfInt16le, ArrayOfInt32, ArrayOfInt32le, ArrayOfInt8
Constant Summary collapse
- HUMAN_SEPARATOR =
Separator used in #to_human. May be ovverriden by subclasses
','
Constants included from LengthFrom
Class Method Summary collapse
-
.set_of(klass) ⇒ void
Define type of objects in set.
-
.set_of_klass ⇒ Class
Get class set with Array.set_of.
Instance Method Summary collapse
-
#<<(obj) ⇒ Array
abstract
Add an object to this array, and increment associated counter, if any.
- #==(other) ⇒ Object
-
#[](index) ⇒ Object
Return the element at
index
. -
#clear ⇒ void
Clear array.
-
#clear! ⇒ void
Clear array.
-
#delete(obj) ⇒ Object
Delete an object from this array.
-
#delete_at(index) ⇒ Object?
Delete element at
index
. -
#each ⇒ Array
Calls the given block once for each element in self, passing that element as a parameter.
-
#empty? ⇒ Booelan
Return
true
if contains no element. -
#first ⇒ Object
Return first element.
-
#initialize(options = {}) ⇒ Array
constructor
A new instance of Array.
-
#initialize_copy(_other) ⇒ Object
Initialize array for copy: * duplicate internal array.
-
#last ⇒ Object
Return last element.
-
#push(obj) ⇒ Array
abstract
Add an object to this array.
-
#read(data) ⇒ self
Populate object from a string or from an array of hashes.
-
#size ⇒ Integer
(also: #length)
Get number of element in array.
-
#sz ⇒ Integer
Get size in bytes.
-
#to_a ⇒ ::Array
Return an Array.
-
#to_human ⇒ String
Get a human readable string.
-
#to_s ⇒ String
Get binary string.
Methods included from LengthFrom
#initialize_length_from, #read_with_length_from, #sz_to_read
Methods included from Fieldable
Constructor Details
#initialize(options = {}) ⇒ Array
Returns a new instance of Array.
85 86 87 88 89 |
# File 'lib/packetgen/types/array.rb', line 85 def initialize(={}) @counter = [:counter] @array = [] initialize_length_from() end |
Class Method Details
.set_of(klass) ⇒ void
77 78 79 |
# File 'lib/packetgen/types/array.rb', line 77 def set_of(klass) @klass = klass end |
.set_of_klass ⇒ Class
Get class set with set_of.
70 71 72 |
# File 'lib/packetgen/types/array.rb', line 70 def set_of_klass @klass end |
Instance Method Details
#<<(obj) ⇒ Array
depend on private method #record_from_hash
which should be declared by subclasses.
Add an object to this array, and increment associated counter, if any
152 153 154 155 156 |
# File 'lib/packetgen/types/array.rb', line 152 def <<(obj) push obj @counter&.read(@counter.to_i + 1) self end |
#==(other) ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/packetgen/types/array.rb', line 97 def ==(other) @array == case other when Array other.to_a else other end end |
#[](index) ⇒ Object
Return the element at index
.
58 |
# File 'lib/packetgen/types/array.rb', line 58 def_delegators :@array, :[], :clear, :each, :empty?, :first, :last, :size |
#clear ⇒ void
This method returns an undefined value.
Clear array.
58 |
# File 'lib/packetgen/types/array.rb', line 58 def_delegators :@array, :[], :clear, :each, :empty?, :first, :last, :size |
#clear! ⇒ void
This method returns an undefined value.
Clear array. Reset associated counter, if any.
108 109 110 111 |
# File 'lib/packetgen/types/array.rb', line 108 def clear! @array.clear @counter&.read(0) end |
#delete(obj) ⇒ Object
Delete an object from this array. Update associated counter if any
116 117 118 119 120 |
# File 'lib/packetgen/types/array.rb', line 116 def delete(obj) deleted = @array.delete(obj) @counter.read(@counter.to_i - 1) if @counter && deleted deleted end |
#delete_at(index) ⇒ Object?
Delete element at index
.
125 126 127 128 129 |
# File 'lib/packetgen/types/array.rb', line 125 def delete_at(index) deleted = @array.delete_at(index) @counter.read(@counter.to_i - 1) if @counter && deleted deleted end |
#each ⇒ Array
Calls the given block once for each element in self, passing that element as a parameter. Returns the array itself.
58 |
# File 'lib/packetgen/types/array.rb', line 58 def_delegators :@array, :[], :clear, :each, :empty?, :first, :last, :size |
#empty? ⇒ Booelan
Return true
if contains no element.
58 |
# File 'lib/packetgen/types/array.rb', line 58 def_delegators :@array, :[], :clear, :each, :empty?, :first, :last, :size |
#first ⇒ Object
Return first element
58 |
# File 'lib/packetgen/types/array.rb', line 58 def_delegators :@array, :[], :clear, :each, :empty?, :first, :last, :size |
#initialize_copy(_other) ⇒ Object
Initialize array for copy:
-
duplicate internal array.
93 94 95 |
# File 'lib/packetgen/types/array.rb', line 93 def initialize_copy(_other) @array = @array.dup end |
#last ⇒ Object
Return last element.
58 |
# File 'lib/packetgen/types/array.rb', line 58 def_delegators :@array, :[], :clear, :each, :empty?, :first, :last, :size |
#push(obj) ⇒ Array
depend on private method #record_from_hash
which should be declared by subclasses.
Add an object to this array
136 137 138 139 140 141 142 143 144 145 |
# File 'lib/packetgen/types/array.rb', line 136 def push(obj) obj = case obj when Hash record_from_hash obj else obj end @array << obj self end |
#read(data) ⇒ self
Populate object from a string or from an array of hashes
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/packetgen/types/array.rb', line 161 def read(data) clear case data when ::Array read_from_array(data) else read_from_string(data) end self end |
#size ⇒ Integer Also known as: length
Get number of element in array
58 |
# File 'lib/packetgen/types/array.rb', line 58 def_delegators :@array, :[], :clear, :each, :empty?, :first, :last, :size |
#sz ⇒ Integer
Get size in bytes
174 175 176 |
# File 'lib/packetgen/types/array.rb', line 174 def sz to_s.size end |
#to_a ⇒ ::Array
Return an Array
180 181 182 |
# File 'lib/packetgen/types/array.rb', line 180 def to_a @array end |
#to_human ⇒ String
Get a human readable string
192 193 194 |
# File 'lib/packetgen/types/array.rb', line 192 def to_human @array.map(&:to_human).join(self.class::HUMAN_SEPARATOR) end |
#to_s ⇒ String
Get binary string
186 187 188 |
# File 'lib/packetgen/types/array.rb', line 186 def to_s @array.map(&:to_s).join end |