Class: Y2Network::InterfacesCollection
- Inherits:
-
Object
- Object
- Y2Network::InterfacesCollection
- Extended by:
- Forwardable
- Includes:
- CanBeCopied, Yast2::Equatable, Yast::Logger
- Defined in:
- src/lib/y2network/interfaces_collection.rb
Overview
A container for network devices.
Objects of this class are able to keep a list of interfaces and perform simple queries on such a list. In the end should implement methods for mass operations over network interfaces
Instance Attribute Summary collapse
-
#interfaces ⇒ Array<Interface>
(also: #to_a)
readonly
List of interfaces.
Instance Method Summary collapse
-
#+(other) ⇒ InterfacesCollection
Returns a new collection including elements from both collections.
-
#-(other) ⇒ InterfacesCollection
Returns a new collection including only the elements that are not in the given collection.
-
#by_busid(busid) ⇒ Interface?
Returns an interface with the given hardware busid if present.
-
#by_name(name) ⇒ Interface?
Returns an interface with the given name if present.
-
#by_type(type) ⇒ InterfacesCollection
Returns list of interfaces of given type.
-
#delete_if(&block) ⇒ InterfacesCollection
Deletes elements which meet a given condition.
- #eql_hash ⇒ Object
-
#free_name(prefix) ⇒ String
Returns free interface name for given prefix.
-
#free_names(prefix, count) ⇒ Array<String>
Returns free interface name for given prefix.
-
#initialize(interfaces = []) ⇒ InterfacesCollection
constructor
Constructor.
-
#known_names ⇒ Array<String>
Returns all interfaces names.
-
#physical ⇒ InterfacesCollection
Returns the list of physical interfaces.
Methods included from CanBeCopied
Constructor Details
#initialize(interfaces = []) ⇒ InterfacesCollection
Constructor
61 62 63 |
# File 'src/lib/y2network/interfaces_collection.rb', line 61 def initialize(interfaces = []) @interfaces = interfaces end |
Instance Attribute Details
#interfaces ⇒ Array<Interface> (readonly) Also known as: to_a
Returns List of interfaces.
50 51 52 |
# File 'src/lib/y2network/interfaces_collection.rb', line 50 def interfaces @interfaces end |
Instance Method Details
#+(other) ⇒ InterfacesCollection
Returns a new collection including elements from both collections
150 151 152 |
# File 'src/lib/y2network/interfaces_collection.rb', line 150 def +(other) self.class.new(to_a + other.to_a) end |
#-(other) ⇒ InterfacesCollection
Returns a new collection including only the elements that are not in the given collection
158 159 160 |
# File 'src/lib/y2network/interfaces_collection.rb', line 158 def -(other) self.class.new(to_a - other.to_a) end |
#by_busid(busid) ⇒ Interface?
Returns an interface with the given hardware busid if present
88 89 90 91 92 |
# File 'src/lib/y2network/interfaces_collection.rb', line 88 def by_busid(busid) interfaces.find do |iface| iface.hardware && iface.hardware.busid == busid end end |
#by_name(name) ⇒ Interface?
It uses the hardware's name as a fallback if interface's name is not set
Returns an interface with the given name if present
77 78 79 80 81 82 |
# File 'src/lib/y2network/interfaces_collection.rb', line 77 def by_name(name) interfaces.find do |iface| iface_name = iface.name || iface.hardware.name iface_name == name end end |
#by_type(type) ⇒ InterfacesCollection
Returns list of interfaces of given type
98 99 100 101 |
# File 'src/lib/y2network/interfaces_collection.rb', line 98 def by_type(type) type = InterfaceType.from_short_name(type.to_s) unless type.is_a?(InterfaceType) InterfacesCollection.new(interfaces.select { |i| i.type == type }) end |
#delete_if(&block) ⇒ InterfacesCollection
Deletes elements which meet a given condition
113 114 115 116 |
# File 'src/lib/y2network/interfaces_collection.rb', line 113 def delete_if(&block) interfaces.delete_if(&block) self end |
#eql_hash ⇒ Object
65 66 67 68 69 |
# File 'src/lib/y2network/interfaces_collection.rb', line 65 def eql_hash h = super h[:interfaces] = h[:interfaces].sort_by(&:hash) if h.keys.include?(:interfaces) h end |
#free_name(prefix) ⇒ String
Returns free interface name for given prefix
129 130 131 |
# File 'src/lib/y2network/interfaces_collection.rb', line 129 def free_name(prefix) free_names(prefix, 1).first end |
#free_names(prefix, count) ⇒ Array<String>
Returns free interface name for given prefix
134 135 136 137 138 139 140 141 142 143 144 |
# File 'src/lib/y2network/interfaces_collection.rb', line 134 def free_names(prefix, count) result = [] # TODO: when switch rubocop use endless range `(0..)` (0..100000).each do |i| candidate = prefix + i.to_s next if by_name(candidate) result << candidate return result if result.size == count end end |
#known_names ⇒ Array<String>
Returns all interfaces names
For those interfaces that are renamed, the new and old names are included in the list.
124 125 126 |
# File 'src/lib/y2network/interfaces_collection.rb', line 124 def known_names @interfaces.map { |i| [i.old_name, i.name] }.flatten.compact end |
#physical ⇒ InterfacesCollection
Returns the list of physical interfaces
106 107 108 |
# File 'src/lib/y2network/interfaces_collection.rb', line 106 def physical interfaces.select { |i| i.is_a?(PhysicalInterface) } end |