Skip to content

Instantly share code, notes, and snippets.

@the-teacher
Created September 29, 2016 07:54
Show Gist options
  • Save the-teacher/2aa6fde120b995df36dc8b3f968bcc62 to your computer and use it in GitHub Desktop.
Save the-teacher/2aa6fde120b995df36dc8b3f968bcc62 to your computer and use it in GitHub Desktop.
Shop GUID Algo
require 'digest/md5'
module ShopCartDigestGUID
private
def guid_generator
srand.to_s[1..12]
end
end
class ShopCart
attr_accessor :guid
prepend ::ShopCartDigestGUID
def initialize
@guid = splitter guid_generator, 4
end
private
def guid_generator
::Digest::MD5.hexdigest(srand.to_s)[1..12].upcase
end
def splitter value, size = 4
value.to_s.scan(/.{1,#{size}}/).join(' ')
end
end
cart = ShopCart.new
puts cart.guid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment