nickel 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,6 @@
1
1
  require 'time'
2
2
 
3
3
  module Nickel
4
-
5
4
  class ZTime
6
5
  include Comparable
7
6
 
@@ -11,10 +10,12 @@ module Nickel
11
10
  # \@time is always stored on 24 hour clock, but we could initialize a Time object with ZTime.new("1020", :pm)
12
11
  # we will convert this to 24 hour clock and set \@firm = true
13
12
  def initialize(hhmmss = nil, am_pm = nil)
14
- t = hhmmss ? hhmmss : ::Time.new.strftime("%H%M%S")
15
- t.gsub!(/:/,'') # remove any hyphens, so a user can initialize with something like "2008-10-23"
13
+ t = hhmmss ? hhmmss : ::Time.new.strftime('%H%M%S')
14
+ t.gsub!(/:/, '') # remove any hyphens, so a user can initialize with something like "2008-10-23"
16
15
  self.time = t
17
- if am_pm then adjust_for(am_pm) end
16
+ if am_pm
17
+ adjust_for(am_pm)
18
+ end
18
19
  end
19
20
 
20
21
  def time
@@ -33,7 +34,7 @@ module Nickel
33
34
 
34
35
  # @deprecated Please use {#min_str} instead
35
36
  def minute_str
36
- warn "[DEPRECATION] `minute_str` is deprecated. Please use `min_str` instead."
37
+ warn '[DEPRECATION] `minute_str` is deprecated. Please use `min_str` instead.'
37
38
  min_str
38
39
  end
39
40
 
@@ -43,7 +44,7 @@ module Nickel
43
44
 
44
45
  # @deprecated Please use {#sec_str} instead
45
46
  def second_str
46
- warn "[DEPRECATION] `second_str` is deprecated. Please use `sec_str` instead."
47
+ warn '[DEPRECATION] `second_str` is deprecated. Please use `sec_str` instead.'
47
48
  sec_str
48
49
  end
49
50
 
@@ -57,7 +58,7 @@ module Nickel
57
58
 
58
59
  # @deprecated Please use {#min} instead
59
60
  def minute
60
- warn "[DEPRECATION] `minute` is deprecated. Please use `min` instead."
61
+ warn '[DEPRECATION] `minute` is deprecated. Please use `min` instead.'
61
62
  min
62
63
  end
63
64
 
@@ -67,7 +68,7 @@ module Nickel
67
68
 
68
69
  # @deprecated Please use {#sec} instead
69
70
  def second
70
- warn "[DEPRECATION] `second` is deprecated. Please use `sec` instead."
71
+ warn '[DEPRECATION] `second` is deprecated. Please use `sec` instead.'
71
72
  sec
72
73
  end
73
74
 
@@ -82,25 +83,24 @@ module Nickel
82
83
  def add_minutes(number, &block)
83
84
  # new minute is going to be (current minute + number) % 60
84
85
  # number of hours to add is (current minute + number) / 60
85
- hours_to_add = (self.min + number) / 60
86
+ hours_to_add = (min + number) / 60
86
87
  # note add_hours returns a new time object
87
88
  if block_given?
88
- o = self.add_hours(hours_to_add, &block)
89
+ o = add_hours(hours_to_add, &block)
89
90
  else
90
- o = self.add_hours(hours_to_add)
91
+ o = add_hours(hours_to_add)
91
92
  end
92
93
  o.change_minute_to((o.min + number) % 60) # modifies self
93
94
  end
94
95
 
95
96
  def add_hours(number, &block)
96
- o = self.dup
97
+ o = dup
97
98
  if block_given?
98
99
  yield((o.hour + number) / 24)
99
100
  end
100
101
  o.change_hour_to((o.hour + number) % 24)
101
102
  end
102
103
 
103
-
104
104
  # NOTE: change_ methods modify self.
105
105
  def change_hour_to(h)
106
106
  self.time = ZTime.format_time(h, min_str, sec_str)
