Class: BetterIpaddr::Space

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/better_ipaddr/space.rb

Overview

Address space utilities

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(networks, space: nil, family: nil) ⇒ Space

Returns a new instance of Space.

Raises:

  • (ArgumentError)


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

#familyObject (readonly)

Returns the value of attribute family.



9
10
11
# File 'lib/better_ipaddr/space.rb', line 9

def family
  @family
end

#networksObject (readonly)

Returns the value of attribute networks.



9
10
11
# File 'lib/better_ipaddr/space.rb', line 9

def networks
  @networks
end

#spaceObject (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

#eachObject



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

#gapsObject



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

#summarizeObject



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