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
]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ UUID4


18
19
20
# File 'lib/uuid4.rb', line 18

def initialize(value)
  @value = value
end

Class Method Details

._newObject


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

alias _new new

._parse(value) ⇒ Object


121
122
123
124
125
126
127
128
129
130
# File 'lib/uuid4.rb', line 121

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 { |formatter|
      formatter.decode(value) if formatter.respond_to?(:decode)
    }.find {|value| !value.nil? }
  end
end

.new(value = nil) ⇒ Object


93
94
95
96
97
98
99
100
101
# File 'lib/uuid4.rb', line 93

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

.try_convert(value) ⇒ Object


103
104
105
106
107
108
109
110
111
# File 'lib/uuid4.rb', line 103

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


113
114
115
116
117
118
119
# File 'lib/uuid4.rb', line 113

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

.valid_int?(int) ⇒ Boolean


132
133
134
# File 'lib/uuid4.rb', line 132

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

Instance Method Details

#==(other) ⇒ Object


22
23
24
25
26
27
# File 'lib/uuid4.rb', line 22

def ==(other)
  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
end

#as_jsonObject


49
50
51
# File 'lib/uuid4.rb', line 49

def as_json(*)
  to_str
end

#componentsObject


75
76
77
78
79
80
81
82
83
# File 'lib/uuid4.rb', line 75

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

#eql?(object) ⇒ Boolean


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

def eql?(object)
  object.is_a?(::UUID4) && object.hash === hash
end

#hashObject


63
64
65
# File 'lib/uuid4.rb', line 63

def hash
  @value.hash
end

#inspectObject


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

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

#to_intObject Also known as: to_i


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

def to_int
  @value
end

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


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/uuid4.rb', line 29

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 RuntimeError.new("Unknown format: #{format}")
  end

  formatter.encode(self)
end

#to_uuid4Object


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

def to_uuid4
  self
end