@@ -118,11 +118,11 @@ module Nickel
118
118
  end
119
119
 
120
120
  def readable
121
- @time[0..1] + ":" + @time[2..3] + ":" + @time[4..5]
121
+ @time[0..1] + ':' + @time[2..3] + ':' + @time[4..5]
122
122
  end
123
123
 
124
124
  def readable_12hr
125
- hour_on_12hr_clock + ":" + @time[2..3] + " #{am_pm}"
125
+ hour_on_12hr_clock + ':' + @time[2..3] + " #{am_pm}"
126
126
  end
127
127
 
128
128
  def hour_on_12hr_clock
@@ -132,19 +132,24 @@ module Nickel
132
132
  end
133
133
 
134
134
  def is_am?
135
+ warn '[DEPRECATION] `is_am?` is deprecated. Please use `am?` instead.'
136
+ am?
137
+ end
138
+
139
+ def am?
135
140
  hour < 12 # 0 through 11 on 24hr clock
136
141
  end
137
142
 
138
143
  def am_pm
139
- is_am? ? "am" : "pm"
144
+ am? ? 'am' : 'pm'
140
145
  end
141
146
 
142
- def <=>(t2)
143
- return nil unless [:hour, :min, :sec].all?{|m| t2.respond_to?(m)}
147
+ def <=>(other)
148
+ return nil unless [:hour, :min, :sec].all? { |m| other.respond_to?(m) }
144
149
 
145
- if before?(t2)
150
+ if before?(other)
146
151
  -1
147
- elsif after?(t2)
152
+ elsif after?(other)
148
153
  1
149
154
  else
150
155
  0
@@ -160,13 +165,12 @@ module Nickel
160
165
  end
161
166
 
162
167
  class << self
163
-
164
168
  # send an array of ZTime objects, this will make a guess at whether they should be am/pm if the user did not specify
165
169
  # NOTE ORDER IS IMPORTANT: times[0] is assumed to be BEFORE times[1]
166
170
  def am_pm_modifier(*time_array)
167
171
  # find firm time indices
168
172
  firm_time_indices = []
169
- time_array.each_with_index {|t,i| firm_time_indices << i if t.firm}
173
+ time_array.each_with_index { |t, i| firm_time_indices << i if t.firm }
170
174
 
171
175
  if firm_time_indices.empty?
172
176
  # pure guess
@@ -174,22 +178,22 @@ module Nickel
174
178
  time_array.each_index do |i|
175
179
  # user gave us nothing
176
180
  next if i == 0
177
- time_array[i].guess_modify_such_that_is_after(time_array[i-1])
181
+ time_array[i].guess_modify_such_that_is_after(time_array[i - 1])
178
182
  end
179
183
  else
180
184
  # first handle soft times up to first firm time
181
185
  min_boundary = 0
182
186
  max_boundary = firm_time_indices[0]
183
187
  (min_boundary...max_boundary).to_a.reverse.each do |i| # this says, iterate backwards starting from max_boundary, but not including it, until the min boundary
184
- time_array[i].modify_such_that_is_before(time_array[i+1])
188
+ time_array[i].modify_such_that_is_before(time_array[i + 1])
185
189
  end
186
190
 
187
191
  firm_time_indices.each_index do |j|
188
192
  # now handle all times after first firm time until the next firm time
189
193
  min_boundary = firm_time_indices[j]
190
- max_boundary = firm_time_indices[j+1] || time_array.size
194
+ max_boundary = firm_time_indices[j + 1] || time_array.size
191
195
  (min_boundary + 1...max_boundary).each do |i| # any boundary problems here? What if there is only 1 time? Nope.
192
- time_array[i].modify_such_that_is_after(time_array[i-1])
196
+ time_array[i].modify_such_that_is_after(time_array[i - 1])
193
197
  end
194
198
  end
195
199
  end
@@ -217,7 +221,7 @@ module Nickel
217
221
  end
218
222
 
219
223
  # formats the hours, minutes and seconds into the format expected by the ZTime constructor
