Class: Serf::Util::Uuidable

Inherits:
Object
  • Object
show all
Defined in:
lib/serf/util/uuidable.rb

Overview

Helper module to for various UUID tasks.

  1. Primarily to create and parse ‘coded’ UUIDs, which are just

base64 encoded UUIDs without trailing '='.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Uuidable

Returns a new instance of Uuidable.



18
19
20
21
# File 'lib/serf/util/uuidable.rb', line 18

def initialize(*args)
  opts = Optser.extract_options! args
  @uuid_tool = opts.get :uuid_tool, UUIDTools::UUID
end

Instance Attribute Details

#uuid_toolObject (readonly)

Returns the value of attribute uuid_tool.



16
17
18
# File 'lib/serf/util/uuidable.rb', line 16

def uuid_tool
  @uuid_tool
end

Instance Method Details

#coded_uuid_time(coded_uuid) ⇒ Object

Parses a coded_uuid and returns a time object for the Timestamped UUID.

Parameters:

  • coded_uuid

    the coded uuid from which to get a time.

Returns:

  • ruby time object for which the coded_uuid was timestamped.



52
53
54
55
# File 'lib/serf/util/uuidable.rb', line 52

def coded_uuid_time(coded_uuid)
  uuid = parse_coded_uuid coded_uuid
  uuid.timestamp.utc
end

#create_coded_uuidObject

Creates a Timestamp UUID, base64 encoded.

NOTE: UUIDTools TimeStamp code creates a UTC based timestamp UUID.



28
29
30
31
32
33
34
35
36
# File 'lib/serf/util/uuidable.rb', line 28

def create_coded_uuid
  # All raw UUIDs are 16 bytes long. Base64 lengthens the string to
  # 24 bytes long. We chomp off the last two equal signs '==' to
  # trim the string length to 22 bytes. This gives us an overhead
  # of an extra 6 bytes over raw UUID, but with the readability
  # benefit. And saves us 14 bytes of size from the 'standard'
  # string hex representation of UUIDs.
  Base64.urlsafe_encode64(uuid_tool.timestamp_create.raw).chomp('==')
end

#create_uuids(parent = nil) ⇒ Object

Create a new set of uuids.



60
61
62
63
64
65
66
67
68
69
# File 'lib/serf/util/uuidable.rb', line 60

def create_uuids(parent=nil)
  parent ||= {}
  Hashie::Mash.new(
    uuid: create_coded_uuid,
    parent_uuid: parent[:uuid],
    origin_uuid: (
      parent[:origin_uuid] ||
      parent[:parent_uuid] ||
      parent[:uuid]))
end

#parse_coded_uuid(coded_uuid) ⇒ Object

Parameters:

  • coded_uuid

    a coded uuid to parse.



41
42
43
# File 'lib/serf/util/uuidable.rb', line 41

def parse_coded_uuid(coded_uuid)
  uuid_tool.parse_raw Base64.urlsafe_decode64("#{coded_uuid}==")
end