tuid 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +17 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +64 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/tuid.rb +108 -0
- data/lib/tuid/version.rb +3 -0
- data/tuid.gemspec +25 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4f43c78e9fec58f4a0b33b62750bd2396e4d147c
|
4
|
+
data.tar.gz: 1c3de5f090b9126f04f62580201003327c453d7d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ec762736d6404dd98e10ece8a37ab3770077c708a57e4f3dae668d81d0b4d1d644b92b6e2f18e6555fdb8c988533d143395b3be02f795bc0507022151c58c3ba
|
7
|
+
data.tar.gz: 8e7d1dcdeb10e9bba783f7a0502be43c33f1f277a675274976eaf4cabaeef2a1eebf38c4250066ea6fadc0e8d5a68348e9d0d45488d5a9ee5a455216a22a81ec
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 2.0
|
4
|
+
- 2.1
|
5
|
+
- 2.2
|
6
|
+
- rbx-2
|
7
|
+
- jruby
|
8
|
+
- ruby-head
|
9
|
+
- jruby-head
|
10
|
+
before_install: gem install bundler -v 1.10.6
|
11
|
+
matrix:
|
12
|
+
allow_failures:
|
13
|
+
- rvm: ruby-head
|
14
|
+
- rvm: jruby-head
|
15
|
+
addons:
|
16
|
+
code_climate:
|
17
|
+
repo_token: 3967a141e0a4910cba7c8b82d2dab094939483528ef42a9ab57ea4fdc50f4716
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Seba Gamboa
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# Time based Unique IDentifiers
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/sagmor/tuid.svg?branch=master)](https://travis-ci.org/sagmor/tuid)
|
4
|
+
[![Test Coverage](https://codeclimate.com/github/sagmor/tuid/badges/coverage.svg)](https://codeclimate.com/github/sagmor/tuid/coverage)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/sagmor/tuid/badges/gpa.svg)](https://codeclimate.com/github/sagmor/tuid)
|
6
|
+
[![Inline docs](http://inch-ci.org/github/sagmor/tuid.svg?branch=master)](http://inch-ci.org/github/sagmor/tuid)
|
7
|
+
|
8
|
+
TUIDs are values compatible with UUIDs
|
9
|
+
|
10
|
+
They share the same base formati, so can be used instead of them.
|
11
|
+
The main property of TUIDs is that they are prefixed by the time they
|
12
|
+
were generated. Which allow them to be sorted and behave better on database
|
13
|
+
indexes.
|
14
|
+
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
Add this line to your application's Gemfile:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
gem 'tuid'
|
22
|
+
```
|
23
|
+
|
24
|
+
And then execute:
|
25
|
+
|
26
|
+
$ bundle
|
27
|
+
|
28
|
+
Or install it yourself as:
|
29
|
+
|
30
|
+
$ gem install tuid
|
31
|
+
|
32
|
+
## Usage
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
# Generate a random TUID
|
36
|
+
tuid = TUID.new
|
37
|
+
#=> #<TUID 56342da3-7cc0-280d-f0b8-507634551518(2015-10-30 23:55:31 -0300)>
|
38
|
+
|
39
|
+
# Get its generation time
|
40
|
+
tuid.time
|
41
|
+
#=> 2015-10-30 23:55:31 -0300
|
42
|
+
|
43
|
+
# Turn it into a String
|
44
|
+
tuid.to_s
|
45
|
+
#=> "56342da3-7cc0-280d-f0b8-507634551518"
|
46
|
+
|
47
|
+
# Generate a TUID from a UUID formated string
|
48
|
+
tuid2 = TUID.new("56342da3-7cc0-280d-f0b8-507634551518")
|
49
|
+
#=> #<TUID 56342da3-7cc0-280d-f0b8-507634551518(2015-10-30 23:55:31 -0300)>
|
50
|
+
|
51
|
+
# And they can be compared
|
52
|
+
tuid == tuid2
|
53
|
+
#=> true
|
54
|
+
```
|
55
|
+
|
56
|
+
## Contributing
|
57
|
+
|
58
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/sagmor/tuid. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
59
|
+
|
60
|
+
|
61
|
+
## License
|
62
|
+
|
63
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
64
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "tuid"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/tuid.rb
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
require "tuid/version"
|
2
|
+
require "securerandom"
|
3
|
+
|
4
|
+
# TUID value.
|
5
|
+
#
|
6
|
+
# TUIDs are values compatible with UUID
|
7
|
+
#
|
8
|
+
# They share the same base format so can be used instead of them.
|
9
|
+
# The main property of them is that they are prefixed by the time they
|
10
|
+
# were generated. This allow them to be sorted and behave better on database
|
11
|
+
# indexes.
|
12
|
+
#
|
13
|
+
class TUID
|
14
|
+
|
15
|
+
# TUID binary representation
|
16
|
+
attr_reader :bytes
|
17
|
+
include Comparable
|
18
|
+
|
19
|
+
# Initialize a new TUID
|
20
|
+
#
|
21
|
+
# @overload initialize
|
22
|
+
# @return Random TUID based on the current time
|
23
|
+
#
|
24
|
+
# @overload initialize(data)
|
25
|
+
# @param data [String] TUID string representation.
|
26
|
+
# @return TUID object equivalent to the passed string
|
27
|
+
#
|
28
|
+
# @overload initialize(time)
|
29
|
+
# @param time [Time] Reference time.
|
30
|
+
# @return Random TUID using the given generation time.
|
31
|
+
def initialize(data=nil)
|
32
|
+
@bytes = case data
|
33
|
+
when TUID
|
34
|
+
data.bytes
|
35
|
+
when String
|
36
|
+
bytes_from_string(data)
|
37
|
+
when Time, NilClass
|
38
|
+
random_bytes_with_time(data || Time.now)
|
39
|
+
else
|
40
|
+
raise type_error(data, "invalid type")
|
41
|
+
end.freeze
|
42
|
+
end
|
43
|
+
|
44
|
+
# Compares two TUIDs.
|
45
|
+
#
|
46
|
+
# @return [-1,0,1] the compare result.
|
47
|
+
def <=>(other)
|
48
|
+
self.bytes <=> other.bytes
|
49
|
+
end
|
50
|
+
|
51
|
+
# Console friendly representation
|
52
|
+
def inspect
|
53
|
+
"#<#{self.class.name} #{to_s} (#{time.utc})>"
|
54
|
+
end
|
55
|
+
|
56
|
+
# Get the TUID in UUID string format.
|
57
|
+
#
|
58
|
+
# @return [String] formatted as XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
|
59
|
+
def to_s
|
60
|
+
"%08x-%04x-%04x-%04x-%04x%08x" % bytes.unpack("NnnnnN")
|
61
|
+
end
|
62
|
+
alias :to_str :to_s
|
63
|
+
|
64
|
+
# Extract the time at which the TUID was generated.
|
65
|
+
#
|
66
|
+
# @return [Time] the generation time.
|
67
|
+
def time
|
68
|
+
Time.at(*bytes.unpack("N"))
|
69
|
+
end
|
70
|
+
|
71
|
+
# Compare two TUID objects
|
72
|
+
def eql?(other)
|
73
|
+
other.instance_of?(self.class) && self == other
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
# Parse bytes from String
|
79
|
+
def bytes_from_string(string)
|
80
|
+
case string.length
|
81
|
+
when 16
|
82
|
+
string.frozen? ? string : string.dup
|
83
|
+
when 36
|
84
|
+
elements = string.split("-")
|
85
|
+
raise type_error(string, "malformed UUID representation") if elements.size != 5
|
86
|
+
[elements.join].pack('H32')
|
87
|
+
else
|
88
|
+
raise type_error(string, "invalid bytecount")
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
# Generate bytes string with the given time
|
93
|
+
def random_bytes_with_time(time)
|
94
|
+
bytes = [
|
95
|
+
time.to_i
|
96
|
+
] + SecureRandom.random_bytes(12).unpack("nnnnN")
|
97
|
+
bytes[2] = (bytes[2] & 0x0fff) | 0x4000
|
98
|
+
bytes[3] = (bytes[3] & 0x3fff) | 0x8000
|
99
|
+
|
100
|
+
bytes.pack("NnnnnN")
|
101
|
+
end
|
102
|
+
|
103
|
+
# Create a formatted type error.
|
104
|
+
def type_error(source,error)
|
105
|
+
TypeError.new("Expected #{source.inspect} to cast to a #{self.class} (#{error})")
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
data/lib/tuid/version.rb
ADDED
data/tuid.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'tuid/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "tuid"
|
8
|
+
spec.version = TUID::VERSION
|
9
|
+
spec.authors = ["Seba Gamboa"]
|
10
|
+
spec.email = ["me@sagmor.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Time based Unique IDentifierss}
|
13
|
+
spec.description = %q{UUID compatible objects prefixed by time}
|
14
|
+
spec.homepage = "https://github.com/sagmor/tuid"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tuid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Seba Gamboa
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: UUID compatible objects prefixed by time
|
56
|
+
email:
|
57
|
+
- me@sagmor.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- lib/tuid.rb
|
73
|
+
- lib/tuid/version.rb
|
74
|
+
- tuid.gemspec
|
75
|
+
homepage: https://github.com/sagmor/tuid
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.4.5.1
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: Time based Unique IDentifierss
|
99
|
+
test_files: []
|