ruuid 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 12955371c15837c3d5bd2ab62753e04852114933
4
+ data.tar.gz: 154a406dce57289289633ca8107e24c372bde09f
5
+ SHA512:
6
+ metadata.gz: 520286586d5dc145c9f5872ec9f24c07e16920350175f8541bb93757ea262aab4a91a62f8f06edc3effccd06bc76da4cb6f38c44a714ef391293e628d0b731ad
7
+ data.tar.gz: f3a64c3fc5840bb2b1bf89d8ebaa76a8f0432829d64ff4e2c713150233519336b84803842ca4b32f887605ebaaf2b7e12c83114204fddc6b01ed5506b0b27e76
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ sudo: false
3
+ rvm:
4
+ - 2.2.2
5
+ - ruby-head
6
+ matrix:
7
+ allow_failures:
8
+ - rvm: ruby-head
9
+ fast_finish: true
data/.yardopts ADDED
@@ -0,0 +1,7 @@
1
+ --embed-mixin RUUID::Generator
2
+ --no-private
3
+ --protected
4
+ -
5
+ CODE_OF_CONDUCT.md
6
+ LICENSE.txt
7
+ README.md
@@ -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, 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
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruuid.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 HashRabbit, Inc.
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
+ # RUUID
2
+
3
+ [![Build Status](https://img.shields.io/travis/hashrabbit/ruuid.svg)][travis]
4
+
5
+ RUUID (Ruby UUID) parses, generates, and represents [RFC 4122][RFC 4122]
6
+ universally unique identifiers. Currently, only v4 UUIDs can be generated by
7
+ this library.
8
+
9
+ [travis]: https://travis-ci.org/hashrabbit/ruuid
10
+ [RFC 4122]: https://www.ietf.org/rfc/rfc4122.txt
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'ruuid'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install ruuid
27
+
28
+ ## Example Usage
29
+
30
+ ```ruby
31
+ require 'ruuid'
32
+
33
+ uuid = RUUID.generate
34
+ # => <RUUID::UUID:0x70168845476220 data=7cc9f80c-c2e2-4d03-ad35-1e3112dc77de>
35
+
36
+ uuid.version
37
+ # => 4
38
+
39
+ uuid.to_s
40
+ # => "7cc9f80c-c2e2-4d03-ad35-1e3112dc77de"
41
+ uuid.to_s(:compact)
42
+ # => "7cc9f80cc2e24d03ad351e3112dc77de"
43
+ ```
44
+
45
+ See [API documentation](http://www.rubydoc.info/docs/ruuid) for
46
+ additional information.
47
+
48
+ ## Development
49
+
50
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
51
+ `bin/console` for an interactive prompt that will allow you to experiment.
52
+
53
+ To install this gem onto your local machine, run `bundle exec rake install`. To
54
+ release a new version, update the version number in `version.rb`, and then run
55
+ `bundle exec rake release` to create a git tag for the version, push git commits
56
+ and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it ( https://github.com/hashrabbit/ruuid/fork )
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'yard'
4
+ YARD::Rake::YardocTask.new(:doc)
5
+
6
+ require 'rspec/core/rake_task'
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'pry'
5
+ require 'ruuid'
6
+
7
+ Pry.start
data/bin/rake ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rake' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rake', 'rake')
data/bin/rspec ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rspec' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rspec-core', 'rspec')
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/bin/yard ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'yard' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('yard', 'yard')
data/bin/yardoc ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'yardoc' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('yard', 'yardoc')
data/lib/ruuid.rb ADDED
@@ -0,0 +1,23 @@
1
+ require 'ruuid/version'
2
+
3
+ require 'ruuid/formatter'
4
+ require 'ruuid/generator'
5
+ require 'ruuid/generators/v4'
6
+ require 'ruuid/parser'
7
+ require 'ruuid/uuid'
8
+
9
+ module RUUID
10
+ extend self
11
+
12
+ class InvalidUUIDError < StandardError; end
13
+
14
+ attr_accessor :default_generator
15
+ self.default_generator = Generators::V4
16
+
17
+ attr_accessor :default_format
18
+ self.default_format = :canonical
19
+
20
+ def generate
21
+ default_generator.generate
22
+ end
23
+ end
@@ -0,0 +1,19 @@
1
+ module RUUID
2
+ # @private
3
+ module Formatter
4
+ extend self
5
+
6
+ HEX_FORMAT = 'H8 H4 H4 H4 H12'.freeze
7
+
8
+ def format(data, format = RUUID.default_format)
9
+ parts = data.unpack(HEX_FORMAT)
10
+
11
+ case format
12
+ when :canonical then parts.join('-')
13
+ when :compact then parts.join
14
+ else
15
+ raise ArgumentError, "`#{format.inspect}' format not supported. Supported formats are: :canonical and :compact"
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,55 @@
1
+ require 'singleton'
2
+
3
+ module RUUID
4
+ # Generator is responsible for generating new UUIDs. Implementors can
5
+ # include and override methods to provide new UUID variants.
6
+ module Generator
7
+ def self.included(base)
8
+ super
9
+ base.extend ClassMethods
10
+ base.include Singleton
11
+ end
12
+
13
+ module ClassMethods
14
+ def version(value)
15
+ instance.version = value
16
+ end
17
+
18
+ def source(value)
19
+ instance.source = value
20
+ end
21
+
22
+ def generate
23
+ instance.generate
24
+ end
25
+ end # ClassMethods
26
+
27
+ attr_accessor :version, :source
28
+
29
+ def initialize
30
+ @version = 0
31
+ end
32
+
33
+ def generate
34
+ UUID.from_data(masked_bytes)
35
+ end
36
+
37
+ private
38
+
39
+ def raw_bytes
40
+ unless source.respond_to?(:call)
41
+ raise NotImplementedError, "#{self.class} does not specify a data source"
42
+ end
43
+ source.call.bytes
44
+ end
45
+
46
+ def masked_bytes
47
+ raw_bytes.tap { |bytes|
48
+ # Mask version
49
+ bytes[6] = (bytes[6] & 0x0f) | (version << 4)
50
+ # Mask variant
51
+ bytes[8] = (bytes[8] & 0x3f) | 0x80
52
+ }.collect(&:chr).join
53
+ end
54
+ end # Generator
55
+ end # RUUID
@@ -0,0 +1,12 @@
1
+ require 'securerandom'
2
+
3
+ module RUUID
4
+ module Generators
5
+ class V4
6
+ include Generator
7
+
8
+ version 4
9
+ source ->{ SecureRandom.random_bytes(16) }
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ module RUUID
2
+ # @private
3
+ module Parser
4
+ extend self
5
+
6
+ BLACKLIST = /[^a-f0-9]/i
7
+
8
+ def parse(string)
9
+ string = sanitize(string)
10
+ [string].pack('H*')
11
+ end
12
+
13
+ private
14
+
15
+ def sanitize(string)
16
+ string.gsub(BLACKLIST, '')
17
+ end
18
+ end # Parser
19
+ end # RUUID
data/lib/ruuid/uuid.rb ADDED
@@ -0,0 +1,84 @@
1
+ module RUUID
2
+ class UUID
3
+ include Comparable
4
+
5
+ # @private
6
+ def self.from_data(data)
7
+ uuid = allocate
8
+ uuid.instance_variable_set :@data, data.dup.freeze
9
+ uuid.validate
10
+ uuid
11
+ end
12
+
13
+ def self.parse(string)
14
+ from_data(Parser.parse(string))
15
+ end
16
+
17
+ # @private
18
+ def initialize
19
+ raise RuntimeError, 'RUUID::UUID.new is not allowed. Use RUUID.generate instead.'
20
+ end
21
+
22
+ # @private
23
+ attr_reader :data
24
+
25
+ def to_s(format = RUUID.default_format)
26
+ Formatter.format(data, format)
27
+ end
28
+ alias_method :to_str, :to_s
29
+
30
+ def to_json(*args)
31
+ to_s(*args).to_json
32
+ end
33
+
34
+ def inspect
35
+ "<#{self.class}:0x#{object_id} data=#{to_s}>"
36
+ end
37
+
38
+ # @private
39
+ def marshal_dump
40
+ data
41
+ end
42
+
43
+ # @private
44
+ def marshal_load(data)
45
+ @data = data.dup.freeze
46
+ validate
47
+ end
48
+
49
+ def hash
50
+ data.hash
51
+ end
52
+
53
+ def ==(other)
54
+ return super unless other.respond_to?(:data)
55
+ data == other.data
56
+ end
57
+ alias_method :eql?, :==
58
+
59
+ def ===(other)
60
+ return super unless other.respond_to?(:to_str)
61
+ to_str === other.to_str
62
+ end
63
+
64
+ def <=>(other)
65
+ return super unless other.respond_to?(:data)
66
+ data <=> other.data
67
+ end
68
+
69
+ def version
70
+ data.bytes[6] >> 4
71
+ end
72
+
73
+ # @private
74
+ def validate
75
+ unless data.length == 16
76
+ raise InvalidUUIDError, 'Invalid UUID length'
77
+ end
78
+
79
+ unless data.bytes[8] >> 6 == 2
80
+ raise InvalidUUIDError, 'Invalid UUID variant'
81
+ end
82
+ end
83
+ end # UUID
84
+ end # RUUID
@@ -0,0 +1,3 @@
1
+ module RUUID
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/ruuid.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 'ruuid/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'ruuid'
8
+ spec.version = RUUID::VERSION
9
+ spec.authors = ['Gabe Evans']
10
+ spec.email = ['gabe@ga.be']
11
+
12
+ spec.summary = %q{RUUID (Ruby UUID) parses, generates, and represents RFC 4122 universally unique identifiers.}
13
+ spec.homepage = 'https://github.com/hashrabbit/ruuid'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.require_paths = %w[lib]
18
+
19
+ spec.add_development_dependency 'bundler', '~> 1.9'
20
+ spec.add_development_dependency 'pry', '~> 0.10.1'
21
+ spec.add_development_dependency 'rake', '~> 10.0'
22
+ spec.add_development_dependency 'rspec', '~> 3.2'
23
+ spec.add_development_dependency 'simplecov', '~> 0.10.0'
24
+ spec.add_development_dependency 'yard', '~> 0.8.7'
25
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruuid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gabe Evans
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-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.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.10.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.10.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.10.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.10.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: yard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.8.7
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.8.7
97
+ description:
98
+ email:
99
+ - gabe@ga.be
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - ".yardopts"
108
+ - CODE_OF_CONDUCT.md
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/rake
115
+ - bin/rspec
116
+ - bin/setup
117
+ - bin/yard
118
+ - bin/yardoc
119
+ - lib/ruuid.rb
120
+ - lib/ruuid/formatter.rb
121
+ - lib/ruuid/generator.rb
122
+ - lib/ruuid/generators/v4.rb
123
+ - lib/ruuid/parser.rb
124
+ - lib/ruuid/uuid.rb
125
+ - lib/ruuid/version.rb
126
+ - ruuid.gemspec
127
+ homepage: https://github.com/hashrabbit/ruuid
128
+ licenses:
129
+ - MIT
130
+ metadata: {}
131
+ post_install_message:
132
+ rdoc_options: []
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ requirements: []
146
+ rubyforge_project:
147
+ rubygems_version: 2.4.5
148
+ signing_key:
149
+ specification_version: 4
150
+ summary: RUUID (Ruby UUID) parses, generates, and represents RFC 4122 universally
151
+ unique identifiers.
152
+ test_files: []
153
+ has_rdoc: