chronological 1.0.0beta7 → 1.0.0beta8
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.
- data/README.md +3 -0
- data/lib/chronological/version.rb +1 -1
- data/lib/chronological.rb +8 -2
- data/spec/strategies/relative_spec.rb +53 -2
- data/spec/support/active_record.rb +6 -0
- metadata +1 -1
data/README.md
CHANGED
@@ -242,6 +242,9 @@ class MyTimeRangeClass
|
|
242
242
|
end
|
243
243
|
```
|
244
244
|
|
245
|
+
_Note: The `:base_of_offset` option can be anything that has accessor
|
246
|
+
methods for the field._
|
247
|
+
|
245
248
|
### Advanced Method Usage
|
246
249
|
|
247
250
|
--------------------------------------------------------------------------------
|
data/lib/chronological.rb
CHANGED
@@ -15,9 +15,12 @@ module Chronological
|
|
15
15
|
columns_hash[strategy.field_names[:ending_date].to_s] ||= ActiveRecord::ConnectionAdapters::Column.new(strategy.field_names[:ending_date], nil, 'date')
|
16
16
|
end
|
17
17
|
|
18
|
+
# TODO: Once we implement anonymous modules in Greenwich and Chronological, then
|
19
|
+
# we can call `super` here which would call Greenwich which would convert the
|
20
|
+
# time over to UTC.
|
18
21
|
if strategy.has_absolute_start?
|
19
22
|
define_method(strategy.field_names[:starting_time]) do |*args|
|
20
|
-
super()
|
23
|
+
super().try(:utc)
|
21
24
|
end
|
22
25
|
else
|
23
26
|
define_method(strategy.field_names[:starting_time]) do |*args|
|
@@ -27,9 +30,12 @@ module Chronological
|
|
27
30
|
end
|
28
31
|
end
|
29
32
|
|
33
|
+
# TODO: Once we implement anonymous modules in Greenwich and Chronological, then
|
34
|
+
# we can call `super` here which would call Greenwich which would convert the
|
35
|
+
# time over to UTC.
|
30
36
|
if strategy.has_absolute_end?
|
31
37
|
define_method(strategy.field_names[:ending_time]) do |*args|
|
32
|
-
super()
|
38
|
+
super().try(:utc)
|
33
39
|
end
|
34
40
|
else
|
35
41
|
define_method(strategy.field_names[:ending_time]) do |*args|
|
@@ -34,6 +34,17 @@ class RelativeChronologicalWithOverriddenTime < ActiveRecord::Base
|
|
34
34
|
ending_date: :foobar_ending_date
|
35
35
|
end
|
36
36
|
|
37
|
+
class RelativeChronologicableWithDynamicBase < ActiveRecord::Base
|
38
|
+
extend Chronological
|
39
|
+
|
40
|
+
timeframe type: :relative,
|
41
|
+
starting_offset: :starting_offset,
|
42
|
+
ending_offset: :ending_offset,
|
43
|
+
base_of_offset: :base_datetime_utc
|
44
|
+
|
45
|
+
attr_accessor :base_datetime_utc
|
46
|
+
end
|
47
|
+
|
37
48
|
describe Chronological::RelativeStrategy, :timecop => true do
|
38
49
|
let(:now) { nil }
|
39
50
|
let(:starting_offset) { nil }
|
@@ -67,6 +78,14 @@ describe Chronological::RelativeStrategy, :timecop => true do
|
|
67
78
|
RelativeChronologicalWithOverriddenTime.new
|
68
79
|
end
|
69
80
|
|
81
|
+
let(:chronologicable_with_dynamic_base) do
|
82
|
+
RelativeChronologicableWithDynamicBase.new(
|
83
|
+
starting_offset: starting_offset,
|
84
|
+
ending_offset: ending_offset,
|
85
|
+
base_datetime_utc: base_time
|
86
|
+
)
|
87
|
+
end
|
88
|
+
|
70
89
|
it { chronologicable_with_overridden_time.should respond_to(:foobar_starting_time) }
|
71
90
|
it { chronologicable_with_overridden_time.should respond_to(:foobar_ending_time) }
|
72
91
|
it { chronologicable_with_overridden_time.should respond_to(:foobar_starting_date) }
|
@@ -122,7 +141,7 @@ describe Chronological::RelativeStrategy, :timecop => true do
|
|
122
141
|
context 'when there are two chronologicables that start at different times' do
|
123
142
|
context 'and end at different times' do
|
124
143
|
let!(:chronologicable_1) { RelativeChronologicable.create(:base_datetime_utc => Time.now, :starting_offset => 3, :ending_offset => 2) }
|
125
|
-
let!(:chronologicable_2) { RelativeChronologicable.create(:base_datetime_utc => Time.now, :starting_offset => 2,
|
144
|
+
let!(:chronologicable_2) { RelativeChronologicable.create(:base_datetime_utc => Time.now, :starting_offset => 2, :ending_offset => 1) }
|
126
145
|
|
127
146
|
context 'when in ascending order' do
|
128
147
|
it 'sorts them by the start date' do
|
@@ -141,7 +160,7 @@ describe Chronological::RelativeStrategy, :timecop => true do
|
|
141
160
|
|
142
161
|
context 'but end at the same time' do
|
143
162
|
let!(:chronologicable_1) { RelativeChronologicable.create(:base_datetime_utc => Time.now, :starting_offset => 3, :ending_offset => 1) }
|
144
|
-
let!(:chronologicable_2) { RelativeChronologicable.create(:base_datetime_utc => Time.now, :starting_offset => 2,
|
163
|
+
let!(:chronologicable_2) { RelativeChronologicable.create(:base_datetime_utc => Time.now, :starting_offset => 2, :ending_offset => 1) }
|
145
164
|
|
146
165
|
context 'when in ascending order' do
|
147
166
|
it 'sorts them by the start date' do
|
@@ -376,6 +395,10 @@ describe Chronological::RelativeStrategy, :timecop => true do
|
|
376
395
|
end
|
377
396
|
end
|
378
397
|
|
398
|
+
it 'does not have a start time when the base is dynamic' do
|
399
|
+
chronologicable_with_dynamic_base.started_at.should be_nil
|
400
|
+
end
|
401
|
+
|
379
402
|
it 'does not have a start time when called directly' do
|
380
403
|
chronologicable.started_at.should be_nil
|
381
404
|
end
|
@@ -472,6 +495,10 @@ describe Chronological::RelativeStrategy, :timecop => true do
|
|
472
495
|
end
|
473
496
|
end
|
474
497
|
|
498
|
+
it 'does not have a start time when the base is dynamic' do
|
499
|
+
chronologicable_with_dynamic_base.started_at.should be_nil
|
500
|
+
end
|
501
|
+
|
475
502
|
it 'does not have a start time when called directly' do
|
476
503
|
chronologicable.started_at.should be_nil
|
477
504
|
end
|
@@ -484,6 +511,10 @@ describe Chronological::RelativeStrategy, :timecop => true do
|
|
484
511
|
context 'and the ending offset is set' do
|
485
512
|
let(:ending_offset) { 0 }
|
486
513
|
|
514
|
+
it 'does not have an end time when the base is dynamic' do
|
515
|
+
chronologicable_with_dynamic_base.ended_at.should be_nil
|
516
|
+
end
|
517
|
+
|
487
518
|
it 'does not have an end time when called directly' do
|
488
519
|
chronologicable.ended_at.should be_nil
|
489
520
|
end
|
@@ -496,6 +527,10 @@ describe Chronological::RelativeStrategy, :timecop => true do
|
|
496
527
|
context 'and the ending offset is not set' do
|
497
528
|
let(:ending_offset) { nil }
|
498
529
|
|
530
|
+
it 'does not have an end time when the base is dynamic' do
|
531
|
+
chronologicable_with_dynamic_base.ended_at.should be_nil
|
532
|
+
end
|
533
|
+
|
499
534
|
it 'does not have an end time when called directly' do
|
500
535
|
chronologicable.ended_at.should be_nil
|
501
536
|
end
|
@@ -766,6 +801,10 @@ describe Chronological::RelativeStrategy, :timecop => true do
|
|
766
801
|
end
|
767
802
|
end
|
768
803
|
|
804
|
+
it 'does not have a start time when the base is dynamic' do
|
805
|
+
chronologicable_with_dynamic_base.started_at.should be_nil
|
806
|
+
end
|
807
|
+
|
769
808
|
it 'does not have a start time when called directly' do
|
770
809
|
chronologicable.started_at.should be_nil
|
771
810
|
end
|
@@ -946,6 +985,10 @@ describe Chronological::RelativeStrategy, :timecop => true do
|
|
946
985
|
end
|
947
986
|
end
|
948
987
|
|
988
|
+
it 'calculates the correct start time when the base is dynamic' do
|
989
|
+
chronologicable_with_dynamic_base.started_at.should eql Time.local(2012, 7, 26, 6, 0, 0)
|
990
|
+
end
|
991
|
+
|
949
992
|
it 'calculates the correct start time when called directly' do
|
950
993
|
chronologicable.started_at.should eql Time.local(2012, 7, 26, 6, 0, 0)
|
951
994
|
end
|
@@ -958,6 +1001,10 @@ describe Chronological::RelativeStrategy, :timecop => true do
|
|
958
1001
|
context 'and the ending offset is set' do
|
959
1002
|
let(:ending_offset) { 30 }
|
960
1003
|
|
1004
|
+
it 'calculates the correct end time when the base is dynamic' do
|
1005
|
+
chronologicable_with_dynamic_base.ended_at.should eql Time.local(2012, 7, 26, 6, 0, 0)
|
1006
|
+
end
|
1007
|
+
|
961
1008
|
it 'calculates the correct end time when called directly' do
|
962
1009
|
chronologicable.ended_at.should eql Time.local(2012, 7, 26, 6, 0, 0)
|
963
1010
|
end
|
@@ -970,6 +1017,10 @@ describe Chronological::RelativeStrategy, :timecop => true do
|
|
970
1017
|
context 'and the ending offset is not set' do
|
971
1018
|
let(:ending_offset) { nil }
|
972
1019
|
|
1020
|
+
it 'does not have an end time when the base is dynamic' do
|
1021
|
+
chronologicable_with_dynamic_base.ended_at.should be_nil
|
1022
|
+
end
|
1023
|
+
|
973
1024
|
it 'does not have a end time when called directly' do
|
974
1025
|
chronologicable.ended_at.should be_nil
|
975
1026
|
end
|
@@ -50,12 +50,18 @@ ActiveRecord::Base.connection.create_table :absolute_chronologicable_with_time_z
|
|
50
50
|
t.string :time_zone
|
51
51
|
end
|
52
52
|
|
53
|
+
ActiveRecord::Base.connection.create_table :relative_chronologicable_with_dynamic_bases do |t|
|
54
|
+
t.integer :starting_offset
|
55
|
+
t.integer :ending_offset
|
56
|
+
end
|
57
|
+
|
53
58
|
RSpec.configure do |config|
|
54
59
|
config.before(:each) do
|
55
60
|
ActiveRecord::Base.connection.execute 'DELETE FROM chronologicable_strategy_classes'
|
56
61
|
ActiveRecord::Base.connection.execute 'DELETE FROM base_chronologicables'
|
57
62
|
ActiveRecord::Base.connection.execute 'DELETE FROM relative_chronologicables'
|
58
63
|
ActiveRecord::Base.connection.execute 'DELETE FROM relative_chronologicable_with_time_zones'
|
64
|
+
ActiveRecord::Base.connection.execute 'DELETE FROM relative_chronologicable_with_dynamic_bases'
|
59
65
|
ActiveRecord::Base.connection.execute 'DELETE FROM absolute_chronologicables'
|
60
66
|
ActiveRecord::Base.connection.execute 'DELETE FROM absolute_chronologicable_with_time_zones'
|
61
67
|
end
|