lucid_works 0.6.6 → 0.6.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,19 @@
1
1
  module LucidWorks
2
2
  class Datasource
3
3
 
4
+ #
5
+ # Datasource::Schedule supplies numerous convenience methods to make it easy and clean to
6
+ # display and set schedules in simple formats
7
+ #
8
+ # The simple formats supported are:
9
+ # hourly at 15 minutes past the hour # => {:frequency => 'hourly', :start => {:minutes => 15}}
10
+ # daily at 3:15 # => {:frequency => 'daily', :start => {:hours => 3, :minutes => 15}}
11
+ # weekly at 3:15 on Tuesday # => {:frequency => 'weekly', :start => {:days => 1, :hours => 3, :minutes => 15}}
12
+ #
13
+ # Notes:
14
+ # day of week is specified as the # of days from Monday b/c Rails day of week helpers are monday-based
15
+ # :start hash options keys are pluralized b/c we feed them direcly to Time.advance, e.g. Time.now.advance(:days => 3)
16
+ #
4
17
  class Schedule < Base
5
18
  self.singleton = true
6
19
  belongs_to :datasource
@@ -8,10 +21,14 @@ module LucidWorks
8
21
  schema do
9
22
  attribute :period
10
23
  attribute :start_time, :iso8601
11
- attribute :active
24
+ attribute :active, :boolean
12
25
  attribute :type
13
26
  end
14
27
 
28
+ #
29
+ # returns hourly, daily, weekly based on period
30
+ # returns custom if it doesn't fit a supported interval
31
+ #
15
32
  def frequency
16
33
  case period
17
34
  when 1.weeks.seconds then 'weekly'
@@ -22,6 +39,9 @@ module LucidWorks
22
39
  end
23
40
  end
24
41
 
42
+ #
43
+ # accepts hourly, daily, weekly, and sets period to respective # of seconds
44
+ #
25
45
  def frequency=(frequency)
26
46
  self.period = case frequency
27
47
  when 'hourly' then 1.hours.seconds
@@ -31,7 +51,9 @@ module LucidWorks
31
51
  end
32
52
  end
33
53
 
54
+ #
34
55
  # predict when action will occur next if active at that time
56
+ #
35
57
  def next_start
36
58
  return start_time if reference_time <= start_time
37
59
  # require 'ruby-debug'; debugger
@@ -47,8 +69,10 @@ module LucidWorks
47
69
  end
48
70
  start_time + period * next_interval_num
49
71
  end
50
-
72
+
73
+ #
51
74
  # Provide ability to override "now" explicitly for testing purposes
75
+ #
52
76
  def reference_time=(time); @reference_time = time; end
53
77
  def reference_time; @reference_time || Time.now.localtime; end
54
78
 
@@ -84,9 +108,7 @@ module LucidWorks
84
108
  all_attributes['start'].to_options!
85
109
  all_attributes['start'].each{|k,v| all_attributes['start'][k]=v.to_i}
86
110
 
87
- if all_attributes.keys.include?('active')
88
- self.active = (all_attributes['active'].to_s.downcase == 'true')
89
- end
111
+ self.active = all_attributes['active'] if all_attributes.keys.include?('active')
90
112
 
91
113
  self.frequency = all_attributes['frequency']
92
114
  self.start_time =
@@ -107,18 +129,44 @@ module LucidWorks
107
129
  end
108
130
  end
109
131
 
132
+ #
133
+ # convenince method for setting the current value in a view selector
134
+ #
110
135
  def hour
111
136
  self.start_time.localtime.hour rescue nil
112
137
  end
113
138
 
139
+ #
140
+ # convenince method for setting the current value in a view selector
141
+ #
114
142
  def min
115
143
  self.start_time.localtime.min rescue nil
116
144
  end
117
145
 
146
+ #
147
+ # convenince method for setting the current value in a view selector
148
+ #
118
149
  def day_of_week
119
- # subract 1 because '%u' returns 1-7, and this is for indexing a ruby array of day names
150
+ # subract 1 because '%u' returns 1-7, and this is working with zero-indexed ruby arrays
120
151
  self.start_time.localtime.strftime('%u').to_i - 1 rescue nil
121
152
  end
