Class: UUID4

Inherits:
Object
  • Object
show all
Defined in:
lib/uuid4.rb,
lib/uuid4/version.rb,
lib/uuid4/formatter/urn.rb,
lib/uuid4/formatter/base62.rb,
lib/uuid4/formatter/compact.rb,
lib/uuid4/formatter/default.rb

Defined Under Namespace

Modules: Formatter, VERSION

Constant Summary collapse

FORMATTERS =
[
  Formatter::Default.new,
  Formatter::Compact.new,
  Formatter::URN.new,
  Formatter::Base62.new,
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ UUID4

Returns a new instance of UUID4.



20
21
22
# File 'lib/uuid4.rb', line 20

def initialize(value)
  @value = value
end

Class Method Details

._newObject



96
# File 'lib/uuid4.rb', line 96

alias _new new

._parse(value) ⇒ Object



127
128
129
130
131
132
133
134
135
136
# File 'lib/uuid4.rb', line 127

def _parse(value)
  if value.respond_to?(:to_int) && valid_int?(value = value.to_int)
    value
  else
    # Return the result of the first formatter that can decode this value
    FORMATTERS.lazy.map do |formatter|
      formatter.decode(value) if formatter.respond_to?(:decode)
    end.find {|value| !value.nil? } # rubocop:disable Style/MultilineBlockChain
  end
end

.new(value = nil) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/uuid4.rb', line 99

def new(value = nil)
  if value.nil?
    super(SecureRandom.uuid.tr('-', '').hex)
  elsif (uuid = try_convert(value))
    uuid
  else
    raise TypeError.new "Invalid UUID: #{value.inspect}"
  end
end

.try_convert(value) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/uuid4.rb', line 109

def try_convert(value)
  if value.nil? || value.is_a?(::UUID4)
    value
  elsif value.respond_to?(:to_uuid4)
    value.to_uuid4
  elsif (value = _parse(value))
    _new value
  end
end

.valid?(value) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
122
123
124
125
# File 'lib/uuid4.rb', line 119

def valid?(value)
  if value.is_a?(::UUID4)
    true
  else
    !try_convert(value).nil?
  end
end

.valid_int?(int) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/uuid4.rb', line 138

def valid_int?(int)
  int.to_s(16).rjust(32, '0') =~ Formatter::Compact::REGEXP
end

Instance Method Details

#==(other) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/uuid4.rb', line 24

def ==(other)
  # rubocop:disable Style/CaseEquality
  return value === other.to_i if other.is_a?(UUID4)
  return other.to_uuid4 == self if other.respond_to?(:to_uuid4)

  self.class._parse(other) === value
  # rubocop:enable Style/CaseEquality
end

#as_jsonObject



53
54
55
# File 'lib/uuid4.rb', line 53

def as_json(*)
  to_str
end

#componentsObject



81
82
83
84
85
86
87
88
89
# File 'lib/uuid4.rb', line 81

def components
  [
    (value >> 96) & 0xFFFFFFFF,
    (value >> 80) & 0xFFFF,
    (value >> 64) & 0xFFFF,
    (value >> 48) & 0xFFFF,
    (value >> 0)  & 0xFFFFFFFFFFFF,
  ]
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
# File 'lib/uuid4.rb', line 71

def eql?(other)
  # rubocop:disable Style/CaseEquality
  other.is_a?(::UUID4) && other.hash === hash
  # rubocop:enable Style/CaseEquality
end

#hashObject



67
68
69
# File 'lib/uuid4.rb', line 67

def hash
  @value.hash
end

#inspectObject



77
78
79
# File 'lib/uuid4.rb', line 77

def inspect
  "<UUID4:#{self}>"
end

#to_intObject Also known as: to_i



61
62
63
# File 'lib/uuid4.rb', line 61

def to_int
  @value
end

#to_str(format: :default, formatter: nil) ⇒ Object Also known as: to_s, to_uuid



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/uuid4.rb', line 33

def to_str(format: :default, formatter: nil)
  case format
    when :default
      formatter = FORMATTERS[0]
    when :compact
      formatter = FORMATTERS[1]
    when :urn
      formatter = FORMATTERS[2]
    when :base62
      formatter = FORMATTERS[3]
    else
      raise "Unknown format: #{format}"
  end

  formatter.encode(self)
end

#to_uuid4Object



57
58
59
# File 'lib/uuid4.rb', line 57

def to_uuid4
  self
end