Class: PacketGen::Types::Array Abstract
- Inherits:
-
Object
- Object
- PacketGen::Types::Array
- Includes:
- Enumerable
- 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.
Direct Known Subclasses
Header::DHCP::Options, Header::DHCPv6::Options, Header::DHCPv6::RequestedOptions, Header::DNS::Name, Header::DNS::RRSection, Header::IGMPv3::GroupRecords, Header::IKE::Attributes, Header::IKE::SAProposals, Header::IKE::TrafficSelectors, Header::IKE::Transforms, 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::TCP::Options, ArrayOfInt16, ArrayOfInt16le, ArrayOfInt32, ArrayOfInt32le, ArrayOfInt8
Constant Summary collapse
- HUMAN_SEPARATOR =
Separator used in #to_human. May be ovverriden by subclasses
','
Class Method Summary collapse
-
.set_of(klass) ⇒ void
Define type of objects in set.
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
trueif contains no element. -
#first ⇒ Object
Return first element.
-
#force_binary(str) ⇒ String
Force binary encoding for
str. -
#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(str) ⇒ self
Populate object from a string.
-
#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.
Constructor Details
#initialize(options = {}) ⇒ Array
Returns a new instance of Array.
36 37 38 39 |
# File 'lib/packetgen/types/array.rb', line 36 def initialize(={}) @counter = [:counter] @array = [] end |
Class Method Details
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
140 141 142 143 144 |
# File 'lib/packetgen/types/array.rb', line 140 def <<(obj) push obj @counter.read(@counter.to_i + 1) if @counter self end |
#==(other) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/packetgen/types/array.rb', line 54 def ==(other) case other when Array @array == other.to_a else @array == other end end |
#[](index) ⇒ Object
Return the element at index.
50 51 52 |
# File 'lib/packetgen/types/array.rb', line 50 def [](index) @array[index] end |
#clear ⇒ void
This method returns an undefined value.
Clear array.
65 66 67 |
# File 'lib/packetgen/types/array.rb', line 65 def clear @array.clear end |
#clear! ⇒ void
This method returns an undefined value.
Clear array. Reset associated counter, if any.
71 72 73 74 |
# File 'lib/packetgen/types/array.rb', line 71 def clear! @array.clear @counter.read(0) if @counter end |
#delete(obj) ⇒ Object
Delete an object from this array. Update associated counter if any
79 80 81 82 83 |
# File 'lib/packetgen/types/array.rb', line 79 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.
88 89 90 91 92 |
# File 'lib/packetgen/types/array.rb', line 88 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.
97 98 99 |
# File 'lib/packetgen/types/array.rb', line 97 def each @array.each { |el| yield el } end |
#empty? ⇒ Booelan
Return true if contains no element.
103 104 105 |
# File 'lib/packetgen/types/array.rb', line 103 def empty? @array.empty? end |
#first ⇒ Object
Return first element
109 110 111 |
# File 'lib/packetgen/types/array.rb', line 109 def first @array.first end |
#force_binary(str) ⇒ String
Force binary encoding for str
198 199 200 |
# File 'lib/packetgen/types/array.rb', line 198 def force_binary(str) PacketGen.force_binary str end |
#initialize_copy(_other) ⇒ Object
Initialize array for copy:
-
duplicate internal array.
43 44 45 |
# File 'lib/packetgen/types/array.rb', line 43 def initialize_copy(_other) @array = @array.dup end |
#last ⇒ Object
Return last element.
115 116 117 |
# File 'lib/packetgen/types/array.rb', line 115 def last @array.last end |
#push(obj) ⇒ Array
depend on private method #record_from_hash which should be declared by subclasses.
Add an object to this array
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/packetgen/types/array.rb', line 124 def push(obj) obj = case obj when Hash record_from_hash obj else obj end @array << obj self end |
#read(str) ⇒ self
Populate object from a string
149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/packetgen/types/array.rb', line 149 def read(str) clear return self if str.nil? return self if @counter && @counter.to_i.zero? force_binary str klass = self.class.class_eval { @klass } until str.empty? obj = klass.new.read(str) @array << obj str.slice!(0, obj.sz) break if @counter && self.size == @counter.to_i end self end |
#size ⇒ Integer Also known as: length
Get number of element in array
166 167 168 |
# File 'lib/packetgen/types/array.rb', line 166 def size @array.size end |
#sz ⇒ Integer
Get size in bytes
173 174 175 |
# File 'lib/packetgen/types/array.rb', line 173 def sz to_s.size end |
#to_a ⇒ ::Array
Return an Array
179 180 181 |
# File 'lib/packetgen/types/array.rb', line 179 def to_a @array end |
#to_human ⇒ String
Get a human readable string
191 192 193 |
# File 'lib/packetgen/types/array.rb', line 191 def to_human @array.map(&:to_human).join(self.class::HUMAN_SEPARATOR) end |
#to_s ⇒ String
Get binary string
185 186 187 |
# File 'lib/packetgen/types/array.rb', line 185 def to_s @array.map(&:to_s).join end |