better_ipaddr 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5cd953909cfae8bb5422a297acdd4592ec96957c262072587add707a78ca9dd3
4
- data.tar.gz: 32ff0ee9fb1aa6fc91a1fd2db3802add0d9b10af29c97ecbb066b3805ca5dcf3
3
+ metadata.gz: c932b6f935ba9e71a4bb4471ecb0bfcfebbf77f316799e75a4617d3a39cdf7b0
4
+ data.tar.gz: 677d6e46f7129f07dbcb5a57f78845eba60ace671400c477609e6fd7fbe43e7d
5
5
  SHA512:
6
- metadata.gz: 4ac0277a9876f20973dd0418547093ec41342ecbd1186c2da235c2463a0cd70f9cf39c623242ebad8cb2aa2156528a1d9735317c82a04310385ae93624beec75
7
- data.tar.gz: 2d8f9e58dc03ea59d6af81995345e37395fd259bb1a615ef6ad9cd0d6e19a5b9cfb5f1ef3cc2891574861e99ae9280ee72288ed5d29308483b4d909041f7dba1
6
+ metadata.gz: f98ca91e742c0081664a2126fea68d69c194e37a1ab30ceee3d5774cfd99d7d8891cd79300bce192ce9efecc0277406c2a4a1bafbb65fe49ee78cade918cdb94
7
+ data.tar.gz: c7eade85d3969499eb0c0a50b5240b3b94a1828947de936082e88fb8269e1b3e820ffa93bda60769c7212188b5c08cd2e33d46aca4fe4e6c5a82b43a51f6e656
@@ -33,6 +33,8 @@ class IPAddr
33
33
  end
34
34
  end
35
35
 
36
+ # rubocop: disable CyclomaticComplexity
37
+
36
38
  # Create an IPAddr from the given object, guessing the type of address given
37
39
  # based on its type and content.
38
40
  #
@@ -50,6 +52,10 @@ class IPAddr
50
52
  # @param classful [Boolean] see Base.from_string
51
53
  # @return [IPAddr, Nil]
52
54
  def self.from(address, exception: false, classful: false)
55
+ if class_converter?(address)
56
+ return class_convert(address, classful: classful, exception: exception)
57
+ end
58
+
53
59
  case address
54
60
  when IPAddr
55
61
  specialize address
@@ -59,7 +65,62 @@ class IPAddr
59
65
  V6[address]
60
66
  end || (
61
67
  if exception
62
- raise TypeError, "can't convert #{address.inspect} to #{self}"
68
+ (raise TypeError, "can't convert #{address.inspect} to #{self}")
69
+ end
70
+ )
71
+ end
72
+
73
+ # rubocop: enable CyclomaticComplexity
74
+
75
+ # A Hash of classes and class names which can be converted to IPAddr::Base
76
+ # subclasses, but which are not necessarily loaded at the same time as this
77
+ # file.
78
+ #
79
+ # Conversion callables for custom classes can be registered here, e.g., with
80
+ # custom class Foo:
81
+ #
82
+ # IPAddr::Base.class_converters[Foo] = proc { |foo, _| foo.to_ipaddr }
83
+ #
84
+ # The arguments passed to the callable will be the same as the parameters of
85
+ # Kernel#IPAddr.
86
+ #
87
+ # @return [Hash{String, Class => Proc, Method}]
88
+ def self.class_converters
89
+ @class_converters ||= {
90
+ 'Resolv::IPv4' => V4.method(:from_string_representable),
91
+ 'Resolv::IPv6' => V6.method(:from_string_representable)
92
+ }
93
+ end
94
+
95
+ # Return the class_converter for the given object, if one exists.
96
+ #
97
+ # Checks by both class identity and class name so that every class with a
98
+ # converter doesn't need to be loaded for this file to load.
99
+ #
100
+ # @return [Proc, Method, Nil]
101
+ def self.class_converter(address)
102
+ class_converters[address.class] ||= class_converters[address.class.name]
103
+ class_converters[address.class]
104
+ end
105
+
106
+ def self.class_converter?(address)
107
+ class_converters.key?(address.class) ||
108
+ class_converters.key?(address.class.name)
109
+ end
110
+
111
+ # Convert the given object using its class_converter, if one exists.
112
+ #
113
+ # @return [IPAddr, Nil]
114
+ def self.class_convert(address, mask = nil, classful: nil, exception: false)
115
+ converter = class_converter(address)
116
+ converter && converter.call(
117
+ address,
118
+ mask,
119
+ classful: classful,
120
+ exception: exception
121
+ ) || (
122
+ if exception
123
+ (raise TypeError, "can't convert #{address.inspect} to #{self}")
63
124
  end
64
125
  )
65
126
  end
@@ -132,6 +193,29 @@ class IPAddr
132
193
  end
133
194
  end
134
195
 
196
+ # Create an IPAddr from an object that can be converted to a String via
197
+ # #to_s.
198
+ #
199
+ #
200
+ # @param address [#to_s]
201
+ # @param mask [Integer, String] a netmask or prefix length
202
+ # @param family [Integer, Nil]
203
+ # @param classful [Boolean] controls the conversion of IPv4 addresses
204
+ # without a prefix length in CIDR notation. When false, these are assumed
205
+ # to be host networks (/32). When true, these are assumed to be classful
206
+ # (rfc791) networks, with an implicit prefix length. Has no effect on IPv6
207
+ # addresses.
208
+ # @return [IPAddr]
209
+ def self.from_string_representable(
210
+ address,
211
+ mask = nil,
212
+ family: self::FAMILY,
213
+ classful: false,
214
+ exception: false
215
+ )
216
+ from_string(address.to_s, mask, family: family, classful: classful)
217
+ end
218
+
135
219
  # Convert an object to a prefix length.
136
220
  #
137
221
  # @param mask [Integer, String]
@@ -1,3 +1,3 @@
1
1
  module BetterIpaddr
2
- VERSION = "0.6.0".freeze
2
+ VERSION = "0.7.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: better_ipaddr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Miller
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-14 00:00:00.000000000 Z
11
+ date: 2018-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler