mm_uses_uuid 0.0.6 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -61,15 +61,36 @@ class Group
61
61
 
62
62
  many :people, :class_name => 'Person'
63
63
 
64
- uuid_lsn 0xf
64
+ uuid_lsn 0x0
65
65
  end
66
66
  ```
67
67
 
68
- Once this value is set you can use `MongoMapper.find_by_uuid(...)` to find by id (or a list of ids) and it will automatically detect the class by inspecting
69
- the last character of the UUIDs you pass. So for the example above, all UUIDs generated for new Group objects will end in 'f'
70
- and, if you pass a UUID ending in 'f' to `MongoMapper.find`, it will pass that request on to `Group.find()`.
68
+ Once this value is set you can use `UuidModel.find(...)` to find by id (or a list of ids) and it will automatically detect the class by inspecting
69
+ the last character of the UUIDs you pass. So for the example above, all UUIDs generated for new Group objects will end in '0'
70
+ and, if you pass a UUID ending in '0' to `UuidModel.find`, it will pass that request on to `Group.find()`. Clearly, this method only allows you
71
+ to target 16 collections at most as the last nibble can only have values of 0 to 15.
72
+
73
+ This method can be useful if you need to implement a polymorphic many-to-many association but you don't want to use [Single Collection Inheritance][1]
74
+ because the polymorphic values have significantly different behaviours and attributes. The following example shows how to do this:
71
75
 
72
- This method can be useful if you need to store long lists of ids, but don't want to incur any additional complexity and storage by storing
73
- the collection name as well. However, because we only use one nibble, this method can only be used for 16 distinct collections.
76
+ ```
77
+ class Person
78
+ include MongoMapper::Document
79
+ plugin MmUsesUuid
80
+
81
+ key :name
82
+ key :age
83
+
84
+ key :interest_ids, Array
85
+ many :interests, :in => :interest_ids, :class_name => 'UuidModel' #values can be a Group or a Person
86
+
87
+ belongs_to :group
88
+
89
+ uuid_lsn 0xf
90
+ end
91
+ ```
74
92
 
75
93
  Copyright (c) 2011 PeepAll Ltd, released under the MIT license
94
+
95
+
96
+ [1]: http://mongomapper.com/documentation/plugins/single-collection-inheritance.html
@@ -1,3 +1,3 @@
1
1
  module MmUsesUuid
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.8"
3
3
  end
data/lib/mm_uses_uuid.rb CHANGED
@@ -19,11 +19,26 @@ class BsonUuid
19
19
  end
20
20
  end
21
21
 
22
- module MongoMapper
22
+ class MongoMapper::Plugins::Associations::InArrayProxy
23
+
24
+ def find_target
25
+ return [] if ids.blank?
26
+ if klass == UuidModel
27
+ UuidModel.find(*criteria[:_id])
28
+ else
29
+ all
30
+ end
31
+ end
32
+
33
+ end
34
+
35
+ class UuidModel
36
+
37
+ include MongoMapper::Document
23
38
 
24
39
  @@lsn_class ||= []
25
40
 
26
- def self.find_by_uuid(*args)
41
+ def self.find(*args)
27
42
  args.flatten!
28
43
  ids_by_class = {}
29
44
  args.each do |id|
@@ -35,7 +50,7 @@ module MongoMapper
35
50
  end
36
51
  ids_by_class.map {|klass, ids| klass.find(ids)} .flatten
37
52
  end
38
-
53
+
39
54
  end
40
55
 
41
56
  module MmUsesUuid
@@ -75,7 +90,7 @@ module MmUsesUuid
75
90
  end
76
91
 
77
92
  def add_class_lsn(klass, lsn_integer)
78
- MongoMapper.class_eval "@@lsn_class[#{lsn_integer}] = #{klass}"
93
+ UuidModel.class_eval "@@lsn_class[#{lsn_integer}] = #{klass}"
79
94
  end
80
95
 
81
96
  end
@@ -111,7 +126,7 @@ module MmUsesUuid
111
126
 
112
127
  def make_uuid
113
128
  uuid = SecureRandom.uuid.gsub!('-', '')
114
- lsn_class = MongoMapper.class_variable_get('@@lsn_class')
129
+ lsn_class = UuidModel.class_variable_get('@@lsn_class')
115
130
  if replacement_lsn = lsn_class.index(self.class)
116
131
  uuid[-1] = replacement_lsn.to_s(16)
117
132
  end
@@ -21,6 +21,9 @@ describe MmUsesUuid do
21
21
  key :name
22
22
  key :age
23
23
 
24
+ key :interest_ids, Array
25
+ many :interests, :in => :interest_ids, :class_name => 'UuidModel' # this allows many-to-many polymorphic interests without the need for groups and people to be stored in a single collection
26
+
24
27
  belongs_to :group
25
28
 
26
29
  uuid_lsn 0xf
@@ -43,7 +46,11 @@ describe MmUsesUuid do
43
46
  "33333333-3333-4333-y333-333333333333"
44
47
  )
45
48
  @group = Group.create(name: 'mongo_mapper fanclub')
46
- @person = Person.create(name: 'Jon', age: 33)
49
+
50
+ @person = Person.new(name: 'Jon', age: 33)
51
+ @person.interests << @group
52
+ @person.interests << @person #I'm very self-centred
53
+ @person.save
47
54
  end
48
55
 
49
56
  it "should cause newly initialized objects to have a BSON::Binary uuid" do
@@ -57,7 +64,12 @@ describe MmUsesUuid do
57
64
  end
58
65
 
59
66
  it "should perform a find on the right collection if MongoMapper.find is used" do
60
- MongoMapper.find_by_uuid(@person.id, @group.id).map(&:name).should include(@person.name, @group.name)
67
+ UuidModel.find(@person.id, @group.id).map(&:name).should include(@person.name, @group.name)
68
+ end
69
+
70
+ it "should support polymorphic many to many associations that use LSN encoding" do
71
+ person = Person.find_by_name 'Jon'
72
+ person.interests.map(&:name).should include(@person.name, @group.name)
61
73
  end
62
74
 
63
75
  it "should not set a new uuid if one as passed as a param" do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 6
9
- version: 0.0.6
8
+ - 8
9
+ version: 0.0.8
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jonathan Chambers