Class: IPAddr::Base
- Includes:
- BetterIpaddr::Constants, BetterIpaddr::InstanceMethods, Comparable, Enumerable
- Defined in:
- lib/better_ipaddr/classes.rb
Constant Summary
Constants included from BetterIpaddr::Constants
BetterIpaddr::Constants::FAMILY_TO_BIT_LENGTH, BetterIpaddr::Constants::NETMASK_TO_PREFIX_LENGTH, BetterIpaddr::Constants::PREFIX_LENGTH_TO_NETMASK, BetterIpaddr::Constants::SYMBOL_TO_FAMILY
Instance Attribute Summary
Attributes included from BetterIpaddr::InstanceMethods
Class Method Summary collapse
-
.[](address, mask = nil, family: self::FAMILY) ⇒ IPAddr, Nil
Create an IPAddr from the given object.
-
.from_integer(address, prefix_length, family: self::FAMILY) ⇒ IPAddr
Create an IPAddr from an Integer.
-
.from_ipaddr(address, prefix_length, family: self::FAMILY) ⇒ IPAddr
Create an IPAddr from an IPAddr.
-
.from_string(address, mask = nil, family: self::FAMILY) ⇒ IPAddr
Create an IPAddr from a String.
-
.integer_to_prefix_length(mask, family = self::FAMILY) ⇒ Integer
Convert an integer to a prefix length.
-
.ipaddr_to_prefix_length(mask, family = self::FAMILY) ⇒ Integer
Convert a netmask represented as an IPAddr to a prefix length.
-
.object_to_prefix_length(mask, family = self::FAMILY) ⇒ Integer
Convert an object to a prefix length.
-
.parse(address) ⇒ IPAddr::V4, ...
Convert the given string to an IPAddr subclass.
-
.specialize(address) ⇒ IPAddr::V4, ...
Return the given address as an instance of a class specific to its address family.
- .specialize_constants(family) ⇒ Object
-
.string_to_prefix_length(mask, family = self::FAMILY) ⇒ Integer
Convert a string to a prefix length.
-
.valid_prefix_length?(prefix_length, family: self::FAMILY) ⇒ Boolean
Return true if the given number is a valid prefix length, false otherwise.
Instance Method Summary collapse
- #address_family_bit_length ⇒ Object
- #inherited(cls) ⇒ Object
- #network? ⇒ Boolean
- #prefix_length ⇒ Object
Methods included from BetterIpaddr::InstanceMethods
#+, #-, #<=>, #==, #[], #base, #cidr, #cover?, #each, #first, #grow, #host?, #last, #netmask, #overlap?, #shrink, #size, #summarize_with, #to_range, #wildcard
Methods included from BetterIpaddr::ClassMethods
Class Method Details
.[](address, mask = nil, family: self::FAMILY) ⇒ IPAddr, Nil
Create an IPAddr from the given object.
Returns nil if the object is of a type that can’t be converted to an IPAddr.
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/better_ipaddr/classes.rb', line 23 def self.[](address, mask = nil, family: self::FAMILY) prefix_length = mask && object_to_prefix_length(mask, family) case address when Integer from_integer(address, prefix_length, family: family) when IPAddr from_ipaddr(address, prefix_length, family: family) when String from_string(address, prefix_length, family: family) end end |
.from_integer(address, prefix_length, family: self::FAMILY) ⇒ IPAddr
Create an IPAddr from an Integer.
42 43 44 |
# File 'lib/better_ipaddr/classes.rb', line 42 def self.from_integer(address, prefix_length, family: self::FAMILY) new(address, family).mask(prefix_length || FAMILY_TO_BIT_LENGTH[family]) end |
.from_ipaddr(address, prefix_length, family: self::FAMILY) ⇒ IPAddr
Create an IPAddr from an IPAddr.
52 53 54 |
# File 'lib/better_ipaddr/classes.rb', line 52 def self.from_ipaddr(address, prefix_length, family: self::FAMILY) new(address.to_i, family).mask(prefix_length || address.prefix_length) end |
.from_string(address, mask = nil, family: self::FAMILY) ⇒ IPAddr
Create an IPAddr from a String.
62 63 64 65 66 67 68 |
# File 'lib/better_ipaddr/classes.rb', line 62 def self.from_string(address, mask = nil, family: self::FAMILY) if mask new(address, family).mask(mask) else new(address, family) end end |
.integer_to_prefix_length(mask, family = self::FAMILY) ⇒ Integer
Convert an integer to a prefix length.
If the integer is within the range of possible prefix lengths, returns the same integer. Otherwise it assumes that the given integer is the integer representation of a netmask.
Returns nil if the integer can’t be converted.
98 99 100 101 102 103 104 105 |
# File 'lib/better_ipaddr/classes.rb', line 98 def self.integer_to_prefix_length(mask, family = self::FAMILY) if valid_prefix_length?(mask) mask else NETMASK_TO_PREFIX_LENGTH[family][mask] || (raise ArgumentError, "Can't convert #{mask} to prefix length") end end |
.ipaddr_to_prefix_length(mask, family = self::FAMILY) ⇒ Integer
Convert a netmask represented as an IPAddr to a prefix length.
Returns nil if the IPAddr can’t be converted.
113 114 115 |
# File 'lib/better_ipaddr/classes.rb', line 113 def self.ipaddr_to_prefix_length(mask, family = self::FAMILY) NETMASK_TO_PREFIX_LENGTH[family][mask.to_i] end |
.object_to_prefix_length(mask, family = self::FAMILY) ⇒ Integer
Convert an object to a prefix length.
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/better_ipaddr/classes.rb', line 75 def self.object_to_prefix_length(mask, family = self::FAMILY) case mask when Integer integer_to_prefix_length(mask, family) when String string_to_prefix_length(mask, family) when IPAddr ipaddr_to_prefix_length(mask, family) else raise ArgumentError, "Can't convert #{mask.class} to prefix length" end end |
.parse(address) ⇒ IPAddr::V4, ...
Convert the given string to an IPAddr subclass.
146 147 148 |
# File 'lib/better_ipaddr/classes.rb', line 146 def self.parse(address) specialize IPAddr.new(address) end |
.specialize(address) ⇒ IPAddr::V4, ...
Return the given address as an instance of a class specific to its address family.
155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/better_ipaddr/classes.rb', line 155 def self.specialize(address) return address unless address.class == IPAddr case address.family when Family::IPV4 IPAddr::V4[address.to_i, address.instance_variable_get(:@mask_addr)] when Family::IPV6 IPAddr::V6[address.to_i, address.instance_variable_get(:@mask_addr)] when Family::EUI48 IPAddr::MAC[address.to_i, address.instance_variable_get(:@mask_addr)] end end |
.specialize_constants(family) ⇒ Object
167 168 169 170 171 172 173 174 |
# File 'lib/better_ipaddr/classes.rb', line 167 def self.specialize_constants(family) const_set(:FAMILY, family) const_set(:BIT_LENGTH, FAMILY_TO_BIT_LENGTH.fetch(self::FAMILY)) const_set(:NETMASK_TO_PREFIX_LENGTH, NETMASK_TO_PREFIX_LENGTH.fetch(self::FAMILY)) const_set(:PREFIX_LENGTH_TO_NETMASK, PREFIX_LENGTH_TO_NETMASK.fetch(self::FAMILY)) end |
.string_to_prefix_length(mask, family = self::FAMILY) ⇒ Integer
Convert a string to a prefix length.
Accepts the decimal representations of integers as well as netmasks in dotted quad notation.
Returns nil if the string can’t be converted.
126 127 128 129 130 131 132 |
# File 'lib/better_ipaddr/classes.rb', line 126 def self.string_to_prefix_length(mask, family = self::FAMILY) if mask =~ /^\d+$/ integer_to_prefix_length(mask.to_i, family) else NETMASK_TO_PREFIX_LENGTH[family][new(mask).to_i] end end |
.valid_prefix_length?(prefix_length, family: self::FAMILY) ⇒ Boolean
Return true if the given number is a valid prefix length, false otherwise.
138 139 140 |
# File 'lib/better_ipaddr/classes.rb', line 138 def self.valid_prefix_length?(prefix_length, family: self::FAMILY) 0 <= prefix_length && prefix_length <= FAMILY_TO_BIT_LENGTH[family] end |
Instance Method Details
#address_family_bit_length ⇒ Object
176 177 178 |
# File 'lib/better_ipaddr/classes.rb', line 176 def address_family_bit_length self.class::BIT_LENGTH end |
#inherited(cls) ⇒ Object
10 11 12 |
# File 'lib/better_ipaddr/classes.rb', line 10 def inherited(cls) cls.extend BetterIpaddr::ClassMethods end |
#network? ⇒ Boolean
180 181 182 |
# File 'lib/better_ipaddr/classes.rb', line 180 def network? prefix_length < self.class::BIT_LENGTH end |
#prefix_length ⇒ Object
184 185 186 |
# File 'lib/better_ipaddr/classes.rb', line 184 def prefix_length self.class::NETMASK_TO_PREFIX_LENGTH[mask_addr] end |