diff --git a/lib/cql/uuid.rb b/lib/cql/uuid.rb index d5f1a02..f4d5a17 100644 --- a/lib/cql/uuid.rb +++ b/lib/cql/uuid.rb @@ -9,11 +9,13 @@ module Cql # If you want to generate UUIDs see {Cql::TimeUuid::Generator}. # class Uuid + InvalidUuidError = Class.new(CqlError) + # Creates a new UUID either from a string (expected to be on the standard # 8-4-4-4-12 form, or just 32 characters without hyphens), or from a # 128 bit number. # - # @raise [ArgumentError] if the string does not conform to the expected format + # @raise [Cql::Uuid::InvalidUuidError] if the string does not conform to the expected format # def initialize(n) case n @@ -61,22 +63,13 @@ def eql?(other) RAW_FORMAT = '%032x'.force_encoding(Encoding::ASCII).freeze HYPHEN = '-'.force_encoding(Encoding::ASCII).freeze EMPTY_STRING = ''.freeze + HEX_RE = /^[A-Fa-f0-9]+$/ - if RUBY_ENGINE == 'jruby' - HEX_RE = /^[A-Fa-f0-9]+$/ - # See https://github.com/jruby/jruby/issues/1608 - def from_s(str) - str = str.gsub(HYPHEN, EMPTY_STRING) - raise ArgumentError, "Expected 32 hexadecimal digits but got #{str.length}" unless str.length == 32 - raise ArgumentError, "invalid value for Integer(): \"#{str}\"" unless str =~ HEX_RE - Integer(str, 16) - end - else - def from_s(str) - str = str.gsub(HYPHEN, EMPTY_STRING) - raise ArgumentError, "Expected 32 hexadecimal digits but got #{str.length}" unless str.length == 32 - Integer(str, 16) - end + def from_s(str) + str = str.gsub(HYPHEN, EMPTY_STRING) + raise InvalidUuidError, "Expected 32 hexadecimal digits but got #{str.length}" unless str.length == 32 + raise InvalidUuidError, "Expected only hexidecimal digits but got \"#{str}\"" unless str =~ HEX_RE + Integer(str, 16) end end end diff --git a/spec/cql/uuid_spec.rb b/spec/cql/uuid_spec.rb index d4cf0c2..c97d4e4 100644 --- a/spec/cql/uuid_spec.rb +++ b/spec/cql/uuid_spec.rb @@ -15,15 +15,15 @@ module Cql end it 'raises an error if the string is shorter than 32 chars' do - expect { Uuid.new('a4a7090024e111df8924001ff359171') }.to raise_error(ArgumentError) + expect { Uuid.new('a4a7090024e111df8924001ff359171') }.to raise_error(Cql::Uuid::InvalidUuidError) end it 'raises an error if the string is longer than 32 chars' do - expect { Uuid.new('a4a7090024e111df8924001ff35917111') }.to raise_error(ArgumentError) + expect { Uuid.new('a4a7090024e111df8924001ff35917111') }.to raise_error(Cql::Uuid::InvalidUuidError) end it 'raises an error if the string is not a hexadecimal number' do - expect { Uuid.new('a4a7090024e111df8924001ff359171x') }.to raise_error(ArgumentError) + expect { Uuid.new('a4a7090024e111df8924001ff359171x') }.to raise_error(Cql::Uuid::InvalidUuidError) end it 'can be created from a number' do