picky-client 2.1.1 → 2.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/picky-client/convenience.rb +13 -13
- metadata +1 -1
@@ -1,5 +1,5 @@
|
|
1
1
|
module Picky
|
2
|
-
|
2
|
+
|
3
3
|
# Use this class to extend the hash that the client returns.
|
4
4
|
#
|
5
5
|
module Convenience
|
@@ -9,7 +9,7 @@ module Picky
|
|
9
9
|
def empty?
|
10
10
|
allocations.empty?
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
# Returns the topmost n results.
|
14
14
|
# (Note that not all ids are returned with the results. By default only maximally 20.)
|
15
15
|
#
|
@@ -21,7 +21,7 @@ module Picky
|
|
21
21
|
allocations.each { |allocation| allocation[4].each { |id| break if ids.size > limit; ids << id } }
|
22
22
|
ids
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
# Removes all ids of each allocation.
|
26
26
|
#
|
27
27
|
def clear_ids
|
@@ -43,7 +43,7 @@ module Picky
|
|
43
43
|
def total
|
44
44
|
@total || @total = self[:total]
|
45
45
|
end
|
46
|
-
|
46
|
+
|
47
47
|
# Populates the ids with (rendered) model instances.
|
48
48
|
#
|
49
49
|
# Give it an AR class and options for the find and it
|
@@ -60,28 +60,28 @@ module Picky
|
|
60
60
|
#
|
61
61
|
def populate_with model_class, amount = 20, options = {}, &block
|
62
62
|
the_ids = ids amount
|
63
|
-
|
63
|
+
|
64
64
|
objects = model_class.find the_ids, options
|
65
|
-
|
65
|
+
|
66
66
|
# Put together a mapping.
|
67
67
|
#
|
68
68
|
mapped_entries = objects.inject({}) do |mapped, entry|
|
69
69
|
mapped[entry.id] = entry if entry
|
70
70
|
mapped
|
71
71
|
end
|
72
|
-
|
72
|
+
|
73
73
|
# Preserves the order
|
74
74
|
#
|
75
75
|
objects = the_ids.map { |id| mapped_entries[id] }
|
76
|
-
|
76
|
+
|
77
77
|
objects.collect! &block if block_given?
|
78
|
-
|
78
|
+
|
79
79
|
amend_ids_with objects
|
80
80
|
clear_ids
|
81
|
-
|
81
|
+
|
82
82
|
objects
|
83
83
|
end
|
84
|
-
|
84
|
+
|
85
85
|
# Returns either
|
86
86
|
# * the rendered entries, if you have used #populate_with _with_ a block
|
87
87
|
# OR
|
@@ -99,7 +99,7 @@ module Picky
|
|
99
99
|
entries
|
100
100
|
end
|
101
101
|
end
|
102
|
-
|
102
|
+
|
103
103
|
# The ids need to come in the order which the ids were returned by the ids method.
|
104
104
|
#
|
105
105
|
def amend_ids_with entries # :nodoc:
|
@@ -112,6 +112,6 @@ module Picky
|
|
112
112
|
end
|
113
113
|
end
|
114
114
|
end
|
115
|
-
|
115
|
+
|
116
116
|
end
|
117
117
|
end
|