Class: Transit::UUID

Inherits:
Object
  • Object
show all
Defined in:
lib/transit/transit_types.rb

Overview

Represents a transit UUID extension type.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uuid_or_most_significant_bits = nil, least_significant_bits = nil) ⇒ UUID

Returns a new instance of UUID.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/transit/transit_types.rb', line 89

def initialize(uuid_or_most_significant_bits=nil,least_significant_bits=nil)
  case uuid_or_most_significant_bits
  when String
    @string_rep = uuid_or_most_significant_bits
  when Array
    @numeric_rep = uuid_or_most_significant_bits.map {|n| twos_complement(n)}
  when Numeric
    @numeric_rep = [twos_complement(uuid_or_most_significant_bits), twos_complement(least_significant_bits)]
  when nil
    @string_rep = SecureRandom.uuid
  else
    raise "Can't build UUID from #{uuid_or_most_significant_bits.inspect}"
  end
end

Class Method Details

.randomObject



85
86
87
# File 'lib/transit/transit_types.rb', line 85

def self.random
  new
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



120
121
122
123
124
125
126
127
128
# File 'lib/transit/transit_types.rb', line 120

def ==(other)
  return false unless other.is_a?(self.class)
  if @numeric_rep
    other.most_significant_bits == most_significant_bits &&
      other.least_significant_bits == least_significant_bits
  else
    other.to_s == @string_rep
  end
end

#hashObject



131
132
133
# File 'lib/transit/transit_types.rb', line 131

def hash
  most_significant_bits.hash + least_significant_bits.hash
end

#inspectObject



116
117
118
# File 'lib/transit/transit_types.rb', line 116

def inspect
  @inspect ||= "<#{self.class} \"#{to_s}\">"
end

#least_significant_bitsObject



112
113
114
# File 'lib/transit/transit_types.rb', line 112

def least_significant_bits
  @least_significant_bits ||= numeric_rep[1]
end

#most_significant_bitsObject



108
109
110
# File 'lib/transit/transit_types.rb', line 108

def most_significant_bits
  @most_significant_bits ||= numeric_rep[0]
end

#to_sObject



104
105
106
# File 'lib/transit/transit_types.rb', line 104

def to_s
  @string_rep ||= numbers_to_string
end