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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 97a7482f3c135b954fe912e2bb6b8948f4ba14596e282a91687aec9cbcb06b3d
4
- data.tar.gz: 4577435dca22e84ded9236bc52b94cc24d02681973697623d5bca3c884034d6b
3
+ metadata.gz: f9953a5747485077b6b886669e70f2e652ec75837f2fc3a18e9593f208953d47
4
+ data.tar.gz: 9ffe1c9d237754ce9c2bdfbd8385e95e0b5ccbaa7eb8169dc10e4313a69e4253
5
5
  SHA512:
6
- metadata.gz: d00dd78d49cd44f1c79ad9c316d1e4843bb255f5b7728f0cb813bd656d8b3ff794c3d5dffa4013bf3d9e4805dda858f01bace09557c241e0b69d3869b06fd3f7
7
- data.tar.gz: 11dbf26dbddd36bc93c408f6679c9980044c990c4ba4990298dd86ca1633687d5bfc0df9b13b38ad067d12645a3b001989e2356a175405ea3ed34a1cc354f5ed
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
- start_and_finish_not_equal: "(%{start_key} != %{finish_key})",
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
- start_and_finish_not_equal: "(%{start_key} != %{finish_key})"
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
@@ -1,3 +1,3 @@
1
1
  module StateOfTheNation
2
- VERSION = "1.1.4"
2
+ VERSION = "2.0.0"
3
3
  end
@@ -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? || round_if_should(finish) > round_if_should(time)) && round_if_should(start) <= round_if_should(time)
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), round_if_should(time), round_if_should(time))
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(:start_and_finish_not_equal, self.class)) if ignore_empty
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 round_if_should(self.send(start_key) || Time.now.utc)
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
- round_if_should(self.send(finish_key))
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: 1.1.4
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: 2020-02-19 00:00:00.000000000 Z
12
+ date: 2022-03-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler