ohm-identity_map 0.1.0 → 0.1.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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZThmYTc5ODlhMDVkOTVjNGUwOWE0NTJmNDhjZGVjZWI1ZjM5ZDBiZQ==
4
+ N2Q4MTQ0MGRiMTJlYTU5OWQ3MzdiMzc2ZTEzY2NiMDFhYjhmODJkOQ==
5
5
  data.tar.gz: !binary |-
6
- MDMxZjc2YjAyOTE0YzE3YzhiNjA3NTA0ODE5ODc4OWI5NTE5NzJhMA==
6
+ OGRhYjhhZTU5Mzg0ZjBjZjhhZjc1NmJjYTkwMmJkYzdkOTlhMWU5ZQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NDFmNDA4ZWM1N2I4ODM1NTlkY2U5MTNlMjQ3MDEyYzFlZDVjYzYwZmZjNTkx
10
- YWQ2NjA3NGIyNDc2MWY1MjhiNjRkNWZmYmJmYzE0NGUwMzAwZDhiZTRmM2Zj
11
- OTYzYmNjNmM5NjYxMDczMTBjOWZiNzFhNzA5NjNhYTU4ZWJiMzQ=
9
+ YjdhYWFhNGJmZGFlZTZmODBiZWI2MTYwMDFmYjBlM2QzZTU0ODM1ZjljODg3
10
+ NTA2ZjE0ZTk5OGJiZTQ1NzZlYjUzMjMwZTljYTIwMDAyMmEzY2UwN2M0NWVh
11
+ ZGU2NTJhZjBhZWM3MTVlYWYxMzQ5ZTIzZmQ2OTAzYmI0YjQ5Y2U=
12
12
  data.tar.gz: !binary |-
13
- YjMxMWFiMjRjZWMwNzRhNGUzOGJmNWNiZGQxMWY2MzlmNzYwMzdhNTFmNTMy
14
- NzYyZjJhMzNiYTNmMzVhN2I5ZGFmNDhlY2QzNDdhNzBlZWRlZWEzN2M5ZWRj
15
- MmYzMmNhZGQxYjgxZTc5Yjk2ZTM5YWU2YTU0Y2MyZDU0Zjc4NTU=
13
+ YWRjNGQwY2I2NmRmNDQ0NTgzODFiNjE1NWQ5MDQ4Mzc5ZDU1YTZjNzhlZDA2
14
+ NjkwM2I5ZDdjMmM3YTk1NTVhN2NkNTBiNmFjYWFhYWI5Y2E1NDk1MTVhYzk0
15
+ MTNlMTk2YWUyYjg0NGQ5MzI3OWFlNDRjMmQ0NzczMGU2MjUzZTQ=
data/CHANGELOG ADDED
@@ -0,0 +1,7 @@
1
+ 0.1.1 - 2013-04-19
2
+
3
+ * Fix confusing model classes.
4
+
5
+ 0.1.0 - 2013-04-19
6
+
7
+ * Initial release.
@@ -1,7 +1,7 @@
1
1
  require "ohm"
2
2
 
3
3
  module Ohm::IdentityMap
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
 
6
6
  def self.included(model)
7
7
  model.extend(Macros)
@@ -11,7 +11,9 @@ module Ohm::IdentityMap
11
11
 
12
12
  def [](id)
13
13
  if map = Thread.current[:_ohm_identity_map]
14
- map.fetch(id) { map[id] = original_loader(id) }
14
+ key = self.key[id]
15
+
16
+ map.fetch(key) { map.store(key, original_loader(id)) }
15
17
  else
16
18
  original_loader(id)
17
19
  end
@@ -24,7 +26,7 @@ module Ohm::IdentityMap
24
26
  missing_ids, missing_indices = [], []
25
27
 
26
28
  mapped = ids.map.with_index do |id, index|
27
- map.fetch(id) do
29
+ map.fetch(self.key[id]) do
28
30
  missing_ids << id
29
31
  missing_indices << index
30
32
  nil
@@ -33,7 +35,7 @@ module Ohm::IdentityMap
33
35
 
34
36
  original_fetch(missing_ids).each.with_index do |instance, index|
35
37
  mapped[missing_indices[index]] = instance
36
- map.store(missing_ids[index], instance)
38
+ map.store(self.key[missing_ids[index]], instance)
37
39
  end
38
40
 
39
41
  mapped
@@ -51,4 +51,28 @@ scope do
51
51
 
52
52
  assert_equal 2, comments.map(&:object_id).uniq.size
53
53
  end
54
+
55
+ test "Model#reference - identity map disabled" do
56
+ assert Comment[1].post.object_id != Comment[2].post.object_id
57
+ end
58
+
59
+ test "Model#reference - identity map enabled" do
60
+ posts = Ohm::Model.identity_map { [Comment[1].post, Comment[2].post] }
61
+
62
+ assert_equal 1, posts.map(&:object_id).uniq.size
63
+
64
+ orphan = Comment.create(body: "No post.")
65
+
66
+ assert_equal nil, orphan.post
67
+ end
68
+
69
+ test "does not confuse models" do
70
+ models = Ohm::Model.identity_map { [Comment[1], Post[1]] }
71
+
72
+ assert_equal 2, models.map(&:object_id).uniq.size
73
+
74
+ models = Ohm::Model.identity_map { Comment.fetch([1]) + Post.fetch([1]) }
75
+
76
+ assert_equal 2, models.map(&:object_id).uniq.size
77
+ end
54
78
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ohm-identity_map
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Educabilia
@@ -48,6 +48,7 @@ extensions: []
48
48
  extra_rdoc_files: []
49
49
  files:
50
50
  - .gitignore
51
+ - CHANGELOG
51
52
  - README.md
52
53
  - UNLICENSE
53
54
  - lib/ohm/identity_map.rb