Class: BetterIpaddr::Space
- Inherits:
-
Object
- Object
- BetterIpaddr::Space
- Includes:
- Enumerable
- Defined in:
- lib/better_ipaddr/space.rb
Overview
Address space utilities
Instance Attribute Summary collapse
-
#family ⇒ Object
readonly
Returns the value of attribute family.
-
#networks ⇒ Object
readonly
Returns the value of attribute networks.
-
#space ⇒ Object
readonly
Returns the value of attribute space.
Instance Method Summary collapse
- #+(other) ⇒ Object
- #==(other) ⇒ Object
- #each ⇒ Object
- #find_by_minimum_prefix_length(length) ⇒ Object
- #find_by_minimum_size(size) ⇒ Object
- #gaps ⇒ Object
-
#initialize(networks, space: nil, family: nil) ⇒ Space
constructor
A new instance of Space.
- #summarize ⇒ Object
- #summarize_backtrack(list) ⇒ Object
- #with_space(space) ⇒ Object
Constructor Details
#initialize(networks, space: nil, family: nil) ⇒ Space
Returns a new instance of Space.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/better_ipaddr/space.rb', line 11 def initialize(networks, space: nil, family: nil) @family = family || infer_address_family(space, *networks) @networks = networks.map { |network| import(network) }.sort return unless space @space = space outlier = @networks.find { |net| !@space.cover?(net) } return unless outlier raise ArgumentError, "Address space #{@space.inspect} does not cover "\ "network #{outlier.inspect}" end |
Instance Attribute Details
#family ⇒ Object (readonly)
Returns the value of attribute family.
9 10 11 |
# File 'lib/better_ipaddr/space.rb', line 9 def family @family end |
#networks ⇒ Object (readonly)
Returns the value of attribute networks.
9 10 11 |
# File 'lib/better_ipaddr/space.rb', line 9 def networks @networks end |
#space ⇒ Object (readonly)
Returns the value of attribute space.
9 10 11 |
# File 'lib/better_ipaddr/space.rb', line 9 def space @space end |
Instance Method Details
#+(other) ⇒ Object
22 23 24 |
# File 'lib/better_ipaddr/space.rb', line 22 def +(other) self.class.new(networks + other.networks, family: family) end |
#==(other) ⇒ Object
26 27 28 |
# File 'lib/better_ipaddr/space.rb', line 26 def ==(other) networks == other.networks end |
#each ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/better_ipaddr/space.rb', line 30 def each if block_given? networks.each do |network| yield network end else networks.each end end |
#find_by_minimum_prefix_length(length) ⇒ Object
44 45 46 |
# File 'lib/better_ipaddr/space.rb', line 44 def find_by_minimum_prefix_length(length) find_all { |net| net.prefix_length >= length }.min_by(&:prefix_length) end |
#find_by_minimum_size(size) ⇒ Object
40 41 42 |
# File 'lib/better_ipaddr/space.rb', line 40 def find_by_minimum_size(size) find_all { |net| net.size >= size }.min_by(&:prefix_length) end |
#gaps ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/better_ipaddr/space.rb', line 48 def gaps return export([space]) if space && networks.empty? gap_networks = if space gaps_before + gaps_between + gaps_after else gaps_between end self.class.new(gap_networks, family: family) end |
#summarize ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/better_ipaddr/space.rb', line 58 def summarize out = [] networks.each do |network| summary = network.summarize_with(out.last) if summary out[-1] = summary summarize_backtrack(out) else out << network end end export(out) end |
#summarize_backtrack(list) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/better_ipaddr/space.rb', line 72 def summarize_backtrack(list) loop do break unless list.size >= 2 summary = list[-1].summarize_with(list[-2]) break unless summary list.pop 2 list << summary end end |
#with_space(space) ⇒ Object
82 83 84 |
# File 'lib/better_ipaddr/space.rb', line 82 def with_space(space) export(networks, space: space) end |