153
+
154
+ def start_is_more_than_one_period_in_the_future?
155
+ # This works fine with start times that are multiple periods in the past
156
+ # because that gives a negative difference, and period is always >= 0
157
+ (self.start_time - self.reference_time) > self.period
158
+ end
159
+
160
+ #
161
+ # convenience method for detecting whether schedule can be represented in simple format
162
+ # or should be described as custom (meaning it is beyond the capabilities of the convenience methods)
163
+ #
164
+ def custom?
165
+ return true if self.frequency == 'custom'
166
+ return false unless self.start_time
167
+ return true if self.start_is_more_than_one_period_in_the_future?
168
+ return false
169
+ end
122
170
  end
123
171
  end
124
172
  end
@@ -1,3 +1,3 @@
1
1
  module LucidWorks
2
- VERSION = "0.6.6"
2
+ VERSION = "0.6.7"
3
3
  end
@@ -258,9 +258,91 @@ describe LucidWorks::Datasource::Schedule do
258
258
  end
259
259
  end
260
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
261
+ context 'sub-time convenience accessors' do
262
+ describe '#hour' do
263
+ it 'should return the hour component of the time' do
264
+ @schedule.start_time = Time.new(2011, 4, 15, 13, 11, 56)
265
+ @schedule.hour.should == 13
266
+ end
267
+
268
+ it 'should return nil if start_time == nil' do
269
+ @schedule.start_time = nil
270
+ @schedule.hour.should == nil
271
+ end
272
+ end
273
+
274
+ describe '#min' do
275
+ it 'should return the minute component of the time' do
276
+ @schedule.start_time = Time.new(2011, 4, 15, 13, 11, 56)
277
+ @schedule.min.should == 11
278
+ end
279
+
280
+ it 'should return nil if start_time == nil' do
281
+ @schedule.start_time = nil
282
+ @schedule.min.should == nil
283
+ end
284
+ end
285
+
286
+ describe '#day_of_week' do
287
+ it 'should return the week component of the time' do
288
+ @schedule.start_time = Time.new(2011, 4, 15, 13, 11, 56)
289
+ @schedule.day_of_week.should == 4
290
+ end
291
+
292
+ it 'should return nil if start_time == nil' do
293
+ @schedule.start_time = nil
294
+ @schedule.day_of_week.should == nil
295
+ end
296
+ end
297
+ end
298
+
299
+ describe '#custom?' do
300
+ context 'schedule can not be represent as simple weekly/daily/hourly' do
301
+ it 'should be true if frequency is non-standard' do
302
+ @schedule.period = 1
303
+ @schedule.should be_custom
304
+ end
305
+
306
+ it 'should be true if period is standard but start_time is > 1 period from now' do
307
+ some_random_time = Time.new(2011, 4, 15, 13, 11, 56)
308
+ more_than_a_day_later = some_random_time.advance(:days => 2)
309
+
310
+ @schedule.reference_time = some_random_time
311
+ @schedule.frequency = 'daily'
312
+ @schedule.start_time = more_than_a_day_later
313
+
314
+ @schedule.should be_custom
315
+ end
316
+ end
317
+
318
+ context 'schedule can be represent as simple weekly/daily/hourly' do
319
+ it 'should be false if period is standard and start_time is within period from now' do
320
+ half_past_midnight = Time.iso8601("2011-05-09T00:30:00Z")
321
+ a_standard_schedule = {"frequency"=>"hourly", "start"=>{"minutes"=>"15"}}
322
+ a_standard_reference_time = Time.iso8601('2011-05-09T01:15:00Z')
323
+
324
+ @schedule.reference_time = a_standard_reference_time
325
+ @schedule.schedule = a_standard_schedule
326
+
327
+ @schedule.should_not be_custom
328
+ end
329
+
330
+ it 'should be false if period is standard and start time is > 1 period in the past' do
331
+ some_random_time = Time.new(2011, 4, 15, 13, 11, 56)
332
+ more_than_a_day_ago = some_random_time.advance(:days => -2)
333
+ @schedule.reference_time = some_random_time
334
+ @schedule.frequency = 'daily'
335
+ @schedule.start_time = more_than_a_day_ago
336
+
337
+ @schedule.should_not be_custom
338
+ end
339
+
340
+ end
341
+
342
+ it 'should be false if the start_time is nil and frequency is standard' do
343
+ @schedule.start_time = nil
344
+ @schedule.frequency = 'weekly'
345
+ @schedule.should_not be_custom
346
+ end
347
+ end
266
348
  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.6
5
+ version: 0.6.7
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-17 00:00:00 -04:00
13
+ date: 2011-05-18 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency