Class: BetterIpaddr::Space

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Space.



10
11
12
13
14
15
16
17
18
19
# File 'lib/better_ipaddr/space.rb', line 10

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
  fail ArgumentError, "Address space #{@space.inspect} does not cover "\
                      "network #{outlier.inspect}"
end

Instance Attribute Details

#familyObject (readonly)

Returns the value of attribute family.



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

def family
  @family
end

#networksObject (readonly)

Returns the value of attribute networks.



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

def networks
  @networks
end

#spaceObject (readonly)

Returns the value of attribute space.



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

def space
  @space
end

Instance Method Details

#+(other) ⇒ Object



21
22
23
# File 'lib/better_ipaddr/space.rb', line 21

def +(other)
  self.class.new(networks + other.networks, family: family)
end

#==(other) ⇒ Object



25
26
27
# File 'lib/better_ipaddr/space.rb', line 25

def ==(other)
  networks == other.networks
end

#eachObject



29
30
31
32
33
34
35
36
37
# File 'lib/better_ipaddr/space.rb', line 29

def each
  if block_given?
    networks.each do |network|
      yield network
    end
  else
    networks.each
  end
end

#find_by_minimum_prefix_length(length) ⇒ Object



43
44
45
# File 'lib/better_ipaddr/space.rb', line 43

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



39
40
41
# File 'lib/better_ipaddr/space.rb', line 39

def find_by_minimum_size(size)
  find_all { |net| net.size >= size }.min_by(&:prefix_length)
end

#gapsObject



47
48
49
50
51
52
53
54
55
# File 'lib/better_ipaddr/space.rb', line 47

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



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/better_ipaddr/space.rb', line 57

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



71
72
73
74
75
76
77
78
79
# File 'lib/better_ipaddr/space.rb', line 71

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



81
82
83
# File 'lib/better_ipaddr/space.rb', line 81

def with_space(space)
  export(networks, space: space)
end