ar-simple-idmap 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
@@ -26,23 +26,51 @@ module ActiveRecord
26
26
  end
27
27
 
28
28
  private
29
+ def fetch_from_map(map, ids)
30
+ result, not_cached = [], []
31
+ ids.each do |id|
32
+ if ( obj = map[id] )
33
+ result << obj
34
+ else
35
+ not_cached << id
36
+ end
37
+ end
38
+ unless not_cached.empty?
39
+ add = yield not_cached
40
+ result.concat( add ) if add
41
+ end
42
+ result
43
+ end
29
44
 
30
45
  def find_with_identity_map( *args )
31
46
  if_id_map do |map|
32
- unless args.size > 1 && args[1].values.any?
33
- args0 = args[0]
34
- if args0.is_a?(Array)
35
- result, to_find = [], []
36
- args0.each do |id|
37
- if (obj = map[id])
38
- result << obj
39
- else
40
- to_find << id
41
- end
42
- end
43
- result + find_without_identity_map( to_find )
47
+ from_arg0 = args.size == 1 ||
48
+ args[1].is_a?(Hash) && !args[1].values.any?
49
+ from_condition_ids = !from_arg0 &&
50
+ (args[0] == :all || args[0] == :first)
51
+ args.size == 2 && args[1].is_a?(Hash) &&
52
+ args[1].all?{|key, value| key == :conditions || value.blank?} &&
53
+ args[1][:conditions].keys == [:id]
54
+ if from_arg0 || from_condition_ids
55
+ ids = from_arg0 ? args[0] : args[1][:conditions][:id]
56
+ if ids.is_a?(Array)
57
+ if from_arg0
58
+ fetch_from_map( map, ids, &method(:find_without_identity_map) )
59
+ elsif args[0] == :all
60
+ fetch_from_map( map, ids ){|not_cached|
61
+ find_without_identity_map(:all, {:conditions=>{:id=>not_cached}})
62
+ }
63
+ elsif args[0] == :first
64
+ to_find = nil
65
+ result = fetch_from_map( map, ids ){|not_cached| to_find = not_cached; nil}
66
+ unless result.empty?
67
+ result.first
68
+ else
69
+ find_without_identity_map(:first, {:conditions=>{:id=>to_find}})
70
+ end
71
+ end
44
72
  else
45
- map[args0] || find_without_identity_map(args0)
73
+ map[ids]
46
74
  end
47
75
  end
48
76
  end || find_without_identity_map(*args)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar-simple-idmap
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 2
10
- version: 0.1.2
9
+ - 3
10
+ version: 0.1.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sokolov Yura aka funny_falcon