uuid4 1.2.1 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 40dd0f2af493ca5b0bbe31e4ff0b78b4eef6b173
4
- data.tar.gz: a85b9447fafed6c5983b2eddcc85d8e2da91406a
3
+ metadata.gz: ba42502b36d87ad581c49a75fecd0986f5e7300d
4
+ data.tar.gz: c1aa10e23bdeac041883dc68ffc42dfeaf6189b0
5
5
  SHA512:
6
- metadata.gz: 9f9baf2e30e8d2473cab8599b1cb845541a6e5dcc90b55426b366afb7e4c1eec96b97bb5a31c32b686095e401976828c736c12adca9c12fb9989fa125d92dcb8
7
- data.tar.gz: 5e5e2bb9fb811b840cabca2c740e6fd43b714e0af33b1111c06493f2d07150dee87e3f8c8679ced1961d8a13d0f44ea222d4d088c9f1a608c2712f187da790d8
6
+ metadata.gz: 894d8c3b4820762cdb81cb6c58ef0ae0a3a22333351d3e16da416a4d64b00b39a92ea74f586b311559d1c2ce46cd9a77a80d42bbf759b082e01b8119dc443dc0
7
+ data.tar.gz: 45a20cbd99f390ce85d30ee49d2eba22d773a84f49f67f72923a9f67a359ff5c3dde1a32e25af54cedccbe7916c671ee697b44bb88647a7b1607d72f6f04b59f
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.3.0
4
+
5
+ * Add `UUID#hash` and `UUID#eql?` for using UUIDs e.g. as hash keys
6
+ * Add `UUID.try_convert` returning a UUID or `nil`.
7
+ * Add `UUID.valid?` to validate objects as UUIDs (#1)
8
+
3
9
  ## 1.2.1
4
10
 
5
11
  * Improve `#as_json` compatibility by swallowing arbitrary arguments passed by encoders.
@@ -60,6 +60,14 @@ class UUID4
60
60
 
61
61
  alias_method :to_i, :to_int
62
62
 
63
+ def hash
64
+ @value.hash
65
+ end
66
+
67
+ def eql?(object)
68
+ object.is_a?(::UUID4) && object.hash === hash
69
+ end
70
+
63
71
  def inspect
64
72
  "<UUID4:#{to_s}>"
65
73
  end
@@ -79,33 +87,46 @@ class UUID4
79
87
  attr_reader :value
80
88
 
81
89
  class << self
90
+ alias _new new
91
+ private :_new
92
+
82
93
  def new(value = nil)
83
94
  if value.nil?
84
95
  super(SecureRandom.uuid.tr('-', '').hex)
85
- elsif value.is_a?(UUID4)
96
+ elsif (value = try_convert(value))
86
97
  value
87
- elsif value.respond_to?(:to_uuid4)
88
- value.to_uuid4
89
- elsif value.respond_to?(:to_int) && valid_int?(value = value.to_int)
90
- super(value)
91
- elsif (ret = _parse(value))
92
- super(ret)
93
98
  else
94
99
  raise TypeError.new "Invalid UUID: #{value.inspect}"
95
100
  end
96
101
  end
97
102
 
98
- def _parse(value)
99
- if value.respond_to?(:to_int) && valid_int?(value = value.to_int)
100
- return value
103
+ def try_convert(value)
104
+ if value.nil? || value.is_a?(::UUID4)
105
+ value
106
+ elsif value.respond_to?(:to_uuid4)
107
+ value.to_uuid4
108
+ elsif (value = _parse(value))
109
+ _new value
101
110
  end
111
+ end
102
112
 
103
- FORMATTERS.each do |formatter|
104
- val = formatter.decode(value) if formatter.respond_to?(:decode)
105
- return val if val
113
+ def valid?(value)
114
+ if value.is_a?(::UUID4)
115
+ true
116
+ else
117
+ !try_convert(value).nil?
106
118
  end
119
+ end
107
120
 
108
- nil
121
+ def _parse(value)
122
+ if value.respond_to?(:to_int) && valid_int?(value = value.to_int)
123
+ value
124
+ else
125
+ # Return the result of the first formatter that can decode this value
126
+ FORMATTERS.lazy.map { |formatter|
127
+ formatter.decode(value) if formatter.respond_to?(:decode)
128
+ }.find(&:itself)
129
+ end
109
130
  end
110
131
 
111
132
  def valid_int?(int)
@@ -1,8 +1,8 @@
1
1
  class UUID4
2
2
  module VERSION
3
3
  MAJOR = 1
4
- MINOR = 2
5
- PATCH = 1
4
+ MINOR = 3
5
+ PATCH = 0
6
6
  STAGE = nil
7
7
  STRING = [MAJOR, MINOR, PATCH, STAGE].reject(&:nil?).join('.')
8
8
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uuid4
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Graichen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-15 00:00:00.000000000 Z
11
+ date: 2016-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: base62-rb
@@ -110,9 +110,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
110
  version: '0'
111
111
  requirements: []
112
112
  rubyforge_project:
113
- rubygems_version: 2.5.1
113
+ rubygems_version: 2.6.6
114
114
  signing_key:
115
115
  specification_version: 4
116
116
  summary: A UUIDv4 support library
117
117
  test_files: []
118
- has_rdoc: