state_of_the_nation 1.1.4 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/state_of_the_nation/query_string.rb +2 -2
- data/lib/state_of_the_nation/version.rb +1 -1
- data/lib/state_of_the_nation.rb +26 -32
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f9953a5747485077b6b886669e70f2e652ec75837f2fc3a18e9593f208953d47
|
4
|
+
data.tar.gz: 9ffe1c9d237754ce9c2bdfbd8385e95e0b5ccbaa7eb8169dc10e4313a69e4253
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bcbf936777c4cca79342052f6092ac1722a1cc006017e1abc7b9cd16c70c462ea06d83b05d7fc6123b113c13bc68b670f0cdee9b63ab77c68b80b6f570b8f505
|
7
|
+
data.tar.gz: e3d8a957ae397887f048bc3f105564d2e29a301c150a0233c9116b36f256c4e2ec1e994c983cc506830f6d7c6a0a7bcb4b473d7c6adff0b753fc5a8d9799e1c6
|
@@ -10,13 +10,13 @@ module StateOfTheNation
|
|
10
10
|
active_scope: "(%{finish_key} IS NULL OR %{finish_key} > ?::timestamp) AND %{start_key} <= ?::timestamp",
|
11
11
|
less_than: "(%{start_key} < ?::timestamp)",
|
12
12
|
greater_than_or_null: "(%{finish_key} > ?::timestamp) OR (%{finish_key} IS NULL)",
|
13
|
-
|
13
|
+
start_and_finish_not_equal_or_are_null: "(%{start_key} != %{finish_key}) OR (%{start_key} IS NULL) OR (%{finish_key} IS NULL)",
|
14
14
|
},
|
15
15
|
mysql: {
|
16
16
|
active_scope: "(%{finish_key} IS NULL OR %{finish_key} > ?) AND %{start_key} <= ?",
|
17
17
|
less_than: "(%{start_key} < ?)",
|
18
18
|
greater_than_or_null: "(%{finish_key} > ?) OR (%{finish_key} IS NULL)",
|
19
|
-
|
19
|
+
start_and_finish_not_equal_or_are_null: "(%{start_key} != %{finish_key}) OR (%{start_key} IS NULL) OR (%{finish_key} IS NULL)"
|
20
20
|
}
|
21
21
|
}[appropriate_db_type(klass)]
|
22
22
|
end
|
data/lib/state_of_the_nation.rb
CHANGED
@@ -27,32 +27,34 @@ module StateOfTheNation
|
|
27
27
|
@finish_key = finish_key
|
28
28
|
|
29
29
|
define_method "active?" do |time = Time.now.utc|
|
30
|
-
(finish.blank? ||
|
30
|
+
(finish.blank? || finish > time) && start <= time
|
31
|
+
end
|
32
|
+
|
33
|
+
define_method "active_in_interval?" do |interval_start, interval_end|
|
34
|
+
record_start = start
|
35
|
+
record_end = finish
|
36
|
+
if ignore_empty && record_start == record_end
|
37
|
+
false
|
38
|
+
elsif interval_start.nil? && interval_end.nil?
|
39
|
+
true
|
40
|
+
elsif interval_start == interval_end
|
41
|
+
active?(interval_start)
|
42
|
+
elsif interval_start.nil?
|
43
|
+
record_start < interval_end
|
44
|
+
elsif interval_end.nil?
|
45
|
+
record_end.nil? || record_end > interval_start
|
46
|
+
elsif record_end.nil?
|
47
|
+
interval_end > record_start
|
48
|
+
else
|
49
|
+
record_start < interval_end && record_end > interval_start
|
50
|
+
end
|
31
51
|
end
|
32
52
|
|
33
53
|
scope :active, lambda { |time = Time.now.utc|
|
34
|
-
where(QueryString.query_for(:active_scope, self),
|
54
|
+
where(QueryString.query_for(:active_scope, self), time, time)
|
35
55
|
}
|
36
56
|
end
|
37
57
|
|
38
|
-
private def round_if_should(time)
|
39
|
-
return time if !should_round_timestamps?
|
40
|
-
time.respond_to?(:round) ? time.round : time
|
41
|
-
end
|
42
|
-
|
43
|
-
private def should_round_timestamps?
|
44
|
-
# MySQL datetime fields do not support millisecond resolution while
|
45
|
-
# PostgreSQL's do. To prevent issues with near identical timestamps not
|
46
|
-
# comparing as expected in .active? methods we'll choose the resolution
|
47
|
-
# appropriate for the database adapter backing the model.
|
48
|
-
case self.connection.adapter_name
|
49
|
-
when /PostgreSQL/
|
50
|
-
false
|
51
|
-
else
|
52
|
-
true
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
58
|
self
|
57
59
|
end
|
58
60
|
|
@@ -129,8 +131,9 @@ module StateOfTheNation
|
|
129
131
|
# find competing records which *finish* being active AFTER this record *starts* being active
|
130
132
|
# (or ones which are not set to finish being active)
|
131
133
|
|
132
|
-
records = records.where(QueryString.query_for(:
|
134
|
+
records = records.where(QueryString.query_for(:start_and_finish_not_equal_or_are_null, self.class)) if ignore_empty
|
133
135
|
# exclude records where there is no difference between start and finish dates
|
136
|
+
# (we need to deliberately not filter out records with null value keys with this comparison as not equal comparisons to null are always deemed null/false)
|
134
137
|
|
135
138
|
records
|
136
139
|
end
|
@@ -142,12 +145,12 @@ module StateOfTheNation
|
|
142
145
|
|
143
146
|
def start
|
144
147
|
return unless start_key.present?
|
145
|
-
return
|
148
|
+
return self.send(start_key) || Time.now.utc
|
146
149
|
end
|
147
150
|
|
148
151
|
def finish
|
149
152
|
return unless finish_key.present?
|
150
|
-
|
153
|
+
self.send(finish_key)
|
151
154
|
end
|
152
155
|
|
153
156
|
def bad_configuration?
|
@@ -175,13 +178,4 @@ module StateOfTheNation
|
|
175
178
|
def ignore_empty
|
176
179
|
self.class.ignore_empty
|
177
180
|
end
|
178
|
-
|
179
|
-
def should_round_timestamps?
|
180
|
-
self.class.send(:should_round_timestamps?)
|
181
|
-
end
|
182
|
-
|
183
|
-
def round_if_should(time)
|
184
|
-
return time if time.nil?
|
185
|
-
self.class.send(:round_if_should, time)
|
186
|
-
end
|
187
181
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: state_of_the_nation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Patrick O'Doherty
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2022-03-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|