Last active
December 26, 2015 18:09
-
-
Save gmanley/7192268 to your computer and use it in GitHub Desktop.
UUID helper module.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'securerandom' | |
module UUID | |
UUID_PATTERN = /(\w{8})-?(\w{4})-?(\w{4})-?(\w{4})-?(\w{12})/ | |
VALID_UUID_PATTERN = /\A#{UUID_PATTERN}\Z/ | |
def self.included(base) | |
base.send(:extend, ClassMethods) | |
end | |
module ClassMethods | |
# This aliases better named singleton methods on UUID. It doesn't create | |
# aliases in any of the classes that include UUID. This is an important | |
# distinction as we don't want to overwrite certain methods eg. '#valid?'. | |
def self.extended(base) | |
super | |
# If you have active support then this can just be `base == parent` | |
# Of course it could also just be `base == Uuid` but who likes to hardcode D: | |
if base == name.match(/::[^:]+\Z/).pre_match | |
base.singleton_class.class_eval do | |
alias_method :valid?, :valid_uuid? | |
alias_method :generate_random, :generate_random_uuid | |
alias_method :unformat, :unformat_uuid | |
alias_method :format, :format_uuid | |
end | |
end | |
end | |
def valid_uuid?(value) | |
!!(value =~ VALID_UUID_PATTERN) | |
end | |
def contains_valid_uuid?(value) | |
!!(value =~ UUID_PATTERN) | |
end | |
def generate_random_uuid | |
SecureRandom.uuid | |
end | |
def unformat_uuid(value) | |
value.delete('-') if valid_uuid?(value) | |
end | |
def format_uuid(value) | |
value.scan(VALID_UUID_PATTERN).flatten.join('-') if valid_uuid?(value) | |
end | |
end | |
extend ClassMethods | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
require_relative 'uuid' | |
describe UUID do | |
let(:klass) { Class.new.send(:include, subject) } | |
let(:valid_formatted_uuid) { 'd925fc22-5410-43a1-8980-6785b120e1f6' } | |
let(:valid_unformatted_uuid) { 'd925fc22541043a189806785b120e1f6' } | |
let(:contains_valid_uuid) { "/foo/#{valid_formatted_uuid}/bar" } | |
describe '#valid_uuid?' do | |
it 'should return true when given a valid formatted uuid' do | |
expect(klass.valid_uuid?(valid_formatted_uuid)).to be_true | |
end | |
it 'should return true when given a valid unformatted uuid' do | |
expect(klass.valid_uuid?(valid_unformatted_uuid)).to be_true | |
end | |
it 'should return false when given an invalid uuid' do | |
expect(klass.valid_uuid?('foo bar')).to be_false | |
end | |
it 'should return false when given a string containing a valid uuid' do | |
expect(klass.valid_uuid?(contains_valid_uuid)).to be_false | |
end | |
end | |
describe '#contains_valid_uuid?' do | |
it 'should return true when given a string containing a valid uuid' do | |
expect(klass.contains_valid_uuid?(contains_valid_uuid)).to be_true | |
end | |
end | |
describe '#generate_random_uuid' do | |
# This is the only thing being tested as it is the only part being relied | |
# on that isn't already tested by the external library. | |
it 'should generate a formatted uuid' do | |
expect(klass.generate_random_uuid).to include('-') | |
end | |
end | |
describe '#unformat_uuid' do | |
it 'should return the uuid unformatted when given a formated uuid' do | |
expect(klass.unformat_uuid(valid_formatted_uuid)).to eq(valid_unformatted_uuid) | |
end | |
it 'should return the uuid unmodified when given an unformated uuid' do | |
expect(klass.unformat_uuid(valid_unformatted_uuid)).to eq(valid_unformatted_uuid) | |
end | |
it 'should return nil when given an invalid uuid' do | |
expect(klass.unformat_uuid('foo bar')).to be_nil | |
end | |
end | |
describe '#format_uuid' do | |
it 'should return the uuid formatted when given an unformated uuid' do | |
expect(klass.format_uuid(valid_unformatted_uuid)).to eq(valid_formatted_uuid) | |
end | |
it 'should return the uuid unmodified when given a formated uuid' do | |
expect(klass.format_uuid(valid_formatted_uuid)).to eq(valid_formatted_uuid) | |
end | |
it 'should return nil when given an invalid uuid' do | |
expect(klass.format_uuid('foo bar')).to be_nil | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment