Created
September 29, 2016 07:54
-
-
Save the-teacher/2aa6fde120b995df36dc8b3f968bcc62 to your computer and use it in GitHub Desktop.
Shop GUID Algo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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