Class: Serf::Util::Uuidable
- Inherits:
-
Object
- Object
- Serf::Util::Uuidable
- Defined in:
- lib/serf/util/uuidable.rb
Overview
Helper module to for various UUID tasks.
-
Primarily to create and parse ‘coded’ UUIDs, which are just
base64 encoded UUIDs without trailing '='.
Instance Attribute Summary collapse
-
#uuid_tool ⇒ Object
readonly
Returns the value of attribute uuid_tool.
Instance Method Summary collapse
-
#coded_uuid_time(coded_uuid) ⇒ Object
Parses a coded_uuid and returns a time object for the Timestamped UUID.
-
#create_coded_uuid ⇒ Object
Creates a Timestamp UUID, base64 encoded.
-
#create_uuids(parent = nil) ⇒ Object
Create a new set of uuids.
-
#initialize(*args) ⇒ Uuidable
constructor
A new instance of Uuidable.
- #parse_coded_uuid(coded_uuid) ⇒ Object
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. args @uuid_tool = opts.get :uuid_tool, UUIDTools::UUID end |
Instance Attribute Details
#uuid_tool ⇒ Object (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.
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..utc end |
#create_coded_uuid ⇒ Object
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..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
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 |