Class: UUID

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

Constant Summary collapse

VERSION =

Version

'0.2.0'
REGEX =

Basic regex for validating UUID formatted string

/\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/
NIL =

Nil/Empty UUID

UUID('00000000-0000-0000-0000-000000000000')
FFF =
Note:

It is not a valid UUID but can be usefull

UUID with all bit set.

UUID('ffffffff-ffff-ffff-ffff-ffffffffffff')

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(v) ⇒ UUID

Create UUID

Parameters:

  • UUID (String, Integer)

    value

Raises:

  • (ArgumentError)

    given integer is too big or given string is not 16-byte 8bit



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/uuid.rb', line 49

def initialize(v)
    case v
    when Integer
        hex = "%032x" % [ v ]
        if hex.size > 32
            raise ArgumentError,
                  "integer to big (must fit in 128-bit)"
        end
        @raw = [ hex ].pack('H32')
    when String
        if v.size != 16 || v.encoding.name != 'ASCII-8BIT'
            raise ArgumentError,
                "need to be a 16 byte ASCII-8BIT string (#{v})"
        end
        @raw = v.dup.freeze
    else
        raise ArgumentError,
              "expected 128-bit integer or 16-byte string"
    end
end

Class Method Details

.parse(str) ⇒ UUID

Parse a UUID formatted string

Parameters:

  • str (String)

    string to parse

Returns:

Raises:

  • (ArgumentError)

    the string is not parsable as a UUID



33
34
35
36
37
38
39
# File 'lib/uuid.rb', line 33

def self.parse(str)
    unless str =~ REGEX
        raise ArgumentError, "unable to parse UUID value"
    end

    self.new( [str.delete('-')].pack('H32') )
end

Instance Method Details

#===(other) ⇒ Object



87
88
89
90
# File 'lib/uuid.rb', line 87

def ===(other)
    other = UUID(other) rescue nil
    !other.nil? && self.to_raw == other.to_raw
end

#bytesArray<Integer>

Return the 16-byte sequence forming the UUID.

Returns:

  • (Array<Integer>)


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

def bytes
    @raw.bytes
end

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

Returns:

  • (Boolean)


91
92
93
# File 'lib/uuid.rb', line 91

def eql?(other)
    !other.nil? && other.is_a?(UUID) && self.to_raw == other.to_raw
end

#hashInteger

Generates an integer hash value

Returns:

  • (Integer)

    hash value



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

def hash
    @raw.hash
end

#inspectString

Returns a string containing a human-readable representation of this object

Returns:

  • (String)


103
104
105
# File 'lib/uuid.rb', line 103

def inspect
    "#<#{self.class}:#{to_s}>"
end

#sql_literal(ds) ⇒ Object

Sequel



148
# File 'lib/uuid.rb', line 148

def sql_literal(ds) ; '0x' + @raw.unpack('H32')[0] 		; end

#to_blobObject



149
# File 'lib/uuid.rb', line 149

def to_blob         ; Sequel.blob(@raw)            		; end

#to_iInteger Also known as: to_int

Get the UUID value as an integer

Returns:

  • (Integer)

    UUID value



112
113
114
115
# File 'lib/uuid.rb', line 112

def to_i
    i, j = @raw.unpack('Q>Q>')
    i * 2**64 + j
end

#to_json(*a) ⇒ String

Get the JSON represenation

Returns:

  • (String)


142
143
144
# File 'lib/uuid.rb', line 142

def to_json(*a)
    self.to_s.to_json(*a)
end

#to_rawObject



96
# File 'lib/uuid.rb', line 96

def to_raw          ; @raw					; end

#to_sString Also known as: to_str

Get the UUID string representation

Returns:

  • (String)

    UUID string representation



123
124
125
# File 'lib/uuid.rb', line 123

def to_s
    @raw.unpack('H8H4H4H4H12').join('-')
end

#to_uriString

Get the URI has a URN in UUID namespace

Returns:

  • (String)

    Uniform Resource Name



133
134
135
# File 'lib/uuid.rb', line 133

def to_uri
    "urn:uuid:#{self}"
end