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.
- data/.gitignore +5 -4
- data/.travis.yml +35 -25
- data/CHANGELOG.md +21 -0
- data/Gemfile +25 -4
- data/MIT-LICENSE +20 -20
- data/{README.markdown → README.md} +414 -348
- data/Rakefile +18 -2
- data/UPGRADING +10 -0
- data/by_star.gemspec +32 -31
- data/cleaner.rb +24 -24
- data/lib/by_star.rb +15 -71
- data/lib/by_star/base.rb +53 -0
- data/lib/by_star/between.rb +80 -0
- data/lib/by_star/directional.rb +21 -0
- data/lib/by_star/kernel.rb +41 -0
- data/lib/by_star/normalization.rb +118 -0
- data/lib/by_star/orm/active_record/by_star.rb +52 -0
- data/lib/by_star/orm/mongoid/by_star.rb +59 -0
- data/lib/by_star/version.rb +3 -3
- data/spec/database.yml +15 -15
- data/spec/fixtures/active_record/models.rb +6 -13
- data/spec/fixtures/active_record/schema.rb +11 -36
- data/spec/fixtures/mongoid/models.rb +15 -65
- data/spec/fixtures/shared/seeds.rb +16 -35
- data/spec/integration/active_record/active_record_spec.rb +50 -0
- data/spec/integration/mongoid/mongoid_spec.rb +43 -0
- data/spec/integration/shared/by_calendar_month.rb +55 -0
- data/spec/integration/shared/by_day.rb +84 -0
- data/spec/integration/shared/by_direction.rb +55 -0
- data/spec/integration/shared/by_fortnight.rb +48 -0
- data/spec/integration/shared/by_month.rb +50 -0
- data/spec/integration/shared/by_quarter.rb +49 -0
- data/spec/integration/shared/by_week.rb +54 -0
- data/spec/integration/shared/by_weekend.rb +49 -0
- data/spec/integration/shared/by_year.rb +48 -0
- data/spec/integration/shared/offset_parameter.rb +31 -0
- data/spec/spec_helper.rb +29 -35
- data/spec/unit/kernel_time_spec.rb +57 -0
- data/spec/unit/normalization_spec.rb +255 -0
- metadata +131 -56
- data/Gemfile.lock +0 -94
- data/lib/by_star/by_day.rb +0 -34
- data/lib/by_star/by_direction.rb +0 -52
- data/lib/by_star/by_fortnight.rb +0 -58
- data/lib/by_star/by_month.rb +0 -47
- data/lib/by_star/by_quarter.rb +0 -32
- data/lib/by_star/by_week.rb +0 -32
- data/lib/by_star/by_weekend.rb +0 -20
- data/lib/by_star/by_year.rb +0 -62
- data/lib/by_star/instance_methods.rb +0 -13
- data/lib/by_star/time_ext.rb +0 -21
- data/lib/mongoid/by_star.rb +0 -83
- data/spec/by_star/active_record/active_record_spec.rb +0 -50
- data/spec/by_star/mongoid/mongoid_spec.rb +0 -44
- data/spec/by_star/shared/by_day.rb +0 -62
- data/spec/by_star/shared/by_direction.rb +0 -85
- data/spec/by_star/shared/by_fortnight.rb +0 -47
- data/spec/by_star/shared/by_month.rb +0 -109
- data/spec/by_star/shared/by_quarter.rb +0 -33
- data/spec/by_star/shared/by_week.rb +0 -41
- data/spec/by_star/shared/by_weekend.rb +0 -13
- data/spec/by_star/shared/by_year.rb +0 -54
- data/spec/time_ext_spec.rb +0 -10
@@ -1,62 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
shared_examples_for "by day" do
|
4
|
-
describe "by day" do
|
5
|
-
def posts_count(*args)
|
6
|
-
Post.by_day(*args).count
|
7
|
-
end
|
8
|
-
|
9
|
-
it "should be able to find a post for today" do
|
10
|
-
posts_count.should eql(4)
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should be able to find a post by a given date in last year" do
|
14
|
-
posts_count(:year => Time.zone.now.year - 1).should eql(1)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should be able to use an alternative field" do
|
18
|
-
Event.by_day(Time.now.yesterday, :field => "start_time").size.should eql(1)
|
19
|
-
end
|
20
|
-
|
21
|
-
it "should be able to use a date" do
|
22
|
-
posts_count(Date.today).should eql(4)
|
23
|
-
end
|
24
|
-
|
25
|
-
it "should be able to use a String" do
|
26
|
-
posts_count(Date.today.to_s).should eql(4)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
describe "today" do
|
31
|
-
it "should show the post for today" do
|
32
|
-
Post.today.map(&:text).should include("Today's post")
|
33
|
-
end
|
34
|
-
|
35
|
-
it "should be able to use an alternative field" do
|
36
|
-
# Test may occur on an event day.
|
37
|
-
Event.today(:field => "start_time").size.should eql(1)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
describe "yesterday" do
|
42
|
-
|
43
|
-
it "should show the post for yesterday" do
|
44
|
-
Post.yesterday.map(&:text).should include("Yesterday's post")
|
45
|
-
end
|
46
|
-
|
47
|
-
it "should be able to use an alternative field" do
|
48
|
-
Event.yesterday(:field => "start_time").size.should eql(1)
|
49
|
-
end
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
describe "tomorrow" do
|
54
|
-
it "should show the post for tomorrow" do
|
55
|
-
Post.tomorrow.map(&:text).should include("Tomorrow's post")
|
56
|
-
end
|
57
|
-
|
58
|
-
it "should be able to use an alternative field" do
|
59
|
-
Event.tomorrow(:field => "start_time").size.should eql(1)
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
@@ -1,85 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
shared_examples_for "by direction" do
|
4
|
-
describe "before" do
|
5
|
-
def posts_count(*args)
|
6
|
-
Post.before(*args).count
|
7
|
-
end
|
8
|
-
|
9
|
-
it "should show the correct number of posts in the past" do
|
10
|
-
posts_count.should == 5
|
11
|
-
end
|
12
|
-
|
13
|
-
it "is aliased as before_now" do
|
14
|
-
Post.before_now.count.should == 5
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should find for a given time" do
|
18
|
-
posts_count(Time.zone.now - 2.days).should eql(2)
|
19
|
-
end
|
20
|
-
|
21
|
-
it "should find for a given date" do
|
22
|
-
posts_count(Date.today - 2).should eql(2)
|
23
|
-
end
|
24
|
-
|
25
|
-
it "should find for a given string" do
|
26
|
-
posts_count("next tuesday").should eql(8)
|
27
|
-
end
|
28
|
-
|
29
|
-
it "raises an exception when Chronic can't parse" do
|
30
|
-
lambda { posts_count(";aosdjbjisdabdgofbi") }.should raise_error(ByStar::ParseError)
|
31
|
-
end
|
32
|
-
|
33
|
-
it "should be able to find all events before Ryan's birthday using a non-standard field" do
|
34
|
-
Event.before(Time.local(Time.zone.now.year+2), :field => "start_time").count.should eql(8)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
describe "future" do
|
39
|
-
def posts_count(*args)
|
40
|
-
Post.after(*args).count
|
41
|
-
end
|
42
|
-
|
43
|
-
it "should show the correct number of posts in the future" do
|
44
|
-
Post.after.count.should eql(posts_count)
|
45
|
-
Post.after_now.count.should eql(posts_count)
|
46
|
-
end
|
47
|
-
|
48
|
-
it "should find for a given date" do
|
49
|
-
posts_count(Date.today - 2).should eql(19)
|
50
|
-
end
|
51
|
-
|
52
|
-
it "should find for a given string" do
|
53
|
-
posts_count("next tuesday").should eql(13)
|
54
|
-
end
|
55
|
-
|
56
|
-
it "should be able to find all events before Dad's birthday using a non-standard field" do
|
57
|
-
Event.after(Time.zone.local(Time.zone.now.year, 7, 5), :field => "start_time").count.should eql(3)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
describe "previous and next" do
|
62
|
-
let(:current_post) { Post.find_by_text("post 1") }
|
63
|
-
let(:current_event) { Event.find_by_name("Mum's birthday!") }
|
64
|
-
|
65
|
-
context "previous" do
|
66
|
-
it "can find the previous post" do
|
67
|
-
current_post.previous.text.should == "Yesterday's post"
|
68
|
-
end
|
69
|
-
|
70
|
-
it "takes the field option" do
|
71
|
-
current_event.previous(:field => "start_time").name.should == "Dad's birthday!"
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
context "next" do
|
76
|
-
it "can find the next post" do
|
77
|
-
current_post.next.text.should == "Today's post"
|
78
|
-
end
|
79
|
-
|
80
|
-
it "takes the field option" do
|
81
|
-
current_event.next(:field => "start_time").name.should == "Ryan's birthday!"
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
@@ -1,47 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
shared_examples_for "by fortnight" do
|
4
|
-
describe "by fortnight" do
|
5
|
-
def find_posts(*args)
|
6
|
-
Post.by_fortnight(*args)
|
7
|
-
end
|
8
|
-
|
9
|
-
def posts_count(*args)
|
10
|
-
find_posts(*args).count
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should be able to find posts in the current fortnight" do
|
14
|
-
posts_count.should eql(6)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should be able to find posts in the 1st fortnight of the current year" do
|
18
|
-
posts_count(0).should eql(6)
|
19
|
-
posts_count("0").should eql(6)
|
20
|
-
# There was previously a situation where incorrect time zone math
|
21
|
-
# caused the 'post 1' post to NOT appear, so count would be 7, rather than 8.
|
22
|
-
# So this line simply regression tests that problem.
|
23
|
-
Post.by_fortnight(0).map(&:text).should include("post 1")
|
24
|
-
end
|
25
|
-
|
26
|
-
it "should be able to find posts for a fortnight ago" do
|
27
|
-
posts_count(2.weeks.ago).should eql(2)
|
28
|
-
end
|
29
|
-
|
30
|
-
it "should be able to find posts for a given fortnight in a year" do
|
31
|
-
posts_count(0, :year => Time.zone.now.year - 1).should eql(1)
|
32
|
-
end
|
33
|
-
|
34
|
-
it "should be able to find posts for the current fortnight in a specific year" do
|
35
|
-
posts_count(:year => Time.zone.now.year - 1).should eql(1)
|
36
|
-
end
|
37
|
-
|
38
|
-
it "should raise an error when given an invalid argument" do
|
39
|
-
lambda { find_posts(27) }.should raise_error(ByStar::ParseError, "by_fortnight takes only a Time, Date or a Fixnum (less than or equal to 26).")
|
40
|
-
end
|
41
|
-
|
42
|
-
it "should be able to use an alternative field" do
|
43
|
-
Event.by_fortnight(nil, :field => "start_time").count.should eql(2)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
@@ -1,109 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
shared_examples_for "by month" do
|
4
|
-
describe "by month" do
|
5
|
-
def find_posts(time=Time.zone.now, options={})
|
6
|
-
Post.by_month(time, options)
|
7
|
-
end
|
8
|
-
|
9
|
-
def posts_count(time=Time.zone.now, options={})
|
10
|
-
find_posts(time, options).count
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should be able to find posts for the current month" do
|
14
|
-
posts_count.should eql(6)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should be able to find a single post for January" do
|
18
|
-
# If it is January we'll have all the "current" posts in there.
|
19
|
-
# This makes the count 10.
|
20
|
-
# I'm sure you can guess what happens when it's not January.
|
21
|
-
posts_count("January").should eql(6)
|
22
|
-
end
|
23
|
-
|
24
|
-
it "should be able to find two posts for the 2nd month" do
|
25
|
-
# If it is February we'll have all the "current" posts in there.
|
26
|
-
# This makes the count 10.
|
27
|
-
# I'm sure you can guess what happens when it's not February.
|
28
|
-
posts_count(2).should eql(1)
|
29
|
-
end
|
30
|
-
|
31
|
-
it "should be able to find three posts for the 3rd month, using time instance" do
|
32
|
-
# If it is March we'll have all the "current" posts in there.
|
33
|
-
# This makes the count 10.
|
34
|
-
# I'm sure you can guess what happens when it's not March.
|
35
|
-
time = Time.local(Time.zone.now.year, 3, 1)
|
36
|
-
posts_count(time).should eql(1)
|
37
|
-
end
|
38
|
-
|
39
|
-
it "should be able to find a single post from January last year" do
|
40
|
-
posts_count("January", :year => Time.zone.now.year - 1).should eql(1)
|
41
|
-
end
|
42
|
-
|
43
|
-
it "should fail when given incorrect months" do
|
44
|
-
lambda { find_posts(0) }.should raise_error(ByStar::ParseError)
|
45
|
-
lambda { find_posts(13) }.should raise_error(ByStar::ParseError)
|
46
|
-
lambda { find_posts("Ryan") }.should raise_error(ByStar::ParseError)
|
47
|
-
# LOL arrays
|
48
|
-
lambda { find_posts([1,2,3]) }.should raise_error(NoMethodError)
|
49
|
-
end
|
50
|
-
|
51
|
-
it "should be able to use an alternative field" do
|
52
|
-
Timecop.freeze(Time.zone.local(Time.zone.now.year, 12, 1, 0, 0, 0)) do
|
53
|
-
Event.by_month(:field => "start_time").size.should eql(1)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
it "should be able to specify the year as a string" do
|
58
|
-
posts_count(1, :year => (Time.zone.now.year - 1).to_s).should eql(1)
|
59
|
-
end
|
60
|
-
|
61
|
-
it "should be able to accept time datatype" do
|
62
|
-
posts_count(Time.now - 1.year).should eql(1)
|
63
|
-
end
|
64
|
-
|
65
|
-
it "should be able to accept date datatype" do
|
66
|
-
posts_count(Date.today - 1.year).should eql(1)
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
describe "by calendar month" do
|
71
|
-
def find_posts(time=Time.zone.now, options={})
|
72
|
-
Post.by_calendar_month(time, options)
|
73
|
-
end
|
74
|
-
|
75
|
-
def posts_count(time=Time.zone.now, options={})
|
76
|
-
find_posts(time, options).count
|
77
|
-
end
|
78
|
-
|
79
|
-
it "should be able to find posts for the current calendar month" do
|
80
|
-
posts_count.should eql(8)
|
81
|
-
end
|
82
|
-
|
83
|
-
it "should be able to find a single post for January calendar month" do
|
84
|
-
# If it is January we'll have all the "current" posts in there.
|
85
|
-
# This makes the count 10.
|
86
|
-
# I'm sure you can guess what happens when it's not January.
|
87
|
-
posts_count("January").should eql(8)
|
88
|
-
end
|
89
|
-
|
90
|
-
it "should be able to find two posts for the 2nd calendar month" do
|
91
|
-
# If it is February we'll have all the "current" posts in there.
|
92
|
-
# This makes the count 10.
|
93
|
-
# I'm sure you can guess what happens when it's not February.
|
94
|
-
posts_count(2).should eql(2)
|
95
|
-
end
|
96
|
-
|
97
|
-
it "should be able to find three posts for the 3rd calendar month, using time instance" do
|
98
|
-
# If it is March we'll have all the "current" posts in there.
|
99
|
-
# This makes the count 10.
|
100
|
-
# I'm sure you can guess what happens when it's not March.
|
101
|
-
time = Time.local(Time.zone.now.year, 3, 1)
|
102
|
-
posts_count(time).should eql(1)
|
103
|
-
end
|
104
|
-
|
105
|
-
it "should be able to find a single post from January last year" do
|
106
|
-
posts_count("January", :year => Time.zone.now.year - 1).should eql(1)
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
shared_examples_for "by quarter" do
|
4
|
-
describe "by quarter" do
|
5
|
-
def posts_count(*args)
|
6
|
-
find_posts(*args).count
|
7
|
-
end
|
8
|
-
|
9
|
-
def find_posts(*args)
|
10
|
-
Post.by_quarter(*args)
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should be able to find posts in the current quarter" do
|
14
|
-
posts_count.should eql(8)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should be able to find posts in the 1st quarter" do
|
18
|
-
posts_count(1).should eql(8)
|
19
|
-
end
|
20
|
-
|
21
|
-
it "should be able to find posts in the 1st quarter of last year" do
|
22
|
-
posts_count(1, :year => Time.zone.now.year-1).should eql(1)
|
23
|
-
end
|
24
|
-
|
25
|
-
it "should be able to use an alternative field" do
|
26
|
-
Event.by_quarter(:field => "start_time").size.should eql(3)
|
27
|
-
end
|
28
|
-
|
29
|
-
it "should find posts at the last quarter of the year" do
|
30
|
-
posts_count(Time.zone.now.end_of_year).should eql(4)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
@@ -1,41 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
shared_examples_for "by week" do
|
4
|
-
describe "by week" do
|
5
|
-
def posts_count(*args)
|
6
|
-
find_posts(*args).count
|
7
|
-
end
|
8
|
-
|
9
|
-
def find_posts(*args)
|
10
|
-
Post.by_week(*args)
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should be able to find posts in the current week" do
|
14
|
-
posts_count.should eql(5)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should be able to find posts in the 1st week" do
|
18
|
-
posts_count(0).should eql(6)
|
19
|
-
end
|
20
|
-
|
21
|
-
it "should be able to find posts in the 1st week of last year" do
|
22
|
-
posts_count(0, :year => Time.zone.now.year-1).should eql(1)
|
23
|
-
end
|
24
|
-
|
25
|
-
it "should not find any posts from a week ago" do
|
26
|
-
posts_count(1.week.ago).should eql(1)
|
27
|
-
end
|
28
|
-
|
29
|
-
it "should be able to use an alternative field" do
|
30
|
-
Event.by_week(:field => "start_time").size.should eql(2)
|
31
|
-
end
|
32
|
-
|
33
|
-
it "should find posts at the start of the year" do
|
34
|
-
posts_count(0).should eql(6)
|
35
|
-
end
|
36
|
-
|
37
|
-
it "should find posts at the end of the year" do
|
38
|
-
posts_count(Time.zone.now.end_of_year).should eql(1)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
@@ -1,13 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
shared_examples_for "by weekend" do
|
4
|
-
describe "by weekend" do
|
5
|
-
it "should be able to find the posts on the weekend of the 1st of January" do
|
6
|
-
Post.by_weekend.count.should eql(6)
|
7
|
-
end
|
8
|
-
|
9
|
-
it "should be able to use an alternative field" do
|
10
|
-
Event.by_weekend(:field => "start_time").count.should eql(3)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
@@ -1,54 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
shared_examples_for "by year" do
|
4
|
-
describe "by year" do
|
5
|
-
def posts_count(*args)
|
6
|
-
find_posts(*args).count
|
7
|
-
end
|
8
|
-
|
9
|
-
def find_posts(*args)
|
10
|
-
options = args.extract_options!
|
11
|
-
Post.by_year(args.first, options)
|
12
|
-
end
|
13
|
-
|
14
|
-
let(:this_years_posts) { 18 }
|
15
|
-
|
16
|
-
it "should be able to find all the posts in the current year" do
|
17
|
-
posts_count.should eql(this_years_posts)
|
18
|
-
end
|
19
|
-
|
20
|
-
it "should be able to find if given a string" do
|
21
|
-
posts_count(Time.zone.now.year.to_s).should eql(this_years_posts)
|
22
|
-
end
|
23
|
-
|
24
|
-
it "should be able to find a single post from last year" do
|
25
|
-
posts_count(Time.zone.now.year-1).should eql(3)
|
26
|
-
end
|
27
|
-
|
28
|
-
it "knows what last year's posts were" do
|
29
|
-
find_posts(Time.zone.now.year-1).map(&:text).should =~ ["A week ago", "This time, last year", "Yesterday's post"]
|
30
|
-
end
|
31
|
-
|
32
|
-
it "can find posts given a 2-digit year" do
|
33
|
-
# Should be good for at least a couple of years.
|
34
|
-
posts_count(Time.zone.now.year-2001).should eql(3)
|
35
|
-
end
|
36
|
-
|
37
|
-
it "should be able to use an alternative field (string)" do
|
38
|
-
Event.by_year(:field => "start_time").count.should eql(6)
|
39
|
-
end
|
40
|
-
|
41
|
-
it "should be able to use an alternative field (symbol)" do
|
42
|
-
Event.by_year(:field => :start_time).count.should eql(6)
|
43
|
-
end
|
44
|
-
|
45
|
-
it "should not have to specify the field when using by_star_field" do
|
46
|
-
Event.by_year.count.should eql(6)
|
47
|
-
end
|
48
|
-
|
49
|
-
it "should not include yesterday's (Dec 31st <last year>) event in by_year" do
|
50
|
-
Event.by_year.map(&:name).should_not include("Yesterday")
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|