Skip to content
This repository

Identity 

jnunemaker edited this page · 3 revisions
Clone this wiki locally

UUID's

By default, Toystore uses uuid's for keys, mostly because they are practically unique across time, models, servers and processes. UUID's are the default, but they are not the only option.

Create Your Own Key Factory

You can also create your own key factories quite easily:

class NamespacedUUIDKeyFactory < Toy::Identity::AbstractKeyFactory
  # How should the id be typecast
  def key_type
    String
  end

  # How do we generate the next key
  # object is the record need an id
  def next_key(object)
    [object.class.name, SimpleUUID::UUID.new.to_guid].join(':')
  end
end

class User
  include Toy::Store
  key NamespacedUUIDKeyFactory.new
end

pp User.new.id # "User:ca5ef5c2-3183-11e0-9035-5dd876820c2a"

This sets the key factory on a per model basis. You can also change the default key factory for every Toystore model.

class NamespacedUUIDKeyFactory < Toy::Identity::AbstractKeyFactory
  # How should the id be typecast
  def key_type
    String
  end

  # How do we generate the next key
  # object is the record need an id
  def next_key(object)
    [object.class.name, SimpleUUID::UUID.new.to_guid].join(':')
  end
end

Toy.key_factory = NamespacedUUIDKeyFactory.new

class Game
  include Toy::Store
end

pp Game.new.id # "Game:08dd6ce8-3184-11e0-80b5-28885337cc2d"
Something went wrong with that request. Please try again.