class UUID4

Constants

FORMATTERS

Attributes

value[R]

Public Class Methods

new(value) click to toggle source
# File lib/uuid4.rb, line 18
def initialize(value)
  @value = value
end
Also aliased as: _new

Protected Class Methods

_parse(value) click to toggle source
# 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) click to toggle source
Calls superclass method
# File lib/uuid4.rb, line 93
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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/uuid4.rb, line 132
def valid_int?(int)
  int.to_s(16).rjust(32, '0') =~ Formatter::Compact::REGEXP
end

Private Class Methods

_new(value)
Alias for: new

Public Instance Methods

==(other) click to toggle source
# 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_json(*) click to toggle source
# File lib/uuid4.rb, line 49
def as_json(*)
  to_str
end
components() click to toggle source
# 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) click to toggle source
# File lib/uuid4.rb, line 67
def eql?(object)
  object.is_a?(::UUID4) && object.hash === hash
end
hash() click to toggle source
# File lib/uuid4.rb, line 63
def hash
  @value.hash
end
inspect() click to toggle source
# File lib/uuid4.rb, line 71
def inspect
  "<UUID4:#{to_s}>"
end
to_i()
Alias for: to_int
to_int() click to toggle source
# File lib/uuid4.rb, line 57
def to_int
  @value
end
Also aliased as: to_i
to_s(format: :default, formatter: nil)
Alias for: to_str
to_str(format: :default, formatter: nil) click to toggle source
# 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
Also aliased as: to_s, to_uuid
to_uuid(format: :default, formatter: nil)
Alias for: to_str
to_uuid4() click to toggle source
# File lib/uuid4.rb, line 53
def to_uuid4
  self
end