Class: Codebot::NetworkManager
- Inherits:
-
Object
- Object
- Codebot::NetworkManager
- Defined in:
- lib/codebot/network_manager.rb
Overview
This class manages the networks associated with a configuration.
Instance Attribute Summary collapse
-
#config ⇒ Config
readonly
The configuration managed by this class.
Instance Method Summary collapse
-
#check_channels!(integration) ⇒ Object
Checks that all channels associated with an integration belong to a valid network.
-
#create(params) ⇒ Object
Creates a new network from the given parameters.
-
#destroy(name, params) ⇒ Object
Destroys a network.
-
#find_network(name) ⇒ Network?
Finds a network given its name.
-
#find_network!(name) ⇒ Network
Finds a network given its name.
-
#initialize(config) ⇒ NetworkManager
constructor
Constructs a new network manager for a specified configuration.
-
#list(search) ⇒ Object
Lists all networks, or networks with names containing the given search term.
-
#update(name, params) ⇒ Object
Updates a network with the given parameters.
Constructor Details
#initialize(config) ⇒ NetworkManager
Constructs a new network manager for a specified configuration.
14 15 16 |
# File 'lib/codebot/network_manager.rb', line 14 def initialize(config) @config = config end |
Instance Attribute Details
#config ⇒ Config (readonly)
Returns the configuration managed by this class.
9 10 11 |
# File 'lib/codebot/network_manager.rb', line 9 def config @config end |
Instance Method Details
#check_channels!(integration) ⇒ Object
Checks that all channels associated with an integration belong to a valid network.
97 98 99 100 101 |
# File 'lib/codebot/network_manager.rb', line 97 def check_channels!(integration) integration.channels.map(&:network).map(&:name).each do |network| find_network!(network) end end |
#create(params) ⇒ Object
Creates a new network from the given parameters.
21 22 23 24 25 26 27 28 |
# File 'lib/codebot/network_manager.rb', line 21 def create(params) network = Network.new(params.merge(config: {})) @config.transaction do check_name_available!(network.name) @config.networks << network network_feedback(network, :created) unless params[:quiet] end end |
#destroy(name, params) ⇒ Object
Destroys a network.
49 50 51 52 53 54 55 |
# File 'lib/codebot/network_manager.rb', line 49 def destroy(name, params) @config.transaction do network = find_network!(name) @config.networks.delete network network_feedback(network, :destroyed) unless params[:quiet] end end |
#find_network(name) ⇒ Network?
Finds a network given its name.
76 77 78 |
# File 'lib/codebot/network_manager.rb', line 76 def find_network(name) @config.networks.find { |net| net.name_eql? name } end |
#find_network!(name) ⇒ Network
Finds a network given its name.
85 86 87 88 89 90 91 |
# File 'lib/codebot/network_manager.rb', line 85 def find_network!(name) network = find_network(name) return network unless network.nil? raise CommandError, "a network with the name #{name.inspect} " \ 'does not exist' end |
#list(search) ⇒ Object
Lists all networks, or networks with names containing the given search term.
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/codebot/network_manager.rb', line 61 def list(search) @config.transaction do networks = @config.networks.dup unless search.nil? networks.select! { |net| net.name.downcase.include? search.downcase } end puts 'No networks found' if networks.empty? networks.each { |net| show_network net } end end |
#update(name, params) ⇒ Object
Updates a network with the given parameters.
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/codebot/network_manager.rb', line 34 def update(name, params) @config.transaction do network = find_network!(name) unless params[:name].nil? check_name_available_except!(params[:name], network) end network.update!(params) network_feedback(network, :updated) unless params[:quiet] end end |