/
connection_configs_collection.rb
115 lines (102 loc) · 3.97 KB
/
connection_configs_collection.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# Copyright (c) [2019] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.
require "yast"
require "forwardable"
module Y2Network
# A container for connection configurations objects.
#
# @example Create a new collection
# eth0 = Y2Network::ConnectionConfig::Ethernet.new
# collection = Y2Network::ConnectionConfigsCollection.new([eth0])
#
# @example Find a connection config using its name
# config = collection.by_name("eth0") #=> #<Y2Network::ConnectionConfig::Ethernet:0x...>
class ConnectionConfigsCollection
extend Forwardable
include Yast::Logger
attr_reader :connection_configs
alias_method :to_a, :connection_configs
def_delegators :@connection_configs, :each, :find, :push, :<<, :reject!, :map, :flat_map,
:any?, :size, :first, :empty?
# Constructor
#
# @param connection_configs [Array<ConnectionConfig>] List of connection configurations
def initialize(connection_configs = [])
@connection_configs = connection_configs
end
# Returns a connection configuration with the given name if present
#
# @param name [String] Connection name
# @return [ConnectionConfig::Base,nil] Connection config with the given name or nil if not found
def by_name(name)
connection_configs.find { |c| c.name == name }
end
# Returns connection configurations which are associated to the given interface
#
# @param interface_name [String] Interface name
# @return [Array<ConnectionConfig::Base>] Associated connection configs
def by_interface(interface_name)
connection_configs.select { |c| c.interface == interface_name }
end
# Returns connections with any of the given internal IDs
#
# @param ids [Array<Integer>] Internal IDs
# @return [Array<ConnectionConfig::Base>] Connection config with the given IDs
def by_ids(*ids)
select { |c| ids.include?(c.id) }
end
# Adds or updates a connection configuration
#
# @note It uses the name to do the matching.
#
# @param connection_config [ConnectionConfig::Base] New connection configuration object
def add_or_update(connection_config)
idx = connection_configs.find_index { |c| c.name == connection_config.name }
if idx
connection_configs[idx] = connection_config
else
connection_configs << connection_config
end
end
# Removes a connection configuration
#
# @note It uses the name to do the matching.
#
# @param connection_config [ConnectionConfig::Base,String] Connection configuration object or name
def remove(connection_config)
name = connection_config.respond_to?(:name) ? connection_config.name : connection_config
connection_configs.reject! { |c| c.name == name }
end
# Selects connections which satisfy the given +block+
#
# @param block [Proc]
# @return [ConnectionConfigsCollection] Collection including the selected connections
def select(&block)
self.class.new(to_a.select(&block))
end
# Compares ConnectionConfigsCollection
#
# @return [Boolean] true when both collections contain only equal connections,
# false otherwise
def ==(other)
((connection_configs - other.connection_configs) + (other.connection_configs - connection_configs)).empty?
end
alias_method :eql?, :==
end
end