Permalink
Browse files

Merge pull request #26 from idibon/master

add custom UUID namespace declaration
  • Loading branch information...
2 parents e78a4e0 + 598f486 commit f712b2fdbeb824c32a85211b1e88c618b93d0efa @pyromaniac pyromaniac committed Mar 26, 2013
View
@@ -41,6 +41,29 @@ class Email < ActiveRecord::Base
end
```
+### specify a natural key for generating the UUID based on one or more columns
+```ruby
+class Email < ActiveRecord::Base
+ include ActiveUUID::UUID
+ natural_key :sender_id, :received_at
+ belongs_to :sender
+end
+```
+
+`natural_key` generates a SHA1-based UUID in the ISO OID namespace by default. [7]
+
+### specify a custom UUID namespace for the natural key
+```ruby
+class Email < ActiveRecord::Base
+ include ActiveUUID::UUID
+ uuid_namespace "1dd74dd0-d116-11e0-99c7-5ac5d975667e"
+ natural_key :sender_id, :received_at
+ belongs_to :sender
+end
+```
+
+`uuid_namespace` can either be a UUID in string format, or a UUIDTools::UUID object.
+
### use it:
Here are some example specs:
@@ -118,6 +141,7 @@ Or get the code here: https://github.com/jashmenn/activeuuid
* [4] http://www.codinghorror.com/blog/2007/03/primary-keys-ids-versus-guids.html
* [5] http://krow.livejournal.com/497839.html
* [6] https://github.com/jamesgolick/friendly
+* [7] http://tools.ietf.org/html/rfc4122.html#appendix-C
## Dependencies
Rails ~> 3.1.0
@@ -81,6 +81,7 @@ module UUID
included do
class_attribute :_natural_key, instance_writer: false
+ class_attribute :_uuid_namespace, instance_writer: false
class_attribute :_uuid_generator, instance_writer: false
self._uuid_generator = :random
@@ -93,6 +94,11 @@ def natural_key(*attributes)
self._natural_key = attributes
end
+ def uuid_namespace(namespace)
+ namespace = UUIDTools::UUID.parse_string(namespace) unless namespace.is_a? UUIDTools::UUID
+ self._uuid_namespace = namespace
+ end
+
def uuid_generator(generator_name)
self._uuid_generator = generator_name
end
@@ -120,7 +126,7 @@ def create_uuid
if _natural_key
# TODO if all the attributes return nil you might want to warn about this
chained = _natural_key.map { |attribute| self.send(attribute) }.join('-')
- UUIDTools::UUID.sha1_create(UUIDTools::UUID_OID_NAMESPACE, chained)
+ UUIDTools::UUID.sha1_create(_uuid_namespace || UUIDTools::UUID_OID_NAMESPACE, chained)
else
case _uuid_generator
when :random
@@ -0,0 +1,4 @@
+Fabricator(:uuid_article_with_namespace) do
+ title { Forgery::LoremIpsum.word }
+ body { Forgery::LoremIpsum.sentence }
+end
@@ -0,0 +1,4 @@
+Fabricator(:uuid_article_with_natural_key) do
+ title { Forgery::LoremIpsum.word }
+ body { Forgery::LoremIpsum.sentence }
+end
@@ -184,4 +184,26 @@
end
end
end
-end
+end
+
+describe UuidArticleWithNaturalKey do
+ let!(:article) { Fabricate :uuid_article_with_natural_key }
+ let!(:id) { article.id }
+ let!(:uuid) { UUIDTools::UUID.sha1_create(UUIDTools::UUID_OID_NAMESPACE, article.title) }
+ subject { article }
+ context 'natural_key' do
+ its(:id) { should == uuid }
+ end
+end
+
+describe UuidArticleWithNamespace do
+ let!(:article) { Fabricate :uuid_article_with_namespace }
+ let!(:id) { article.id }
+ let!(:namespace) { UuidArticleWithNamespace._uuid_namespace }
+ let!(:uuid) { UUIDTools::UUID.sha1_create(namespace, article.title) }
+ subject { article }
+ context 'natural_key_with_namespace' do
+ its(:id) { should == uuid }
+ end
+end
+
@@ -0,0 +1,6 @@
+class UuidArticleWithNamespace < ActiveRecord::Base
+ include ActiveUUID::UUID
+ self.table_name = 'uuid_articles'
+ natural_key :title
+ uuid_namespace "45e676ea-8a43-4ffe-98ca-c142b0062a83" # a random UUID
+end
@@ -0,0 +1,5 @@
+class UuidArticleWithNaturalKey < ActiveRecord::Base
+ include ActiveUUID::UUID
+ self.table_name = 'uuid_articles'
+ natural_key :title
+end

0 comments on commit f712b2f

Please sign in to comment.