stator 0.1.4 → 0.1.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6975d1e52854c059916ff592dfa6aa0b6b1ca38e
4
- data.tar.gz: 46770289d79b6b2f4632189aa2cf8e9d8b1cbb58
3
+ metadata.gz: 813eb5f387c3f467935894391e29ec246d9591cf
4
+ data.tar.gz: 0c345b053ec9402ee9466225894411b7baf3d205
5
5
  SHA512:
6
- metadata.gz: 538e813262272898e14939b345f5764905289dc58a56bd881a7566b2d595ea00d979febeb439243a544d647612866e9973ddbec013903419a6c318fa48712c53
7
- data.tar.gz: fd4b003fe8dd0347e2309803b600c12630f164462f003d643fe06d32bbf15a8c327c650e261f098607924c056fc973f29dc88e1bfb8f21821303661feb070005
6
+ metadata.gz: c0f5a3887ea6ec9ccbb5085d5bacd216cb0af88c56fae7dfdccfddd6d96d97cc2b1b25ee3187c1a8dcdd8d61dd9bdf647425103041e0f8d8ecd7801f80c9bb61
7
+ data.tar.gz: 7c5c5b3995d047c798cf9f52d7dec00205a2798e4cdd5a3ae227e14448fba752d06c026c808ecff66e8b747de9de4fc2b9bba9897ede59f3b75290fd2525846e
@@ -77,20 +77,48 @@ module Stator
77
77
 
78
78
  state_at = @record.send("#{state}_#{@machine.field}_at")
79
79
 
80
+ # if we've never been in the state, the answer is no
80
81
  return false if state_at.nil?
82
+
83
+ # if we came into this state later in life, the answer is no
81
84
  return false if state_at > t
82
85
 
83
- min_next = @machine.states.map do |s|
86
+ all_states = @machine.states.reverse
87
+
88
+ # grab all the states and their timestamps that occur on or after state_at and on or before the time in question
89
+ later_states = all_states.map do |s|
90
+
84
91
  next if state == s
92
+
85
93
  at = @record.send("#{s}_#{@machine.field}_at")
86
- at && at >= state_at ? at : nil
87
- end.compact.sort[0]
88
94
 
89
- return true if min_next.nil?
90
- return true if min_next >= t
95
+ next if at.nil?
96
+ next if at < state_at
97
+ next if at > t
98
+
99
+ { state: s, at: at }
100
+ end.compact
101
+
102
+ # if there were no states on or after the state_at, the answer is yes
103
+ return true if later_states.empty?
104
+
105
+ # grab the states that were present at the lowest timestamp
106
+ later_groups = later_states.group_by{|s| s[:at] }
107
+ later_group_key = later_groups.keys.sort[0]
108
+ later_states = later_groups[later_group_key]
109
+
110
+ # if the lowest timestamp is the same as the state's timestamp, evaluate based on state index
111
+ if later_states[0][:at] == state_at
112
+ return all_states.index(state) < all_states.index(later_states[0][:state])
113
+ end
114
+
91
115
  false
92
116
  end
93
117
 
118
+ def likely_state_at(t)
119
+ @machine.states.reverse.detect{|s| in_state_at?(s,t) }
120
+ end
121
+
94
122
 
95
123
  protected
96
124
 
data/lib/stator/model.rb CHANGED
@@ -44,6 +44,11 @@ module Stator
44
44
  machine.integration(self).in_state_at?(state, t)
45
45
  end
46
46
 
47
+ def likely_state_at(t, namespace = '')
48
+ machine = self._stator(namespace)
49
+ machine.integration(self).likely_state_at(t)
50
+ end
51
+
47
52
  protected
48
53
 
49
54
 
@@ -1,7 +1,7 @@
1
1
  module Stator
2
2
  MAJOR = 0
3
3
  MINOR = 1
4
- PATCH = 4
4
+ PATCH = 5
5
5
  PRERELEASE = nil
6
6
 
7
7
  VERSION = [MAJOR, MINOR, PATCH, PRERELEASE].compact.join('.')
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Nelson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-29 00:00:00.000000000 Z
11
+ date: 2016-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord