first_after_created_at 0.0.1 → 0.0.2
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 +4 -4
- data/lib/first_after_created_at/version.rb +1 -1
- data/lib/first_after_created_at.rb +24 -30
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 205c6fbed97f1fdcd252bac9639c675ff593784c
|
4
|
+
data.tar.gz: 21f6e6be86b437c3fc27d1db0fe44b3dec2036e2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 16291ec36459c4a89cee2adb15036b0765cfd0fd249a234c82ef873680522b2797ddbc57c4dbfce55b5b62a224208cf6cdc84086a2191d842c6fb0b3fc945c84
|
7
|
+
data.tar.gz: 0ab38332154389810490d66150bd791470982d149e79b816377e45f2a520042212e7e33a94be3b7627301fedc9d8ebaaf2120ad17ab1d129b8198f7bc2157f37
|
@@ -1,44 +1,38 @@
|
|
1
1
|
require 'active_record'
|
2
2
|
|
3
|
+
# This module extends ActiveRecord::Base to provide the
|
4
|
+
# .first_after_created_at(date) and .first_after_id(id) functions
|
3
5
|
module FirstAfterCreatedAt
|
4
6
|
def first_after_created_at(date)
|
5
|
-
|
6
|
-
|
7
|
-
jump_size = (
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
first = select(:id, :created_at).first
|
8
|
+
last = select(:id, :created_at).last
|
9
|
+
jump_size = (last.id - first.id) / 2
|
10
|
+
best = _best(last, _best(first, nil, date), date)
|
11
|
+
center = _first_after_id(first.id + jump_size)
|
12
|
+
|
11
13
|
while jump_size != 0
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
# move forward
|
16
|
-
first_after_id(center.id + jump_size)
|
17
|
-
else
|
18
|
-
# move backwards
|
19
|
-
first_after_id(center.id - jump_size)
|
20
|
-
end
|
21
|
-
|
22
|
-
|
23
|
-
distance = new_center.created_at - date
|
24
|
-
if distance == 0
|
25
|
-
return new_center
|
26
|
-
elsif distance > 0 && distance < best_distance
|
27
|
-
best = new_center
|
28
|
-
best_distance = distance
|
29
|
-
end
|
30
|
-
|
31
|
-
break if new_center.id == center.id
|
14
|
+
jump_size /= 2
|
15
|
+
direction = center.created_at < date ? 1 : -1 # moving forward or backward
|
16
|
+
new_center = _first_after_id(center.id + jump_size * direction)
|
32
17
|
|
33
|
-
|
18
|
+
best = _best(new_center, best, date)
|
34
19
|
|
35
|
-
|
20
|
+
center = new_center
|
36
21
|
end
|
37
22
|
|
38
|
-
best
|
23
|
+
find(best.id) if best
|
24
|
+
end
|
25
|
+
|
26
|
+
def _best(center, best, date)
|
27
|
+
distance = center.created_at - date
|
28
|
+
if distance >= 0 && (!best || distance < (best.created_at - date))
|
29
|
+
center
|
30
|
+
else
|
31
|
+
best
|
32
|
+
end
|
39
33
|
end
|
40
34
|
|
41
|
-
def
|
35
|
+
def _first_after_id(id)
|
42
36
|
select(:id, :created_at).where('id >= ?', id).order('id asc').first
|
43
37
|
end
|
44
38
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: first_after_created_at
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Glass
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-07-
|
11
|
+
date: 2017-07-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|