Class: WithUuid::CombUuid
- Inherits:
-
Object
- Object
- WithUuid::CombUuid
- Defined in:
- lib/with_uuid/comb_uuid.rb
Class Method Summary collapse
Instance Method Summary collapse
- #[](idx) ⇒ Object
- #first ⇒ Object
-
#initialize(uuid_str) ⇒ CombUuid
constructor
A new instance of CombUuid.
- #last ⇒ Object
- #parts ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(uuid_str) ⇒ CombUuid
Returns a new instance of CombUuid.
4 5 6 |
# File 'lib/with_uuid/comb_uuid.rb', line 4 def initialize( uuid_str ) @uuid_str = uuid_str end |
Class Method Details
.uuid ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/with_uuid/comb_uuid.rb', line 28 def self.uuid # See: # http://stackoverflow.com/questions/7747145/is-comb-guid-a-good-idea-with-rails-3-1-if-i-use-guids-for-primary-keys # http://www.codeproject.com/Articles/32597/Performance-Comparison-Identity-x-NewId-x-NewSeque # MS SQL syntax: SELECT CAST(CAST(NEWID() AS BINARY(10)) + CAST(GETDATE() AS BINARY(6)) AS UNIQUEIDENTIFIER) # Get current Time object = Time.now.utc # Convert to integer with milliseconds: (Seconds since Epoch * 1000) + (6-digit microsecond fraction / 1000) = (.tv_sec * 1000) + (.tv_usec / 1000) # Format as hex, minimum of 12 digits, with leading zero. Note that 12 hex digits handles to year 10889 (*). = "%012x" % # If we supply UUIDTOOLS with a MAC address, it will use that rather than retrieving from system. # Use a regular expression to split into array, then insert ":" characters so it "looks" like a MAC address. UUIDTools::UUID.mac_address = (.scan( /.{2}/ )).join(":") # Generate Version 1 UUID (see RFC 4122). uuid_str = UUIDTools::UUID.().to_s.upcase # (*) A note on maximum time handled by 6-byte timestamp that includes milliseconds: # If utc_timestamp_with_ms_hexstring = "FFFFFFFFFFFF" (12 F's), then # Time.at(Float(utc_timestamp_with_ms_hexstring.hex)/1000).utc.iso8601(10) = "10889-08-02T05:31:50.6550292968Z". self.new( uuid_str ) end |
Instance Method Details
#[](idx) ⇒ Object
16 17 18 |
# File 'lib/with_uuid/comb_uuid.rb', line 16 def []( idx ) parts[idx] end |
#first ⇒ Object
20 21 22 |
# File 'lib/with_uuid/comb_uuid.rb', line 20 def first parts[0] end |
#last ⇒ Object
24 25 26 |
# File 'lib/with_uuid/comb_uuid.rb', line 24 def last parts[4] end |
#parts ⇒ Object
12 13 14 |
# File 'lib/with_uuid/comb_uuid.rb', line 12 def parts @parts ||= @uuid_str.split( '-' ) end |
#to_s ⇒ Object
8 9 10 |
# File 'lib/with_uuid/comb_uuid.rb', line 8 def to_s uuid_str end |