lucid_works 0.6.5 → 0.6.6

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.
@@ -31,10 +31,6 @@ module LucidWorks
31
31
  end
32
32
  end
33
33
 
34
- def time_of_day
35
- Time.now
36
- end
37
-
38
34
  # predict when action will occur next if active at that time
39
35
  def next_start
40
36
  return start_time if reference_time <= start_time
@@ -54,14 +50,44 @@ module LucidWorks
54
50
 
55
51
  # Provide ability to override "now" explicitly for testing purposes
56
52
  def reference_time=(time); @reference_time = time; end
57
- def reference_time; @reference_time || Time.now.utc; end
53
+ def reference_time; @reference_time || Time.now.localtime; end
58
54
 
55
+ #
59
56
  # phantom attribute for compiling real start time and frequency from appropriate form data
57
+ # Allows the user to specify a schedule simply with repeat interval (hourly, daily, weekly)
58
+ # and time within the interval to run (e.g 5 past, or Tuesdays at 2:15)
59
+ #
60
+ # We have to figure out when the actual start time will be because we are not asking
61
+ # the user for this. It needs to be:
62
+ # * at the requested time relative to the interval chosen
63
+ # * as soon as possible
64
+ # * in the future
65
+ #
66
+ # This is obvious to humans, but when computing it, you run into the following problem. It
67
+ # happens with all interval sizes, but for the sake of example, we'll use weekly:
68
+ #
69
+ # Problem 1) If today is Thursday, and the user asks for "weekly on Wednesdays," Wednesday has
70
+ # already happened this week. We have to make sure we pick the nearest wednesday that is in
71
+ # the future
72
+ #
73
+ # Problem 2) If today is Tuesday, and the user asks for "weekly on Wednesdays,", the simple solution
74
+ # to problem 1 (start next week instead of this week) causes you to skip this Tuesday, even though
75
+ # it is valid and is what the user would expect
76
+ #
77
+ # The algorith to solve both problems at once is this:
78
+ # * From Time.now, back up to the beginning of the interval even if it is in the past.
79
+ # * Fast forward to the requested siimple start time
80
+ # * If you are still in the past, advance by one interval
81
+ #
60
82
  def schedule=(all_attributes)
61
83
  # convert to format accepted by Time.advance
62
84
  all_attributes['start'].to_options!
63
85
  all_attributes['start'].each{|k,v| all_attributes['start'][k]=v.to_i}
64
86
 
87
+ if all_attributes.keys.include?('active')
88
+ self.active = (all_attributes['active'].to_s.downcase == 'true')
89
+ end
90
+
65
91
  self.frequency = all_attributes['frequency']
66
92
  self.start_time =
67
93
  case all_attributes['frequency']
@@ -80,6 +106,19 @@ module LucidWorks
80
106
  raise "unexpected frequency encountered"
81
107
  end
82
108
  end
109
+
110
+ def hour
111
+ self.start_time.localtime.hour rescue nil
112
+ end
113
+
114
+ def min
115
+ self.start_time.localtime.min rescue nil
116
+ end
117
+
118
+ def day_of_week
119
+ # subract 1 because '%u' returns 1-7, and this is for indexing a ruby array of day names
120
+ self.start_time.localtime.strftime('%u').to_i - 1 rescue nil
121
+ end
83
122
  end
84
123
  end
85
124
  end
@@ -1,3 +1,3 @@
1
1
  module LucidWorks
2
- VERSION = "0.6.5"
2
+ VERSION = "0.6.6"
3
3
  end
@@ -1,16 +1,17 @@
1
1
  require 'spec_helper'
2
2
 
3
- class String
4
- def weekday
5
- Time.iso8601(self).strftime('%A')
3
+ # convenience methods for readability of these tests themselves
4
+ class String
5
+ def weekday
6
+ Time.iso8601(self).strftime('%A')
7
+ end
6
8
  end
7
- end
8
9
 
9
- class Time
10
- def weekday
11
- self.strftime('%A')
10
+ class Time
11
+ def weekday
12
+ self.strftime('%A')
13
+ end
12
14
  end
13
- end
14
15
 
15
16
  describe LucidWorks::Datasource::Schedule do
16
17
  before do
@@ -19,17 +20,22 @@ describe LucidWorks::Datasource::Schedule do
19
20
  end
20
21
 
21
22
  describe '#frequency' do
22
- it "should return 'daily'" do
23
+ it "should return 'weekly' when period == num of seconds in a week" do
24
+ @schedule.period = 604800
25
+ @schedule.frequency.should == 'weekly'
26
+ end
27
+
28
+ it "should return 'daily' when period == num of seconds in a day" do
23
29
  @schedule.period = 86400
24
30
  @schedule.frequency.should == 'daily'
25
31
  end
26
32
 
27
- it "should return 'hourly'" do
33
+ it "should return 'hourly' when period == num of seconds in an hour" do
28
34
  @schedule.period = 3600
29
35
  @schedule.frequency.should == 'hourly'
30
36
  end
31
37
 
32
- it "should return 'custom'" do
38
+ it "should return 'custom' when period does not correspond to a common number of seconds" do
33
39
  @schedule.period = 1
34
40
  @schedule.frequency.should == 'custom'
35
41
  end
@@ -41,7 +47,7 @@ describe LucidWorks::Datasource::Schedule do
41
47
  end
42
48
 
43
49
  describe '#next_start' do
44
- describe 'start_time is in the future' do
50
+ context 'start_time is in the future' do
45
51
  it 'should return start_time' do
46
52
  @schedule.reference_time = Time.now.utc
47
53
  @schedule.start_time = @schedule.reference_time + 30.minutes
@@ -104,9 +110,49 @@ describe LucidWorks::Datasource::Schedule do
104
110
  end
105
111
  end
106
112
 
113
+ describe '#active, #active=' do
114
+ it 'should maintain specified value' do
115
+ @schedule.active = true
116
+ @schedule.active.should == true
117
+ @schedule.active = false
118
+ @schedule.active.should == false
119
+ # make sure the first true wasn't luck
120
+ @schedule.active = true
121
+ @schedule.active.should == true
122
+ end
123
+ end
124
+
107
125
  describe '#schedule=' do
108
- context 'supplied start time has elapsed w/rt current interval' do
126
+ context 'active attribute supplied' do
127
+ describe 'updates the "active" attribute in the model' do
128
+ it 'should update the "active" attribute in the model' do
129
+ @schedule.active = true
130
+ attributes = {"frequency"=>"hourly", "start"=>{"minutes"=>"15"}, "active" => 'false'}
131
+ @schedule.schedule = attributes
132
+ @schedule.active.should == false
109
133
 
134
+ attributes = {"frequency"=>"hourly", "start"=>{"minutes"=>"15"}, "active" => 'true'}
135
+ @schedule.schedule = attributes
136
+ @schedule.active.should == true
137
+ end
138
+ end
139
+
140
+ describe 'with active attribute not supplied' do
141
+ it 'should not change the attribute in the model' do
142
+ attributes = {"frequency"=>"hourly", "start"=>{"minutes"=>"15"}}
143
+ @schedule.active= true
144
+ @schedule.schedule = attributes
145
+ @schedule.active.should == true
146
+
147
+ @schedule.active= false
148
+ @schedule.schedule = attributes
149
+ @schedule.active.should == false
150
+ end
151
+
152
+ end
153
+ end
154
+
155
+ context 'supplied start time has elapsed w/rt current interval' do
110
156
  describe 'with hourly frequency' do
111
157
  it "should advance the start time" do
112
158
  half_past_midnight = Time.iso8601("2011-05-09T00:30:00Z")
@@ -115,6 +161,7 @@ describe LucidWorks::Datasource::Schedule do
115
161
 
116
162
  @schedule.reference_time = half_past_midnight
117
163
  @schedule.start_time.should_not == quarter_past_1_am
164
+
118
165
  @schedule.schedule = hourly_at_quarter_past
119
166
  @schedule.start_time.should == quarter_past_1_am
120
167
  end
@@ -128,6 +175,7 @@ describe LucidWorks::Datasource::Schedule do
128
175
 
129
176
  @schedule.reference_time = today_at_noon
130
177
  @schedule.start_time.should_not == tomorrow_at_eleven_am
178
+
131
179
  @schedule.schedule = daily_at_11_am
132
180
  @schedule.start_time.should == tomorrow_at_eleven_am
133
181
  end
@@ -146,6 +194,7 @@ describe LucidWorks::Datasource::Schedule do
146
194
 
147
195
  @schedule.reference_time = friday_of_this_week
148
196
  @schedule.start_time.should_not == wednesday_of_next_week
197
+
149
198
  @schedule.schedule = weekly_on_wednesday
150
199
  @schedule.start_time.should == wednesday_of_next_week
151
200
  end
@@ -208,4 +257,10 @@ describe LucidWorks::Datasource::Schedule do
208
257
  end
209
258
  end
210
259
  end
260
+
261
+ # context 'sub-time convenience accessors' do
262
+ # describe '#hour' do
263
+ # @schedule.start_time = Time.iso8601("2011-05-09T00:30:00Z")
264
+ # end
265
+ # end
211
266
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: lucid_works
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.6.5
5
+ version: 0.6.6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Sam Pierson
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-16 00:00:00 -07:00
13
+ date: 2011-05-17 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency