by_star 2.1.0.beta2 → 2.2.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. data/.gitignore +5 -4
  2. data/.travis.yml +35 -25
  3. data/CHANGELOG.md +21 -0
  4. data/Gemfile +25 -4
  5. data/MIT-LICENSE +20 -20
  6. data/{README.markdown → README.md} +414 -348
  7. data/Rakefile +18 -2
  8. data/UPGRADING +10 -0
  9. data/by_star.gemspec +32 -31
  10. data/cleaner.rb +24 -24
  11. data/lib/by_star.rb +15 -71
  12. data/lib/by_star/base.rb +53 -0
  13. data/lib/by_star/between.rb +80 -0
  14. data/lib/by_star/directional.rb +21 -0
  15. data/lib/by_star/kernel.rb +41 -0
  16. data/lib/by_star/normalization.rb +118 -0
  17. data/lib/by_star/orm/active_record/by_star.rb +52 -0
  18. data/lib/by_star/orm/mongoid/by_star.rb +59 -0
  19. data/lib/by_star/version.rb +3 -3
  20. data/spec/database.yml +15 -15
  21. data/spec/fixtures/active_record/models.rb +6 -13
  22. data/spec/fixtures/active_record/schema.rb +11 -36
  23. data/spec/fixtures/mongoid/models.rb +15 -65
  24. data/spec/fixtures/shared/seeds.rb +16 -35
  25. data/spec/integration/active_record/active_record_spec.rb +50 -0
  26. data/spec/integration/mongoid/mongoid_spec.rb +43 -0
  27. data/spec/integration/shared/by_calendar_month.rb +55 -0
  28. data/spec/integration/shared/by_day.rb +84 -0
  29. data/spec/integration/shared/by_direction.rb +55 -0
  30. data/spec/integration/shared/by_fortnight.rb +48 -0
  31. data/spec/integration/shared/by_month.rb +50 -0
  32. data/spec/integration/shared/by_quarter.rb +49 -0
  33. data/spec/integration/shared/by_week.rb +54 -0
  34. data/spec/integration/shared/by_weekend.rb +49 -0
  35. data/spec/integration/shared/by_year.rb +48 -0
  36. data/spec/integration/shared/offset_parameter.rb +31 -0
  37. data/spec/spec_helper.rb +29 -35
  38. data/spec/unit/kernel_time_spec.rb +57 -0
  39. data/spec/unit/normalization_spec.rb +255 -0
  40. metadata +131 -56
  41. data/Gemfile.lock +0 -94
  42. data/lib/by_star/by_day.rb +0 -34
  43. data/lib/by_star/by_direction.rb +0 -52
  44. data/lib/by_star/by_fortnight.rb +0 -58
  45. data/lib/by_star/by_month.rb +0 -47
  46. data/lib/by_star/by_quarter.rb +0 -32
  47. data/lib/by_star/by_week.rb +0 -32
  48. data/lib/by_star/by_weekend.rb +0 -20
  49. data/lib/by_star/by_year.rb +0 -62
  50. data/lib/by_star/instance_methods.rb +0 -13
  51. data/lib/by_star/time_ext.rb +0 -21
  52. data/lib/mongoid/by_star.rb +0 -83
  53. data/spec/by_star/active_record/active_record_spec.rb +0 -50
  54. data/spec/by_star/mongoid/mongoid_spec.rb +0 -44
  55. data/spec/by_star/shared/by_day.rb +0 -62
  56. data/spec/by_star/shared/by_direction.rb +0 -85
  57. data/spec/by_star/shared/by_fortnight.rb +0 -47
  58. data/spec/by_star/shared/by_month.rb +0 -109
  59. data/spec/by_star/shared/by_quarter.rb +0 -33
  60. data/spec/by_star/shared/by_week.rb +0 -41
  61. data/spec/by_star/shared/by_weekend.rb +0 -13
  62. data/spec/by_star/shared/by_year.rb +0 -54
  63. data/spec/time_ext_spec.rb +0 -10
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples_for 'by calendar month' do
4
+
5
+ describe '#by_calendar_month' do
6
+
7
+ context 'point-in-time' do
8
+ subject { Post.by_calendar_month('Feb') }
9
+ its(:count){ should eq 3 }
10
+ end
11
+
12
+ context 'timespan' do
13
+ subject { Event.by_calendar_month(1) }
14
+ its(:count){ should eq 8 }
15
+ end
16
+
17
+ context 'timespan strict' do
18
+ subject { Event.by_calendar_month(Date.parse('2014-02-01'), strict: true) }
19
+ its(:count){ should eq 2 }
20
+ end
21
+
22
+ context 'with :year option' do
23
+
24
+ context 'point-in-time' do
25
+ subject { Post.by_calendar_month(12, year: 2013) }
26
+ its(:count){ should eq 4 }
27
+ end
28
+
29
+ context 'timespan' do
30
+ subject { Event.by_calendar_month('December', year: 2013) }
31
+ its(:count){ should eq 5 }
32
+ end
33
+
34
+ context 'timespan strict' do
35
+ subject { Event.by_calendar_month('Dec', year: 2013, strict: true) }
36
+ its(:count){ should eq 1 }
37
+ end
38
+ end
39
+
40
+ it 'should raise an error when given an invalid argument' do
41
+ ->{ Post.by_calendar_month(0) }.should raise_error(ByStar::ParseError, 'Month must be a number between 1 and 12 or a month name')
42
+ ->{ Post.by_calendar_month(13) }.should raise_error(ByStar::ParseError, 'Month must be a number between 1 and 12 or a month name')
43
+ ->{ Post.by_calendar_month('foobar') }.should raise_error(ByStar::ParseError, 'Month must be a number between 1 and 12 or a month name')
44
+ end
45
+
46
+ it 'should be able to use an alternative field' do
47
+ Event.by_calendar_month(:field => 'end_time').count.should eq 8
48
+ end
49
+
50
+ context ':start_day option' do
51
+ subject { Post.by_calendar_month(1, :start_day => :wednesday) }
52
+ its(:count){ should eq 7 }
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,84 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples_for 'by day' do
4
+
5
+ describe '#by_day' do
6
+
7
+ context 'point-in-time' do
8
+ subject { Post.by_day('2014-01-01') }
9
+ its(:count){ should eq 2 }
10
+ end
11
+
12
+ context 'timespan' do
13
+ subject { Event.by_day(Time.parse '2014-01-01') }
14
+ its(:count){ should eq 4 }
15
+ end
16
+
17
+ context 'timespan strict' do
18
+ subject { Event.by_day(Date.parse('2014-01-01'), strict: true) }
19
+ its(:count){ should eq 0 }
20
+ end
21
+
22
+ it 'should be able to use an alternative field' do
23
+ Event.by_day(:field => 'end_time').count.should eq 4
24
+ end
25
+
26
+ it 'should support :offset option' do
27
+ Post.by_day('2014-01-01', :offset => -16.hours).count.should eq 1
28
+ end
29
+ end
30
+
31
+ describe '#today' do # 2014-01-01
32
+
33
+ context 'point-in-time' do
34
+ subject { Post.today }
35
+ its(:count){ should eq 2 }
36
+ end
37
+
38
+ context 'timespan' do
39
+ subject { Event.today }
40
+ its(:count){ should eq 4 }
41
+ end
42
+
43
+ context 'timespan strict' do
44
+ subject { Event.today(strict: true) }
45
+ its(:count){ should eq 0 }
46
+ end
47
+ end
48
+
49
+ describe '#yesterday' do # 2013-12-31
50
+
51
+ context 'point-in-time' do
52
+ subject { Post.yesterday }
53
+ its(:count){ should eq 1 }
54
+ end
55
+
56
+ context 'timespan' do
57
+ subject { Event.yesterday }
58
+ its(:count){ should eq 4 }
59
+ end
60
+
61
+ context 'timespan strict' do
62
+ subject { Event.yesterday(strict: true) }
63
+ its(:count){ should eq 0 }
64
+ end
65
+ end
66
+
67
+ describe '#tomorrow' do # 2014-01-02
68
+
69
+ context 'point-in-time' do
70
+ subject { Post.tomorrow }
71
+ its(:count){ should eq 0 }
72
+ end
73
+
74
+ context 'timespan' do
75
+ subject { Event.tomorrow }
76
+ its(:count){ should eq 4 }
77
+ end
78
+
79
+ context 'timespan strict' do
80
+ subject { Event.tomorrow(strict: true) }
81
+ its(:count){ should eq 0 }
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples_for 'by direction' do
4
+
5
+ describe '#before' do
6
+
7
+ context 'point-in-time' do
8
+ subject { Post.before(Date.parse '2014-01-05') }
9
+ its(:count){ should eq 3 }
10
+ end
11
+
12
+ context 'timespan' do
13
+ subject { Event.before(Time.parse '2014-01-05') }
14
+ its(:count){ should eq 4 }
15
+ end
16
+
17
+ context 'timespan strict' do
18
+ subject { Event.before('2014-01-05', strict: true) }
19
+ its(:count){ should eq 4 }
20
+ end
21
+ end
22
+
23
+ describe '#after' do
24
+
25
+ context 'point-in-time' do
26
+ subject { Post.after('2014-01-05') }
27
+ its(:count){ should eq 10 }
28
+ end
29
+
30
+ context 'timespan' do
31
+ subject { Event.after(Date.parse '2014-01-05') }
32
+ its(:count){ should eq 9 }
33
+ end
34
+
35
+ context 'timespan strict' do
36
+ subject { Event.after('2014-01-05', strict: true) }
37
+ its(:count){ should eq 9 }
38
+ end
39
+ end
40
+
41
+ describe '#previous and #next' do
42
+
43
+ context 'point-in-time' do
44
+ subject { Post.where(created_at: Time.zone.parse('2014-01-10 17:00:00')).first }
45
+ it{ subject.previous.created_at.should eq Time.zone.parse('2014-01-05 17:00:00') }
46
+ it{ subject.next.created_at.should eq Time.zone.parse('2014-01-12 17:00:00') }
47
+ end
48
+
49
+ context 'timespan' do
50
+ subject { Event.where(start_time: Time.zone.parse('2014-01-05 17:00:00')).first }
51
+ it{ subject.previous.start_time.should eq Time.zone.parse('2013-12-31 17:00:00') }
52
+ it{ subject.next.start_time.should eq Time.zone.parse('2014-01-07 17:00:00') }
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples_for 'by fortnight' do
4
+
5
+ describe '#by_fortnight' do
6
+
7
+ context 'point-in-time' do
8
+ subject { Post.by_fortnight('2014-01-01') }
9
+ its(:count){ should eq 5 }
10
+ end
11
+
12
+ context 'timespan' do
13
+ subject { Event.by_fortnight(0) }
14
+ its(:count){ should eq 6 }
15
+ end
16
+
17
+ context 'timespan strict' do
18
+ subject { Event.by_fortnight(Date.parse('2014-01-01'), strict: true) }
19
+ its(:count){ should eq 0 }
20
+ end
21
+
22
+ context 'with :year option' do
23
+
24
+ context 'point-in-time' do
25
+ subject { Post.by_fortnight(26, year: 2013) }
26
+ its(:count){ should eq 6 }
27
+ end
28
+
29
+ context 'timespan' do
30
+ subject { Event.by_fortnight(26, year: 2013) }
31
+ its(:count){ should eq 6 }
32
+ end
33
+
34
+ context 'timespan strict' do
35
+ subject { Event.by_fortnight(26, year: 2013, strict: true) }
36
+ its(:count){ should eq 1 }
37
+ end
38
+ end
39
+
40
+ it 'should raise an error when given an invalid argument' do
41
+ lambda { Post.by_fortnight(27) }.should raise_error(ByStar::ParseError, 'Fortnight number must be between 0 and 26')
42
+ end
43
+
44
+ it 'should be able to use an alternative field' do
45
+ Event.by_fortnight(:field => 'end_time').count.should eq 6
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples_for 'by month' do
4
+
5
+ describe '#by_month' do
6
+
7
+ context 'point-in-time' do
8
+ subject { Post.by_month('Feb') }
9
+ its(:count){ should eq 2 }
10
+ end
11
+
12
+ context 'timespan' do
13
+ subject { Event.by_month(1) }
14
+ its(:count){ should eq 8 }
15
+ end
16
+
17
+ context 'timespan strict' do
18
+ subject { Event.by_month(Date.parse('2014-02-01'), strict: true) }
19
+ its(:count){ should eq 1 }
20
+ end
21
+
22
+ context 'with :year option' do
23
+
24
+ context 'point-in-time' do
25
+ subject { Post.by_month(12, year: 2013) }
26
+ its(:count){ should eq 1 }
27
+ end
28
+
29
+ context 'timespan' do
30
+ subject { Event.by_month('December', year: 2013) }
31
+ its(:count){ should eq 4 }
32
+ end
33
+
34
+ context 'timespan strict' do
35
+ subject { Event.by_month('Dec', year: 2013, strict: true) }
36
+ its(:count){ should eq 0 }
37
+ end
38
+ end
39
+
40
+ it 'should raise an error when given an invalid argument' do
41
+ ->{ Post.by_month(0) }.should raise_error(ByStar::ParseError, 'Month must be a number between 1 and 12 or a month name')
42
+ ->{ Post.by_month(13) }.should raise_error(ByStar::ParseError, 'Month must be a number between 1 and 12 or a month name')
43
+ ->{ Post.by_month('foobar') }.should raise_error(ByStar::ParseError, 'Month must be a number between 1 and 12 or a month name')
44
+ end
45
+
46
+ it 'should be able to use an alternative field' do
47
+ Event.by_month(:field => 'end_time').count.should eq 8
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples_for 'by quarter' do
4
+
5
+ describe '#by_quarter' do
6
+
7
+ context 'point-in-time' do
8
+ subject { Post.by_quarter(2) }
9
+ its(:count){ should eq 2 }
10
+ end
11
+
12
+ context 'timespan' do
13
+ subject { Event.by_quarter(1) }
14
+ its(:count){ should eq 12 }
15
+ end
16
+
17
+ context 'timespan strict' do
18
+ subject { Event.by_quarter(Date.parse('2014-02-01'), strict: true) }
19
+ its(:count){ should eq 7 }
20
+ end
21
+
22
+ context 'with :year option' do
23
+
24
+ context 'point-in-time' do
25
+ subject { Post.by_quarter(4, year: 2013) }
26
+ its(:count){ should eq 1 }
27
+ end
28
+
29
+ context 'timespan' do
30
+ subject { Event.by_quarter(4, year: 2013) }
31
+ its(:count){ should eq 4 }
32
+ end
33
+
34
+ context 'timespan strict' do
35
+ subject { Event.by_quarter(4, year: 2013, strict: true) }
36
+ its(:count){ should eq 0 }
37
+ end
38
+ end
39
+
40
+ it 'should raise an error when given an invalid argument' do
41
+ ->{ Post.by_quarter(0) }.should raise_error(ByStar::ParseError, 'Quarter number must be between 1 and 4')
42
+ ->{ Post.by_quarter(5) }.should raise_error(ByStar::ParseError, 'Quarter number must be between 1 and 4')
43
+ end
44
+
45
+ it 'should be able to use an alternative field' do
46
+ Event.by_quarter(1, :field => 'end_time').count.should eq 12
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples_for 'by week' do
4
+
5
+ describe '#by_week' do
6
+
7
+ context 'point-in-time' do
8
+ subject { Post.by_week('2014-01-02') }
9
+ its(:count){ should eq 4 }
10
+ end
11
+
12
+ context 'timespan' do
13
+ subject { Event.by_week(0) }
14
+ its(:count){ should eq 5 }
15
+ end
16
+
17
+ context 'timespan strict' do
18
+ subject { Event.by_week(Date.parse('2014-01-01'), strict: true) }
19
+ its(:count){ should eq 0 }
20
+ end
21
+
22
+ context 'with :year option' do
23
+
24
+ context 'point-in-time' do
25
+ subject { Post.by_week(52, year: 2013) }
26
+ its(:count){ should eq 4 }
27
+ end
28
+
29
+ context 'timespan' do
30
+ subject { Event.by_week(52, year: 2013) }
31
+ its(:count){ should eq 5 }
32
+ end
33
+
34
+ context 'timespan strict' do
35
+ subject { Event.by_week(52, year: 2013, strict: true) }
36
+ its(:count){ should eq 0 }
37
+ end
38
+ end
39
+
40
+ it 'should raise an error when given an invalid argument' do
41
+ lambda { Post.by_weekend(-1) }.should raise_error(ByStar::ParseError, 'Week number must be between 0 and 52')
42
+ lambda { Post.by_weekend(53) }.should raise_error(ByStar::ParseError, 'Week number must be between 0 and 52')
43
+ end
44
+
45
+ it 'should be able to use an alternative field' do
46
+ Event.by_week(:field => 'end_time').count.should eq 5
47
+ end
48
+
49
+ context ':start_day option' do
50
+ subject { Post.by_week('2014-01-02', :start_day => :thursday) }
51
+ its(:count){ should eq 1 }
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples_for 'by weekend' do
4
+
5
+ describe '#by_weekend' do
6
+
7
+ context 'point-in-time' do
8
+ subject { Post.by_weekend('2014-01-01') }
9
+ its(:count){ should eq 1 }
10
+ end
11
+
12
+ context 'timespan' do
13
+ subject { Event.by_weekend(0) }
14
+ its(:count){ should eq 5 }
15
+ end
16
+
17
+ context 'timespan strict' do
18
+ subject { Event.by_weekend(Date.parse('2014-01-01'), strict: true) }
19
+ its(:count){ should eq 0 }
20
+ end
21
+
22
+ context 'with :year option' do
23
+
24
+ context 'point-in-time' do
25
+ subject { Post.by_weekend(52, year: 2013) }
26
+ its(:count){ should eq 1 }
27
+ end
28
+
29
+ context 'timespan' do
30
+ subject { Event.by_weekend(52, year: 2013) }
31
+ its(:count){ should eq 5 }
32
+ end
33
+
34
+ context 'timespan strict' do
35
+ subject { Event.by_weekend(52, year: 2013, strict: true) }
36
+ its(:count){ should eq 0 }
37
+ end
38
+ end
39
+
40
+ it 'should raise an error when given an invalid argument' do
41
+ lambda { Post.by_weekend(-1) }.should raise_error(ByStar::ParseError, 'Week number must be between 0 and 52')
42
+ lambda { Post.by_weekend(53) }.should raise_error(ByStar::ParseError, 'Week number must be between 0 and 52')
43
+ end
44
+
45
+ it 'should be able to use an alternative field' do
46
+ Event.by_weekend(:field => 'end_time').count.should eq 5
47
+ end
48
+ end
49
+ end