220
- def format_time(hours, minutes=0, seconds=0)
224
+ def format_time(hours, minutes = 0, seconds = 0)
221
225
  format_hour(hours) + format_minute(minutes) + format_second(seconds)
222
226
  end
223
227
 
@@ -239,7 +243,7 @@ module Nickel
239
243
  if mdata[1].length <= 2
240
244
  # e.g. "11" means 11:00
241
245
  hstr = mdata[1]
242
- mstr = "0"
246
+ mstr = '0'
243
247
  elsif mdata[1].length == 3
244
248
  # e.g. "530" means 5:30
245
249
  hstr = mdata[1][0..0]
@@ -263,8 +267,8 @@ module Nickel
263
267
 
264
268
  # this can very easily be cleaned up
265
269
  def modify_such_that_is_before(time2)
266
- raise "ZTime#modify_such_that_is_before says: trying to modify time that has @firm set" if @firm
267
- raise "ZTime#modify_such_that_is_before says: time2 does not have @firm set" if !time2.firm
270
+ fail 'ZTime#modify_such_that_is_before says: trying to modify time that has @firm set' if @firm
271
+ fail 'ZTime#modify_such_that_is_before says: time2 does not have @firm set' unless time2.firm
268
272
  # self cannot have @firm set, so all hours will be between 1 and 12
269
273
  # time2 is an end time, self could be its current setting, or off by 12 hours
270
274
 
@@ -274,30 +278,30 @@ module Nickel
274
278
  # 1220 to 12am --> 1220 to 0000
275
279
  # 11 to 2am or 1100 to 0200
276
280
  if self > time2
277
- if self.hour == 12 && time2.hour == 0
281
+ if hour == 12 && time2.hour == 0
278
282
  # do nothing
279
283
  else
280
- self.hour == 12 ? change_hour_to(0) : change_hour_to(self.hour + 12)
284
+ hour == 12 ? change_hour_to(0) : change_hour_to(hour + 12)
281
285
  end
282
286
  elsif self < time2
283
287
  if time2.hour >= 12 && ZTime.new(ZTime.format_time(time2.hour - 12, time2.min_str, time2.sec_str)) > self
284
288
  # 4 to 5pm or 0400 to 1700
285
- change_hour_to(self.hour + 12)
289
+ change_hour_to(hour + 12)
286
290
  else
287
291
  # 4 to 1pm or 0400 to 1300
288
292
  # do nothing
289
293
  end
290
294
  else
291
295
  # the times are equal, and self can only be between 0100 and 1200, so move self forward 12 hours, unless hour is 12
292
- self.hour == 12 ? change_hour_to(0) : change_hour_to(self.hour + 12)
296
+ hour == 12 ? change_hour_to(0) : change_hour_to(hour + 12)
293
297
  end
294
298
  self.firm = true
295
299
  self
296
300
  end
297
301
 
298
302
  def modify_such_that_is_after(time1)
299
- raise "ZTime#modify_such_that_is_after says: trying to modify time that has @firm set" if @firm
300
- raise "ZTime#modify_such_that_is_after says: time1 does not have @firm set" if !time1.firm
303
+ fail 'ZTime#modify_such_that_is_after says: trying to modify time that has @firm set' if @firm
304
+ fail 'ZTime#modify_such_that_is_after says: time1 does not have @firm set' unless time1.firm
301
305
  # time1 to self --> time1 to self
302
306
  # 8pm to 835 --> 2000 to 835
303
307
  # 835pm to 835 --> 2035 to 835
@@ -307,7 +311,7 @@ module Nickel
307
311
  # 930pm to 5 ---> 2130 to 0500
308
312
  if self < time1
309
313
  unless time1.hour >= 12 && ZTime.new(ZTime.format_time(time1.hour - 12, time1.min_str, time1.sec_str)) >= self
310
- self.hour == 12 ? change_hour_to(0) : change_hour_to(self.hour + 12)
314
+ hour == 12 ? change_hour_to(0) : change_hour_to(hour + 12)
311
315
  end
312
316
  elsif self > time1
313
317
  # # time1 to self --> time1 to self
@@ -320,7 +324,7 @@ module Nickel
320
324
  # end
321
325
  else
322
326
  # the times are equal, and self can only be between 0100 and 1200, so move self forward 12 hours, unless hour is 12
323
- self.hour == 12 ? change_hour_to(0) : change_hour_to(self.hour + 12)
327
+ hour == 12 ? change_hour_to(0) : change_hour_to(hour + 12)
324
328
  end
325
329
  self.firm = true
326
330
  self
@@ -335,18 +339,18 @@ module Nickel
335
339
  # 12 to 6 ---> 1200 to 0600
336
340
  if time1 >= self
337
341
  # crossed boundary at noon
338
- self.hour == 12 ? change_hour_to(0) : change_hour_to(self.hour + 12)
342
+ hour == 12 ? change_hour_to(0) : change_hour_to(hour + 12)
339
343
  end
340
344
  end
341
345
 
342
346
  private
343
347
 
344
- def before?(t2)
345
- (hour < t2.hour) || (hour == t2.hour && (min < t2.min || (min == t2.min && sec < t2.sec)))
348
+ def before?(other)
349
+ (hour < other.hour) || (hour == other.hour && (min < other.min || (min == other.min && sec < other.sec)))
346
350
  end
347
351
 
348
- def after?(t2)
349
- (hour > t2.hour) || (hour == t2.hour && (min > t2.min || (min == t2.min && sec > t2.sec)))
352
+ def after?(other)
353
+ (hour > other.hour) || (hour == other.hour && (min > other.min || (min == other.min && sec > other.sec)))
350
354
  end
351
355
 
352
356
  def adjust_for(am_pm)
@@ -354,19 +358,19 @@ module Nickel
354
358
  # perform validation on the new time. That won't catch something like this though: ZTime.new("2215", :am)
355
359
  # so we will check for that here.
356
360
  # If user is providing :am or :pm, the hour must be between 1 and 12
357
- raise "ZTime#adjust_for says: you specified am or pm with 1 > hour > 12" unless hour >= 1 && hour <= 12
361
+ fail 'ZTime#adjust_for says: you specified am or pm with 1 > hour > 12' unless hour >= 1 && hour <= 12
358
362
  if am_pm == :am || am_pm == 'am'
359
- change_hour_to(ZTime.am_to_24hr(self.hour))
363
+ change_hour_to(ZTime.am_to_24hr(hour))
360
364
  elsif am_pm == :pm || am_pm == 'pm'
361
- change_hour_to(ZTime.pm_to_24hr(self.hour))
365
+ change_hour_to(ZTime.pm_to_24hr(hour))
362
366
  else
363
- raise "ZTime#adjust_for says: you passed an invalid value for am_pm, use :am or :pm"
367
+ fail 'ZTime#adjust_for says: you passed an invalid value for am_pm, use :am or :pm'
364
368
  end
365
369
  @firm = true
366
370
  end
367
371
 
368
372
  def validate
369
- raise "ZTime#validate says: invalid time" unless valid
373
+ fail 'ZTime#validate says: invalid time' unless valid
370
374
  end
371
375
 
372
376
  def valid
@@ -374,23 +378,23 @@ module Nickel
374
378
  end
375
379
 
376
380
  def valid_hour
377
- hour >= 0 and hour < 24
381
+ hour >= 0 && hour < 24
378
382
  end
379
383
 
380
384
  def valid_minute
381
- min >= 0 and min < 60
385
+ min >= 0 && min < 60
382
386
  end
383
387
 
384
388
  def valid_second
385
- sec >= 0 and sec < 60
389
+ sec >= 0 && sec < 60
386
390
  end
387
391
 
388
392
  def lazy(s)
389
393
  # someone isn't following directions, but we will let it slide
390
394
  s.length == 1 && s = "0#{s}0000" # only provided h
391
- s.length == 2 && s << "0000" # only provided hh
392
- s.length == 4 && s << "00" # only provided hhmm
393
- return s
395
+ s.length == 2 && s << '0000' # only provided hh
396
+ s.length == 4 && s << '00' # only provided hhmm
397
+ s
394
398
  end
395
399
  end
396
400
  end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+ require_relative '../../../lib/nickel/construct'
3
+
4
+ module Nickel
5
+ describe RecurrenceConstruct do
6
+ describe '#get_interval', :deprecated do
7
+ it 'is 1 when the recurrence is daily' do
8
+ expect(RecurrenceConstruct.new(repeats: :daily).get_interval).to eq(1)
9
+ end
10
+ end
11
+
12
+ describe '#interval' do
13
+ it 'is 1 when the recurrence is daily' do
14
+ expect(RecurrenceConstruct.new(repeats: :daily).interval).to eq(1)
15
+ end
16
+
17
+ it 'is 2 when the recurrence is every other day' do
18
+ expect(RecurrenceConstruct.new(repeats: :altdaily).interval).to eq(2)
19
+ end
20
+
21
+ it 'is 3 when the recurrence is every three days' do
22
+ expect(RecurrenceConstruct.new(repeats: :threedaily).interval).to eq(3)
23
+ end
24
+
25
+ it 'is 1 when the recurrence is weekly' do
26
+ expect(RecurrenceConstruct.new(repeats: :weekly).interval).to eq(1)
27
+ end
28
+
29
+ it 'is 2 when the recurrence is every other week' do
30
+ expect(RecurrenceConstruct.new(repeats: :altweekly).interval).to eq(2)
31
+ end
32
+
33
+ it 'is 3 when the recurrence is every three weeks' do
34
+ expect(RecurrenceConstruct.new(repeats: :threeweekly).interval).to eq(3)
35
+ end
36
+
37
+ it 'is 1 when the recurrence is on a specific weekday each month' do
38
+ expect(RecurrenceConstruct.new(repeats: :daymonthly).interval).to eq(1)
39
+ end
40
+
41
+ it 'is 2 when the recurrence is on a specific weekday every other month' do
42
+ expect(RecurrenceConstruct.new(repeats: :altdaymonthly).interval).to eq(2)
43
+ end
44
+
45
+ it 'is 3 when the recurrence is on a specific weekday every three months' do
46
+ expect(RecurrenceConstruct.new(repeats: :threedaymonthly).interval).to eq(3)
47
+ end
48
+
49
+ it 'is 1 when the recurrence is on a specific date each month' do
50
+ expect(RecurrenceConstruct.new(repeats: :datemonthly).interval).to eq(1)
51
+ end
52
+
53
+ it 'is 2 when the recurrence is on a specific date every other month' do
54
+ expect(RecurrenceConstruct.new(repeats: :altdatemonthly).interval).to eq(2)
55
+ end
56
+
57
+ it 'is 3 when the recurrence is on a specific date every three months' do
58
+ expect(RecurrenceConstruct.new(repeats: :threedatemonthly).interval).to eq(3)
59
+ end
60
+
61
+ it 'raises a StandardError if the recurrence is not recognised' do
62
+ expect{ RecurrenceConstruct.new(repeats: :fortnightly).interval }.to raise_error(StandardError)
63
+ end
64
+ end
65
+ end
66
+ end
@@ -1,13 +1,13 @@
1
- require "spec_helper"
2
- require_relative "../../../lib/nickel/nlp"
1
+ require 'spec_helper'
2
+ require_relative '../../../lib/nickel/nlp'
3
3
 
4
4
  module Nickel
5
5
  describe NLP do
6
- describe "#new" do
7
- it "raises an error if the current time argument is not a datetime or time object" do
8
- expect{
9
- NLP.new "lunch 3 days from now", Date.new(2009,05,28)
10
- }.to raise_error("You must pass in a ruby DateTime or Time class object")
6
+ describe '#new' do
7
+ it 'raises an error if the current time argument is not a datetime or time object' do
8
+ expect do
9
+ NLP.new 'lunch 3 days from now', Date.new(2009, 05, 28)
10
+ end.to raise_error('You must pass in a ruby DateTime or Time class object')
11
11
  end
12
12
  end
13
13
  end
@@ -1,48 +1,47 @@
1
- require "spec_helper"
2
- require_relative "../../../lib/nickel/occurrence"
3
- require_relative "../../../lib/nickel/zdate"
1
+ require 'spec_helper'
2
+ require_relative '../../../lib/nickel/occurrence'
3
+ require_relative '../../../lib/nickel/zdate'
4
4
 
5
5
  module Nickel
6
6
  describe Occurrence do
7
- describe "#==" do
8
- let(:occ) { Occurrence.new(type: :daily, start_date: ZDate.new("20140128")) }
7
+ describe '#==' do
8
+ let(:occ) { Occurrence.new(type: :daily, start_date: ZDate.new('20140128')) }
9
9
 
10
- it "is true when comparing to itself" do
10
+ it 'is true when comparing to itself' do
11
11
  expect(occ).to eq occ
12
12
  end
13
13
 
14
- it "is false when comparing to nil" do
14
+ it 'is false when comparing to nil' do
15
15
  expect(occ).to_not eq nil
16
16
  end
17
17
 
18
- it "is false when comparing to a string" do
19
- expect(occ).to_not eq "occ"
18
+ it 'is false when comparing to a string' do
19
+ expect(occ).to_not eq 'occ'
20
20
  end
21
21
 
22
- it "is false when comparing to an occurence with different dates" do
23
- expect(occ).to_not eq Occurrence.new(type: :daily, start_date: ZDate.new("20080825"))
22
+ it 'is false when comparing to an occurence with different dates' do
23
+ expect(occ).to_not eq Occurrence.new(type: :daily, start_date: ZDate.new('20080825'))
24
24
  end
25
25
 
26
- it "is false when comparing to an occurrence with a different type" do
27
- expect(occ).to_not eq Occurrence.new(type: :weekly, start_date: ZDate.new("20140128"))
26
+ it 'is false when comparing to an occurrence with a different type' do
27
+ expect(occ).to_not eq Occurrence.new(type: :weekly, start_date: ZDate.new('20140128'))
28
28
  end
29
29
 
30
- it "is false when comparing to an occurence with a different interval" do
31
- expect(occ).to_not eq Occurrence.new(type: :daily, start_date: ZDate.new("20140128"), interval: 3)
30
+ it 'is false when comparing to an occurence with a different interval' do
31
+ expect(occ).to_not eq Occurrence.new(type: :daily, start_date: ZDate.new('20140128'), interval: 3)
32
32
  end
33
33
 
34
- it "is true when comparing to an occurence with the same values" do
35
- expect(occ).to eq Occurrence.new(type: :daily, start_date: ZDate.new("20140128"))
34
+ it 'is true when comparing to an occurence with the same values' do
35
+ expect(occ).to eq Occurrence.new(type: :daily, start_date: ZDate.new('20140128'))
36
36
  end
37
37
  end
38
38
 
39
- describe "#inspect" do
40
- let(:occ) { Occurrence.new(type: :daily, start_date: ZDate.new("20140128"), end_date: ZDate.new("20140209"), interval: 2) }
39
+ describe '#inspect' do
40
+ let(:occ) { Occurrence.new(type: :daily, start_date: ZDate.new('20140128'), end_date: ZDate.new('20140209'), interval: 2) }
41
41
 
42
- it "shows only members that have been set" do
42
+ it 'shows only members that have been set' do
43
43
  expect(occ.inspect).to eq '#<Occurrence type: daily, start_date: 20140128, end_date: 20140209, interval: 2>'
44
44
  end
45
45
  end
46
46
  end
47
47
  end
48
-