Class: UUID

Inherits:
Object show all
Defined in:
lib/uuid.rb

Overview

Three inputs a UUID ‘string’ of hexdigits and - {} a URN prefixed UUID string a IO input of bytes as a string an array of bytes/numbers a single int

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uuid) ⇒ UUID

Returns a new instance of UUID.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/uuid.rb', line 17

def initialize(uuid)
  @uuid_fmt_32 =  "#{'%x' * 8}#{'-%x%x%x%x'*3}-#{'%x'*12}" 
  @uuid_fmt_16 =  "#{'%02x' * 4}#{'-%02x%02x'*3}-#{'%02x'*6}" 
  case uuid
    when String 
          if  uuid =~ /^urn:/ then
             # assume urn input 
             @value = UUID.create_from_uuid_string(uuid.sub('urn:',''))
          elsif  uuid.length > 31 and uuid =~ /^[-0-9a-fA-F{}]+$/
             # uuid input syntax
             @value= UUID.create_from_uuid_string(uuid)
          else
              raise Exception.new("Fail to create uuid from string:#{uuid}")
          end
    when Bignum
       @value = uuid
    when Fixnum
       @value = uuid
    else
      raise Exception.new("Fail to create uuid: #{uuid.class}:#{uuid}")
    end
end

Instance Attribute Details

#byte_arrayObject

Returns the value of attribute byte_array.



15
16
17
# File 'lib/uuid.rb', line 15

def byte_array
  @byte_array
end

#uuid_fmt_32Object (readonly)

Returns the value of attribute uuid_fmt_32.



16
17
18
# File 'lib/uuid.rb', line 16

def uuid_fmt_32
  @uuid_fmt_32
end

#valueObject

Returns the value of attribute value.



15
16
17
# File 'lib/uuid.rb', line 15

def value
  @value
end

Class Method Details

.create_from_bytes(input) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/uuid.rb', line 43

def self.create_from_bytes(input)
    case input
    when String
          byte_string = input.chars
    when Bignum
          hexstring = input 
    when Fixnum
          hexstring = input 
    when Array
          byte_string = input
    end
    unless byte_string.nil?
       hexstring = byte_string.reduce(0) {|a,b| a << 8| b.ord }
    end
    new(hexstring)
end

.create_from_uuid_string(uuid) ⇒ Object



39
40
41
42
# File 'lib/uuid.rb', line 39

def self.create_from_uuid_string(uuid)
  hexstring = uuid.tr('{}-','').downcase
  hexstring.chars.map(&:hex).reduce(0) {|a,b|( (a << 4)|b ) }
end

Instance Method Details

#==(other) ⇒ Object



83
84
85
# File 'lib/uuid.rb', line 83

def ==(other)
    self.equal?(other) or (other.kind_of?(UUID) and self.value == other.value )
end

#bytesObject



59
60
61
# File 'lib/uuid.rb', line 59

def bytes
    bytes_array.pack('C*')
end

#bytes_array(conversion = :self) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/uuid.rb', line 62

def bytes_array(conversion=:self)
    (0..127).step(8).map {|s| 
        if conversion == :self then
          value_byte(s)
        else 
          value_byte(s).send(conversion) 
        end
    }.reverse
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def eql?(other)
   self == other
end

#to_iObject



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

def to_i
  value
end

#to_sObject



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

def to_s
      uuid_fmt_16 % bytes_array()
end

#value_byte(s, mask = 0xff) ⇒ Object



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

def value_byte(s,mask=0xff) 
      (@value >> s) & mask
end