Class: UUID

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/extra/uuid.rb,
lib/inat/data/types/extras.rb

Constant Summary collapse

BASE =
16
BYTES =
16
DIGITS =
32
ZERO =
make 0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ UUID

Returns a new instance of UUID.



13
14
15
# File 'lib/extra/uuid.rb', line 13

def initialize value
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



11
12
13
# File 'lib/extra/uuid.rb', line 11

def value
  @value
end

Class Method Details

.ddlObject



70
71
72
# File 'lib/inat/data/types/extras.rb', line 70

def ddl
  :TEXT
end

.from_db(src) ⇒ Object



74
75
76
# File 'lib/inat/data/types/extras.rb', line 74

def from_db src
  parse src
end

.generate(count = 1, prefix: nil, prefix_length: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/extra/uuid.rb', line 30

def generate count = 1, prefix: nil, prefix_length: nil
  if prefix == nil
    if prefix_length != nil
      raise ArgumentError, "Prefix length must be Integer < #{ BYTES }!", caller unless Integer === prefix_length && prefix_length < BYTES
      prefix = SecureRandom.hex prefix_length
    else
      prefix = ''
      prefix_length = 0
    end
  else
    raise TypeError, "Prefix must be a String!", caller unless String === prefix
    prefix = prefix.gsub('-', '')
    prefix_length = prefix.length / 2
  end
  result = count.times.map { parse(prefix + SecureRandom.hex(BYTES - prefix_length)) }
  if count == 1
    result[0]
  else
    result
  end
end

.make(*args) ⇒ Object

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/extra/uuid.rb', line 52

def make *args
  raise ArgumentError, "Method receives 1..5 arguments!", caller unless (1..5) === args.size
  raise ArgumentError, "Arguments must be Integers!", caller     unless args.all? { |a| Integer === a }
  last = args.pop
  value = 0
  value += args[0] << 12 * 8 if args.size > 0
  value += args[1] << 10 * 8 if args.size > 1
  value += args[2] <<  8 * 8 if args.size > 2
  value += args[3] <<  6 * 8 if args.size > 3
  value += last
  new(value).freeze
end

.parse(src) ⇒ Object

Raises:

  • (TypeError)


21
22
23
24
25
26
27
28
# File 'lib/extra/uuid.rb', line 21

def parse src
  return nil if src == nil
  raise TypeError, "Source must be a String!", caller unless String === src
  src = src.strip
  raise ArgumentError, "Invalid UUID source: #{ src.inspect }!", caller unless /^\h{8}\-\h{4}\-\h{4}\-\h{4}\-\h{12}$/ === src || /^\h{32}$/ === src
  value = src.gsub('-', '').to_i(BASE)
  new(value).freeze
end

Instance Method Details

#<=>(other) ⇒ Object



85
86
87
88
# File 'lib/extra/uuid.rb', line 85

def <=> other
  return nil unless UUID === other
  @value <=> other.value
end

#inspectObject



79
80
81
# File 'lib/extra/uuid.rb', line 79

def inspect
  to_s
end

#to_dbObject



80
81
82
# File 'lib/inat/data/types/extras.rb', line 80

def to_db
  to_s
end

#to_iObject



69
70
71
# File 'lib/extra/uuid.rb', line 69

def to_i
  @value
end

#to_queryObject



84
85
86
# File 'lib/inat/data/types/extras.rb', line 84

def to_query
  to_s
end

#to_sObject



73
74
75
76
77
# File 'lib/extra/uuid.rb', line 73

def to_s
  raw = @value.to_s(BASE)
  raw = '0' * (DIGITS - raw.length) + raw if raw.length < DIGITS
  "#{ raw[0..7] }-#{ raw[8..11] }-#{ raw[12..15] }-#{ raw[16..19] }-#{ raw[20..31] }"
end