gold-record 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/HISTORY.rdoc CHANGED
@@ -1,3 +1,7 @@
1
+ === Version 0.2.1 (2009-12-06)
2
+
3
+ * Sets version nibble and reserved bits in generated UUID values (per RFC 4122), leaving 122 bits for random data. This restores behavior present in version 0.1.0.
4
+
1
5
  === Version 0.2.0 (2009-12-06)
2
6
 
3
7
  This version replaces ID generation and parsing code from UUIDTools with smaller/faster implementation using pack and SecureRandom directly.
data/README.rdoc CHANGED
@@ -1,5 +1,7 @@
1
1
  = GoldRecord
2
2
 
3
+ Unobtrusive binary UUIDs for ActiveRecord.
4
+
3
5
  * http://github.com/ydnar/gold-record
4
6
  * http://gemcutter.org/gems/gold-record
5
7
  * mailto:randy@shaderlab.com
@@ -10,11 +12,12 @@ GoldRecord is an extension for ActiveRecord that implements unobtrusive binary U
10
12
 
11
13
  == FEATURES
12
14
 
15
+ GoldRecord supports SchemaDumper by not declaring the <tt>id</tt> column a <tt>PRIMARY KEY</tt>. The odds of a random UUID collision are documented here[http://en.wikipedia.org/wiki/Universally_Unique_Identifier#Random_UUID_probability_of_duplicates].
16
+
13
17
  * Uses space-efficient 16-byte binary representation for UUIDs.
14
18
  * Works with associations, migrations and ActiveRecord::SchemaDumper.
15
- * Transparently converts to/from hex-encoded UUIDs in #to_param and #find.
16
-
17
- GoldRecord supports SchemaDumper by not declaring the <tt>id</tt> column a <tt>PRIMARY KEY</tt>. The odds of a random UUID collision are documented here[http://en.wikipedia.org/wiki/Universally_Unique_Identifier#Random_UUID_probability_of_duplicates].
19
+ * Transparently converts to hex-encoded UUIDs in #to_param.
20
+ * Transparently handles binary, hex-encoded and URL-safe Base64 UUIDs in #find.
18
21
 
19
22
  == SYNOPSIS
20
23
 
@@ -35,24 +38,33 @@ GoldRecord supports SchemaDumper by not declaring the <tt>id</tt> column a <tt>P
35
38
  validates_uniqueness_of :lottery_number, :on => :create
36
39
  end
37
40
 
41
+ == FIXTURES
42
+
43
+ # In test/helper.rb or spec_helper.rb:
44
+ require 'active_record/fixtures'
45
+ Fixtures.send :include, GoldRecord::Fixtures
46
+
38
47
  == MIGRATIONS
39
48
 
40
49
  class MakeGoldRecords < ActiveRecord::Migration
50
+ TABLES = [:labels, :record_stores]
51
+ ASSOCIATIONS = [
52
+ [:artists, :label_id, :id],
53
+ [:labels_record_stores, :label_id, false],
54
+ [:labels_record_stores, :record_store_id, false],
55
+ [:artists_record_stores, :record_store_id, false],
56
+ ]
57
+
41
58
  def self.up
42
59
  # Change each int(11) id column to binary(16).
43
60
  # This preserves the column’s original value as a right-padded 16-byte string.
44
- [:labels, :record_stores].each do |table|
61
+ TABLES.each do |table|
45
62
  change_integer_primary_key_to_uuid(table)
46
63
  add_index table, :id
47
64
  end
48
65
 
49
66
  # Change association columns to binary(16).
50
- [
51
- [:artists, :label_id, :id],
52
- [:labels_record_stores, :label_id, false],
53
- [:labels_record_stores, :record_store_id, false],
54
- [:artists_record_stores, :record_store_id, false],
55
- ].each do |table, column, primary_key|
67
+ ASSOCIATIONS.each do |table, column, primary_key|
56
68
  change_integer_to_uuid(table, column, primary_key)
57
69
  end
58
70
  end
@@ -65,18 +77,13 @@ GoldRecord supports SchemaDumper by not declaring the <tt>id</tt> column a <tt>P
65
77
  def self.down
66
78
  # Change each binary(16) id column to int(11).
67
79
  # MySQL casts the string value to an integer.
68
- [:labels, :record_stores].each do |table|
80
+ TABLES.each do |table|
69
81
  remove_index table, :id
70
82
  change_uuid_to_integer_primary_key(table)
71
83
  end
72
84
 
73
85
  # Change association columns to int(11).
74
- [
75
- [:artists, :label_id, :id],
76
- [:labels_record_stores, :label_id, false],
77
- [:labels_record_stores, :record_store_id, false],
78
- [:artists_record_stores, :record_store_id, false],
79
- ].each do |table, column|
86
+ ASSOCIATIONS.each do |table, column|
80
87
  change_uuid_to_integer(table, column, primary_key)
81
88
  end
82
89
  end
@@ -3,7 +3,14 @@ require 'securerandom'
3
3
  module GoldRecord
4
4
  module UUID
5
5
  def self.random_generate
6
- SecureRandom.random_bytes(16)
6
+ bin = SecureRandom.random_bytes(16)
7
+ # Set version 4
8
+ bin[6] &= 0b01001111
9
+ bin[6] |= 0b01000000
10
+ # Set reserved bits
11
+ bin[8] &= 0b10111111
12
+ bin[8] |= 0b10000000
13
+ bin
7
14
  end
8
15
 
9
16
  def self.coerce(value)
@@ -2,7 +2,7 @@ module GoldRecord
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 2
5
- TINY = 0
5
+ TINY = 1
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -2,12 +2,32 @@ require 'cases/helper'
2
2
 
3
3
  class RandomGenerateTest < ActiveRecord::TestCase
4
4
  def test_size_of_generated_string
5
- assert_equal 16, GoldRecord::UUID.random_generate.size
5
+ 1000.times do
6
+ assert_equal 16, GoldRecord::UUID.random_generate.size
7
+ end
8
+ end
9
+
10
+ # 00000000-0000-4000-8000-000000000000
11
+ # ffffffff-ffff-4fff-bfff-ffffffffffff
12
+
13
+ def test_version_nibble
14
+ 1000.times do
15
+ value = GoldRecord::UUID.random_generate
16
+ assert_equal (value[6] & 0xf0), 0x40
17
+ end
18
+ end
19
+
20
+ def test_reserved_bits
21
+ 1000.times do
22
+ value = GoldRecord::UUID.random_generate
23
+ assert_equal (value[8] & 0x80), 0x80
24
+ assert_equal (value[8] & 0x40), 0x00
25
+ end
6
26
  end
7
27
 
8
28
  def test_randomness # This is kind of weak.
9
29
  cache = {}
10
- 10000.times do
30
+ 1000.times do
11
31
  value = GoldRecord::UUID.random_generate
12
32
  assert !cache.has_key?(value)
13
33
  cache[value] = true
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gold-record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Randy Reddig
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-06 00:00:00 +00:00
12
+ date: 2009-12-07 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency