activerecord-collections 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0d82df13bf116718055700ca2c6e5ac38822bc1e
4
- data.tar.gz: 6ab953c834c0eeea548bb42489fb65f7e4acc44d
3
+ metadata.gz: a957127ae2a21f3a1b1d86589127c32b61ea74ec
4
+ data.tar.gz: ea061b5fa0357a66cd1f93d63332b297d25ac8f9
5
5
  SHA512:
6
- metadata.gz: 277865f754c3ecfa851b060c43dc4cce68d5ae5e23f41bcb294b6499c726820928f06cd76cd02f04001e434fb24376f10b9660a6182d099af3fcd6c1c6a82516
7
- data.tar.gz: 196da9b376e13ff8074b6c1c5e7bc8b4e79de781346d7fc929f6b07a996630ff7f4c90a25c846e649a2af691fc36fb11de2a4018a538bcda5eb307fcbd60bd49
6
+ metadata.gz: 9b05f147793bc3239373ef8c0ecc232650f6e8972d1ee7d98942f1259904e0b3451caf3c45422dae6167ecef2708968e06d7a80a0b8e5f7904aaf98a0c52038f
7
+ data.tar.gz: ea45a2d8364d10f0c1d9a998ca9a372db81ab7098fc39ceffc71cc2732491891bf673270cea2c8fb0358e9f7a8bc5b98bfcd36d742ea8f35ac21d865ab4c4e6e
@@ -9,6 +9,11 @@ module ActiveRecord
9
9
  attr_reader :relation, :options
10
10
 
11
11
  class << self
12
+ attr_reader :collections
13
+ def inherited(subclass)
14
+ (@collections ||= []) << subclass
15
+ end
16
+
12
17
  def collectable(klass=nil)
13
18
  unless klass.nil?
14
19
  raise ArgumentError, "The collection model must inherit from ActiveRecord::Base" unless klass.ancestors.include?(ActiveRecord::Base)
@@ -57,20 +57,20 @@ module ActiveRecord
57
57
  end
58
58
  alias_method :batched?, :is_batched?
59
59
 
60
- def batch(btch=1)
61
- dup.batch!(btch)
60
+ def batch(batch_size: nil)
61
+ dup.batch!(batch_size: batch_size)
62
62
  end
63
63
 
64
- def batch!(btch=1)
65
- batchify!(btch, default_batch_size)
64
+ def batch!(batch_size: nil)
65
+ batchify!(1, (batch_size || limit_value || default_batch_size))
66
66
  end
67
67
 
68
- def per_batch(bs=nil)
68
+ def per_batch(bs)
69
69
  dup.per_batch!(bs)
70
70
  end
71
71
 
72
- def per_batch!(bs=nil)
73
- batchify!(current_batch, (bs || default_batch_size))
72
+ def per_batch!(bs)
73
+ batchify!(current_batch, bs)
74
74
  end
75
75
 
76
76
  def batchify!(btch, bs)
@@ -80,11 +80,11 @@ module ActiveRecord
80
80
  end
81
81
 
82
82
  def total_batches
83
- (total_count.to_f / (relation.limit_value || total_count).to_f).ceil
83
+ (total_count.to_f / (limit_value || total_count).to_f).ceil
84
84
  end
85
85
 
86
86
  def current_batch
87
- (relation.offset_value.to_i / (relation.limit_value || 1)) + 1
87
+ (offset_value.to_i / (limit_value || 1)) + 1
88
88
  end
89
89
 
90
90
  def batch_size
@@ -134,18 +134,16 @@ module ActiveRecord
134
134
  alias_method :in_batches, :as_batches
135
135
 
136
136
  def each_batch(&block)
137
- batch!
138
-
139
137
  if total_batches <= 1
140
- yield to_a if block_given?
141
- return [to_a]
138
+ yield dup.as_batch if block_given?
139
+ return [dup.as_batch]
142
140
  end
143
141
 
144
- first_batch!
145
142
  batched = []
143
+ first_batch!
146
144
  total_batches.times do
147
- batched << to_a
148
- yield to_a if block_given?
145
+ batched << dup.as_batch
146
+ yield dup.as_batch if block_given?
149
147
  next_batch!
150
148
  end
151
149
  first_batch!
@@ -153,16 +151,14 @@ module ActiveRecord
153
151
  end
154
152
 
155
153
  def batch_map(&block)
156
- batch!
157
-
158
154
  if total_batches <= 1
159
- return (block_given? ? yield(to_a) : to_a)
155
+ return (block_given? ? yield(dup.as_batch) : dup.as_batch)
160
156
  end
161
157
 
162
- first_batch!
163
158
  batched = []
159
+ first_batch!
164
160
  total_batches.times do
165
- batched << (block_given? ? yield(to_a) : to_a)
161
+ batched << (block_given? ? yield(dup.as_batch) : dup.as_batch)
166
162
  next_batch!
167
163
  end
168
164
  first_batch!
@@ -178,7 +174,7 @@ module ActiveRecord
178
174
  end
179
175
 
180
176
  def first_batch!
181
- batch!(1)
177
+ batchify!(1, batch_size)
182
178
  end
183
179
 
184
180
  def next_batch?
@@ -190,7 +186,7 @@ module ActiveRecord
190
186
  end
191
187
 
192
188
  def next_batch!
193
- batch!(current_batch + 1) if next_batch?
189
+ batchify!(current_batch + 1, batch_size)
194
190
  end
195
191
 
196
192
  def prev_batch?
@@ -202,7 +198,7 @@ module ActiveRecord
202
198
  end
203
199
 
204
200
  def prev_batch!
205
- batch!(current_batch - 1) if prev_batch?
201
+ batchify!(current_batch - 1, batch_size)
206
202
  end
207
203
 
208
204
  def last_batch
@@ -210,7 +206,7 @@ module ActiveRecord
210
206
  end
211
207
 
212
208
  def last_batch!
213
- batch!(total_batches)
209
+ batchify!(total_batches, batch_size)
214
210
  end
215
211
  end
216
212
  end
@@ -2,16 +2,29 @@ module ActiveRecord
2
2
  module Collections
3
3
  module Collectable
4
4
  module Model
5
+ def collection_class(klass=nil)
6
+ @collection_class = klass unless klass.nil?
7
+ @collection_class
8
+ end
9
+
10
+ def kollektion
11
+ @collection_class || ActiveRecord::Collection.collections.to_a.select { |c| c.collectable == self }.first || ActiveRecord::Collection
12
+ end
13
+
5
14
  def collection(*criteria)
6
- ActiveRecord::Collection.new(self, *criteria)
15
+ kollektion.new(self, *criteria)
7
16
  end
8
17
  alias_method :collect, :collection
9
18
  end
10
19
 
11
20
  module Relation
21
+ def kollektion
22
+ klass.kollektion
23
+ end
24
+
12
25
  def collection
13
26
  # do this with a hash so that we don't cause the relation query to execute
14
- ActiveRecord::Collection.from_hash({
27
+ kollektion.from_hash({
15
28
  klass: klass,
16
29
  select: select_values,
17
30
  distinct: distinct_value,
@@ -19,7 +32,8 @@ module ActiveRecord
19
32
  references: references_values,
20
33
  includes: includes_values,
21
34
  where: where_values.map { |v| v.is_a?(String) ? v : v.to_sql },
22
- order: order_values.map { |v| v.is_a?(String) ? v : v.to_sql },
35
+ group: group_values.map { |v| (v.is_a?(String) || v.is_a?(Symbol)) ? v : v.to_sql },
36
+ order: order_values.map { |v| (v.is_a?(String) || v.is_a?(Symbol)) ? v : v.to_sql },
23
37
  bind: bind_values.map { |b| {name: b.first.name, value: b.last} },
24
38
  limit: limit_value,
25
39
  offset: offset_value
@@ -66,8 +66,8 @@ module ActiveRecord
66
66
  protected
67
67
 
68
68
  def call_on_records(meth, *args)
69
- return batch_map do |batch|
70
- if model.columns.map(&:name).include?(meth.to_s) && !batch.loaded?
69
+ return flat_batch_map do |batch|
70
+ if collectable.columns.map(&:name).include?(meth.to_s) && !batch.loaded?
71
71
  batch.pluck(meth)
72
72
  else
73
73
  batch.map { |record| record.send(meth, *args) }
@@ -76,8 +76,8 @@ module ActiveRecord
76
76
  end
77
77
 
78
78
  def records_respond_to?(meth, include_private=false)
79
- model.public_instance_methods.include?(meth) ||
80
- (include_private && model.private_instance_methods.include?(meth)) ||
79
+ collectable.public_instance_methods.include?(meth) ||
80
+ (include_private && collectable.private_instance_methods.include?(meth)) ||
81
81
  (!records.nil? && records.loaded? && records.first.respond_to?(meth, include_private))
82
82
  end
83
83
 
@@ -53,7 +53,6 @@ module ActiveRecord
53
53
  end
54
54
 
55
55
  def select!(*args)
56
- #reset!
57
56
  @relation = relation.select(*args)
58
57
  self
59
58
  end
@@ -63,7 +62,6 @@ module ActiveRecord
63
62
  end
64
63
 
65
64
  def distinct!(bool=true)
66
- #reset!
67
65
  @relation = relation.distinct(bool)
68
66
  self
69
67
  end
@@ -73,8 +71,7 @@ module ActiveRecord
73
71
  end
74
72
 
75
73
  def where!(*args, &block)
76
- #reset!
77
- relation.where!(*args, &block)
74
+ @relation = relation.where(*args, &block)
78
75
  self
79
76
  end
80
77
 
@@ -83,7 +80,6 @@ module ActiveRecord
83
80
  end
84
81
 
85
82
  def not!(*args, &block)
86
- #reset!
87
83
  @relation = relation.where.not(*args, &block)
88
84
  self
89
85
  end
@@ -93,7 +89,6 @@ module ActiveRecord
93
89
  end
94
90
 
95
91
  def or!(*args, &block)
96
- #reset!
97
92
  @relation = relation.or.where(*args, &block)
98
93
  self
99
94
  end
@@ -103,18 +98,25 @@ module ActiveRecord
103
98
  end
104
99
 
105
100
  def order!(*args, &block)
106
- #reset!(false)
107
101
  relation.order!(*args, &block)
108
102
  self
109
103
  end
110
104
 
105
+ def group(*args)
106
+ dup.group!(*args)
107
+ end
108
+
109
+ def group!(*args)
110
+ @relation = relation.group(*args)
111
+ self
112
+ end
113
+
111
114
  def limit(*args, &block)
112
115
  dup.limit!(*args, &block)
113
116
  end
114
117
 
115
118
  def limit!(*args, &block)
116
- #reset!
117
- relation.limit!(*args, &block)
119
+ @relation = relation.limit(*args, &block)
118
120
  self
119
121
  end
120
122
 
@@ -123,8 +125,7 @@ module ActiveRecord
123
125
  end
124
126
 
125
127
  def offset!(*args, &block)
126
- #reset!
127
- relation.offset!(*args, &block)
128
+ @relation = relation.offset(*args, &block)
128
129
  self
129
130
  end
130
131
 
@@ -133,8 +134,7 @@ module ActiveRecord
133
134
  end
134
135
 
135
136
  def joins!(*args)
136
- #reset!
137
- relation.joins!(*args)
137
+ @relation = relation.joins(*args)
138
138
  self
139
139
  end
140
140
 
@@ -143,8 +143,7 @@ module ActiveRecord
143
143
  end
144
144
 
145
145
  def includes!(*args)
146
- #reset!
147
- relation.includes!(*args)
146
+ @relation = relation.includes(*args)
148
147
  self
149
148
  end
150
149
 
@@ -153,8 +152,7 @@ module ActiveRecord
153
152
  end
154
153
 
155
154
  def references!(*table_names)
156
- #reset!
157
- relation.references!(*table_names)
155
+ @relation = relation.references(*table_names)
158
156
  self
159
157
  end
160
158
 
@@ -19,6 +19,7 @@ module ActiveRecord
19
19
  collection.references!(*hash[:references]) unless hash[:references].empty?
20
20
  collection.includes!(*hash[:includes]) unless hash[:includes].empty?
21
21
  collection.where!(*hash[:bind].map { |b| b[:value] }.unshift(hash[:where].join(" AND ").gsub(/\$\d/,'?'))) unless hash[:where].empty?
22
+ collection.group!(hash[:group]) unless hash[:group].empty?
22
23
  collection.order!(hash[:order]) unless hash[:order].empty?
23
24
  collection.limit!(hash[:limit]) unless hash[:limit].nil?
24
25
  collection.offset!(hash[:offset]) unless hash[:offset].nil?
@@ -38,7 +39,8 @@ module ActiveRecord
38
39
  references: references_values,
39
40
  includes: includes_values,
40
41
  where: where_values.map { |v| v.is_a?(String) ? v : v.to_sql },
41
- order: order_values.map { |v| v.is_a?(String) ? v : v.to_sql },
42
+ group: group_values.map { |v| (v.is_a?(String) || v.is_a?(Symbol)) ? v : v.to_sql },
43
+ order: order_values.map { |v| (v.is_a?(String) || v.is_a?(Symbol)) ? v : v.to_sql },
42
44
  bind: bind_values.map { |b| {name: b.first.name, value: b.last} }
43
45
  }
44
46
  if include_limit || try(:is_batch?)
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Collections
3
- VERSION = '0.0.8'
3
+ VERSION = '0.0.9'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-collections
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Rebec
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-31 00:00:00.000000000 Z
11
+ date: 2015-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord