by_star 2.0.0.beta1 → 2.1.0.beta2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/.travis.yml +2 -0
  2. data/Gemfile.lock +42 -42
  3. data/README.markdown +78 -14
  4. data/by_star.gemspec +3 -2
  5. data/lib/by_star.rb +12 -4
  6. data/lib/by_star/by_day.rb +4 -0
  7. data/lib/by_star/by_fortnight.rb +4 -0
  8. data/lib/by_star/by_month.rb +24 -12
  9. data/lib/by_star/by_quarter.rb +32 -0
  10. data/lib/by_star/by_week.rb +5 -3
  11. data/lib/by_star/by_weekend.rb +3 -0
  12. data/lib/by_star/by_year.rb +5 -3
  13. data/lib/by_star/version.rb +1 -1
  14. data/lib/mongoid/by_star.rb +83 -0
  15. data/spec/by_star/active_record/active_record_spec.rb +50 -0
  16. data/spec/by_star/mongoid/mongoid_spec.rb +44 -0
  17. data/spec/by_star/shared/by_day.rb +62 -0
  18. data/spec/by_star/shared/by_direction.rb +85 -0
  19. data/spec/by_star/shared/by_fortnight.rb +47 -0
  20. data/spec/by_star/shared/by_month.rb +109 -0
  21. data/spec/by_star/shared/by_quarter.rb +33 -0
  22. data/spec/by_star/shared/by_week.rb +41 -0
  23. data/spec/by_star/shared/by_weekend.rb +13 -0
  24. data/spec/by_star/shared/by_year.rb +54 -0
  25. data/spec/fixtures/active_record/models.rb +13 -0
  26. data/spec/fixtures/{schema.rb → active_record/schema.rb} +0 -0
  27. data/spec/fixtures/mongoid/models.rb +65 -0
  28. data/spec/fixtures/{models.rb → shared/seeds.rb} +0 -16
  29. data/spec/spec_helper.rb +4 -11
  30. metadata +95 -31
  31. data/spec/by_star/by_day_spec.rb +0 -52
  32. data/spec/by_star/by_direction_spec.rb +0 -82
  33. data/spec/by_star/by_fortnight_spec.rb +0 -46
  34. data/spec/by_star/by_month_spec.rb +0 -60
  35. data/spec/by_star/by_week_spec.rb +0 -39
  36. data/spec/by_star/by_weekend_spec.rb +0 -12
  37. data/spec/by_star/by_year_spec.rb +0 -57
@@ -1,52 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "by day" do
4
- def posts_count(*args)
5
- Post.by_day(*args).count
6
- end
7
-
8
- it "should be able to find a post for today" do
9
- posts_count.should eql(4)
10
- end
11
-
12
- it "should be able to find a post by a given date in last year" do
13
- posts_count(:year => Time.zone.now.year - 1).should eql(1)
14
- end
15
-
16
- it "should be able to use an alternative field" do
17
- Event.by_day(Time.now.yesterday, :field => "start_time").size.should eql(1)
18
- end
19
- end
20
-
21
- describe "today" do
22
- it "should show the post for today" do
23
- Post.today.map(&:text).should include("Today's post")
24
- end
25
-
26
- it "should be able to use an alternative field" do
27
- # Test may occur on an event day.
28
- Event.today(:field => "start_time").size.should eql(1)
29
- end
30
- end
31
-
32
- describe "yesterday" do
33
-
34
- it "should show the post for yesterday" do
35
- Post.yesterday.map(&:text).should include("Yesterday's post")
36
- end
37
-
38
- it "should be able to use an alternative field" do
39
- Event.yesterday(:field => "start_time").size.should eql(1)
40
- end
41
-
42
- end
43
-
44
- describe "tomorrow" do
45
- it "should show the post for tomorrow" do
46
- Post.tomorrow.map(&:text).should include("Tomorrow's post")
47
- end
48
-
49
- it "should be able to use an alternative field" do
50
- Event.tomorrow(:field => "start_time").size.should eql(1)
51
- end
52
- end
@@ -1,82 +0,0 @@
1
- require 'spec_helper'
2
- describe "before" do
3
- def posts_count(*args)
4
- Post.before(*args).count
5
- end
6
-
7
- it "should show the correct number of posts in the past" do
8
- posts_count.should == 5
9
- end
10
-
11
- it "is aliased as before_now" do
12
- Post.before_now.count.should == 5
13
- end
14
-
15
- it "should find for a given time" do
16
- posts_count(Time.zone.now - 2.days).should eql(2)
17
- end
18
-
19
- it "should find for a given date" do
20
- posts_count(Date.today - 2).should eql(2)
21
- end
22
-
23
- it "should find for a given string" do
24
- posts_count("next tuesday").should eql(8)
25
- end
26
-
27
- it "raises an exception when Chronic can't parse" do
28
- lambda { posts_count(";aosdjbjisdabdgofbi") }.should raise_error(ByStar::ParseError)
29
- end
30
-
31
- it "should be able to find all events before Ryan's birthday using a non-standard field" do
32
- Event.before(Time.local(Time.zone.now.year+2), :field => "start_time").count.should eql(8)
33
- end
34
- end
35
-
36
- describe "future" do
37
- def posts_count(*args)
38
- Post.after(*args).count
39
- end
40
-
41
- it "should show the correct number of posts in the future" do
42
- Post.after.count.should eql(posts_count)
43
- Post.after_now.count.should eql(posts_count)
44
- end
45
-
46
- it "should find for a given date" do
47
- posts_count(Date.today - 2).should eql(19)
48
- end
49
-
50
- it "should find for a given string" do
51
- posts_count("next tuesday").should eql(13)
52
- end
53
-
54
- it "should be able to find all events before Dad's birthday using a non-standard field" do
55
- Event.after(Time.zone.local(Time.zone.now.year, 7, 5), :field => "start_time").count.should eql(3)
56
- end
57
- end
58
-
59
- describe "previous and next" do
60
- let(:current_post) { Post.find_by_text("post 1") }
61
- let(:current_event) { Event.find_by_name("Mum's birthday!") }
62
-
63
- context "previous" do
64
- it "can find the previous post" do
65
- current_post.previous.text.should == "Yesterday's post"
66
- end
67
-
68
- it "takes the field option" do
69
- current_event.previous(:field => "start_time").name.should == "Dad's birthday!"
70
- end
71
- end
72
-
73
- context "next" do
74
- it "can find the next post" do
75
- current_post.next.text.should == "Today's post"
76
- end
77
-
78
- it "takes the field option" do
79
- current_event.next(:field => "start_time").name.should == "Ryan's birthday!"
80
- end
81
- end
82
- end
@@ -1,46 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "by fortnight" do
4
-
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
-
@@ -1,60 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "by_month" do
4
- def find_posts(time=Time.zone.now, options={})
5
- Post.by_month(time, options)
6
- end
7
-
8
- def posts_count(time=Time.zone.now, options={})
9
- find_posts(time, options).count
10
- end
11
-
12
- it "should be able to find posts for the current month" do
13
- posts_count.should eql(6)
14
- end
15
-
16
- it "should be able to find a single post for January" do
17
- # If it is January we'll have all the "current" posts in there.
18
- # This makes the count 10.
19
- # I'm sure you can guess what happens when it's not January.
20
- posts_count("January").should eql(6)
21
- end
22
-
23
- it "should be able to find two posts for the 2nd month" do
24
- # If it is February we'll have all the "current" posts in there.
25
- # This makes the count 10.
26
- # I'm sure you can guess what happens when it's not February.
27
- posts_count(2).should eql(1)
28
- end
29
-
30
- it "should be able to find three posts for the 3rd month, using time instance" do
31
- # If it is March we'll have all the "current" posts in there.
32
- # This makes the count 10.
33
- # I'm sure you can guess what happens when it's not March.
34
- time = Time.local(Time.zone.now.year, 3, 1)
35
- posts_count(time).should eql(1)
36
- end
37
-
38
- it "should be able to find a single post from January last year" do
39
- posts_count("January", :year => Time.zone.now.year - 1).should eql(1)
40
- end
41
-
42
- it "should fail when given incorrect months" do
43
- lambda { find_posts(0) }.should raise_error(ByStar::ParseError)
44
- lambda { find_posts(13) }.should raise_error(ByStar::ParseError)
45
- lambda { find_posts("Ryan") }.should raise_error(ByStar::ParseError)
46
- # LOL arrays
47
- lambda { find_posts([1,2,3]) }.should raise_error(NoMethodError)
48
- end
49
-
50
- it "should be able to use an alternative field" do
51
- Timecop.freeze(Time.zone.local(Time.zone.now.year, 12, 1, 0, 0, 0)) do
52
- Event.by_month(:field => "start_time").size.should eql(1)
53
- end
54
- end
55
-
56
- it "should be able to specify the year as a string" do
57
- posts_count(1, :year => (Time.zone.now.year - 1).to_s).should eql(1)
58
- end
59
-
60
- end
@@ -1,39 +0,0 @@
1
- require 'spec_helper'
2
- describe "by week" do
3
- def posts_count(*args)
4
- find_posts(*args).count
5
- end
6
-
7
- def find_posts(*args)
8
- Post.by_week(*args)
9
- end
10
-
11
- it "should be able to find posts in the current week" do
12
- posts_count.should eql(5)
13
- end
14
-
15
- it "should be able to find posts in the 1st week" do
16
- posts_count(0).should eql(6)
17
- end
18
-
19
- it "should be able to find posts in the 1st week of last year" do
20
- posts_count(0, :year => Time.zone.now.year-1).should eql(1)
21
- end
22
-
23
- it "should not find any posts from a week ago" do
24
- posts_count(1.week.ago).should eql(1)
25
- end
26
-
27
- it "should be able to use an alternative field" do
28
- Event.by_week(:field => "start_time").size.should eql(2)
29
- end
30
-
31
- it "should find posts at the start of the year" do
32
- posts_count(0).should eql(6)
33
- end
34
-
35
- it "should find posts at the end of the year" do
36
- posts_count(Time.zone.now.end_of_year).should eql(1)
37
- end
38
-
39
- end
@@ -1,12 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'by weekend' do
4
-
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
@@ -1,57 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "by_year" do
4
- def posts_count(*args)
5
- find_posts(*args).count
6
- end
7
-
8
- def find_posts(*args)
9
- options = args.extract_options!
10
- Post.by_year(args.first, options)
11
- end
12
-
13
- let(:this_years_posts) { 18 }
14
-
15
- it "should be able to find all the posts in the current year" do
16
- posts_count.should eql(this_years_posts)
17
- end
18
-
19
- it "should be able to find if given a string" do
20
- posts_count(Time.zone.now.year.to_s).should eql(this_years_posts)
21
- end
22
-
23
- it "should be able to find a single post from last year" do
24
- posts_count(Time.zone.now.year-1).should eql(3)
25
- end
26
-
27
- it "knows what last year's posts were" do
28
- find_posts(Time.zone.now.year-1).map(&:text).should =~ ["A week ago", "This time, last year", "Yesterday's post"]
29
- end
30
-
31
- it "can find posts given a 2-digit year" do
32
- # Should be good for at least a couple of years.
33
- posts_count(Time.zone.now.year-2001).should eql(3)
34
- end
35
-
36
- it "should be able to use an alternative field (string)" do
37
- Event.by_year(:field => "start_time").count.should eql(6)
38
- end
39
-
40
- it "should be able to use an alternative field (symbol)" do
41
- Event.by_year(:field => :start_time).count.should eql(6)
42
- end
43
-
44
- it "should not have to specify the field when using by_star_field" do
45
- Event.by_year.count.should eql(6)
46
- end
47
-
48
- it "should not include yesterday's (Dec 31st <last year>) event in by_year" do
49
- Event.by_year.map(&:name).should_not include("Yesterday")
50
- end
51
-
52
- it "should be able to order the result set" do
53
- scope = find_posts(Time.zone.now.year, :order => "created_at DESC")
54
- scope.order_values.should == ["created_at DESC"]
55
- end
56
- end
57
-