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
@@ -0,0 +1,33 @@
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
@@ -0,0 +1,41 @@
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
@@ -0,0 +1,13 @@
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
@@ -0,0 +1,54 @@
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
+
@@ -0,0 +1,13 @@
1
+ class Post < ActiveRecord::Base
2
+ default_scope :order => "#{quoted_table_name}.created_at ASC"
3
+ has_and_belongs_to_many :tags
4
+
5
+ def self.factory(text, created_at = nil)
6
+ create!(:text => text, :created_at => created_at)
7
+ end
8
+ end
9
+
10
+ class Event < ActiveRecord::Base
11
+ by_star_field :start_time
12
+ scope :secret, :conditions => { :public => false }
13
+ end
@@ -0,0 +1,65 @@
1
+ class Post
2
+ include Mongoid::Document
3
+ include Mongoid::Timestamps
4
+ include Mongoid::ByStar
5
+
6
+ field :text, type: String
7
+
8
+ default_scope order_by([[:created_at, :asc]])
9
+ has_and_belongs_to_many :tags
10
+
11
+ def self.factory(text, created_at = nil)
12
+ create!(:text => text, :created_at => created_at)
13
+ end
14
+
15
+ def self.find_by_text(text)
16
+ where(:text => text).first
17
+ end
18
+ end
19
+
20
+ class Event
21
+ include Mongoid::Document
22
+ include Mongoid::ByStar
23
+
24
+ field :st, as: :start_time, type: DateTime
25
+ field :end_time, type: DateTime
26
+ field :name, type: String
27
+ field :public, type: Boolean, default: true
28
+
29
+ by_star_field :start_time
30
+ scope :secret, where(:public => false)
31
+
32
+ def self.find_by_name(name)
33
+ where(:name => name).first
34
+ end
35
+ end
36
+
37
+ class Tag
38
+ include Mongoid::Document
39
+ include Mongoid::Timestamps
40
+ include Mongoid::ByStar
41
+
42
+ field :name, type: String
43
+
44
+ has_and_belongs_to_many :posts
45
+ end
46
+
47
+ class Invoice
48
+ include Mongoid::Document
49
+ include Mongoid::ByStar
50
+
51
+ field :value, type: Integer
52
+ field :number, type: Integer
53
+
54
+ has_many :day_entries
55
+ end
56
+
57
+ class DayEntry
58
+ include Mongoid::Document
59
+ include Mongoid::ByStar
60
+
61
+ field :spent_at, type: DateTime
62
+ field :name, type: String
63
+
64
+ belongs_to :invoice
65
+ end
@@ -1,19 +1,3 @@
1
- class Post < ActiveRecord::Base
2
- default_scope :order => "#{quoted_table_name}.created_at ASC"
3
- has_and_belongs_to_many :tags
4
-
5
- def self.factory(text, created_at = nil)
6
- create!(:text => text, :created_at => created_at)
7
- end
8
- end
9
-
10
- class Event < ActiveRecord::Base
11
- by_star_field :start_time
12
- scope :secret, :conditions => { :public => false }
13
- end
14
-
15
- ## seed data:
16
-
17
1
  year = Time.zone.now.year
18
2
 
19
3
  1.upto(12) do |month|
@@ -1,32 +1,27 @@
1
1
  require 'rubygems'
2
2
  require 'bundler'
3
3
  Bundler.setup
4
- require 'active_record'
5
4
  require 'fileutils'
6
5
  require 'logger'
7
- FileUtils.mkdir_p("tmp")
8
6
 
7
+ FileUtils.mkdir_p(File.dirname(__FILE__) + "/tmp")
9
8
  $:.unshift(File.join(File.dirname(__FILE__), "../lib"))
10
9
 
10
+ require 'active_record'
11
+ require 'mongoid' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
11
12
  require 'active_support'
12
13
  require 'active_support/core_ext/string/conversions'
13
14
  require 'by_star'
14
15
  require 'rspec'
15
16
  require 'timecop'
16
17
 
18
+
17
19
  # Define time zone before loading test_helper
18
20
  zone = "UTC"
19
21
  Time.zone = zone
20
- ActiveRecord::Base.default_timezone = :utc
21
-
22
- ActiveRecord::Base.configurations = YAML::load_file(File.dirname(__FILE__) + "/database.yml")
23
- ActiveRecord::Base.establish_connection(ENV["DB"] || "sqlite")
24
- load File.dirname(__FILE__) + "/fixtures/schema.rb"
25
22
 
26
23
  # Freeze time to Jan 1st of this year
27
24
  Timecop.travel(Time.zone.local(Time.zone.now.year, 1, 1, 0, 0, 1, 0))
28
- load File.dirname(__FILE__) + "/fixtures/models.rb"
29
-
30
25
 
31
26
  # Print the location of puts/p calls so you can find them later
32
27
  # def puts str
@@ -38,5 +33,3 @@ load File.dirname(__FILE__) + "/fixtures/models.rb"
38
33
  # puts caller.first
39
34
  # super obj
40
35
  # end
41
-
42
- ActiveRecord::Base.logger = Logger.new("tmp/activerecord.log")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: by_star
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.beta1
4
+ version: 2.1.0.beta2
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-20 00:00:00.000000000 Z
12
+ date: 2013-04-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
16
- requirement: &70234733397300 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: 1.0.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70234733397300
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.0.0
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: sqlite3
27
- requirement: &70234733396920 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: '0'
33
38
  type: :development
34
39
  prerelease: false
35
- version_requirements: *70234733396920
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: pg
38
- requirement: &70234733396460 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ! '>='
@@ -43,10 +53,15 @@ dependencies:
43
53
  version: '0'
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *70234733396460
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: mysql2
49
- requirement: &70234733396040 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ! '>='
@@ -54,10 +69,15 @@ dependencies:
54
69
  version: '0'
55
70
  type: :development
56
71
  prerelease: false
57
- version_requirements: *70234733396040
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
58
78
  - !ruby/object:Gem::Dependency
59
79
  name: rspec-rails
60
- requirement: &70234733395540 !ruby/object:Gem::Requirement
80
+ requirement: !ruby/object:Gem::Requirement
61
81
  none: false
62
82
  requirements:
63
83
  - - ~>
@@ -65,32 +85,63 @@ dependencies:
65
85
  version: '2.8'
66
86
  type: :development
67
87
  prerelease: false
68
- version_requirements: *70234733395540
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: '2.8'
69
94
  - !ruby/object:Gem::Dependency
70
95
  name: timecop
71
- requirement: &70234733395040 !ruby/object:Gem::Requirement
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: '0.3'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
72
105
  none: false
73
106
  requirements:
74
107
  - - ~>
75
108
  - !ruby/object:Gem::Version
76
109
  version: '0.3'
110
+ - !ruby/object:Gem::Dependency
111
+ name: mongoid
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: '3.0'
77
118
  type: :development
78
119
  prerelease: false
79
- version_requirements: *70234733395040
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: '3.0'
80
126
  - !ruby/object:Gem::Dependency
81
127
  name: activerecord
82
- requirement: &70234733394580 !ruby/object:Gem::Requirement
128
+ requirement: !ruby/object:Gem::Requirement
83
129
  none: false
84
130
  requirements:
85
- - - ! '>='
131
+ - - ~>
86
132
  - !ruby/object:Gem::Version
87
- version: 2.0.0
133
+ version: '3.0'
88
134
  type: :runtime
89
135
  prerelease: false
90
- version_requirements: *70234733394580
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ~>
140
+ - !ruby/object:Gem::Version
141
+ version: '3.0'
91
142
  - !ruby/object:Gem::Dependency
92
143
  name: chronic
93
- requirement: &70234733394200 !ruby/object:Gem::Requirement
144
+ requirement: !ruby/object:Gem::Requirement
94
145
  none: false
95
146
  requirements:
96
147
  - - ! '>='
@@ -98,7 +149,12 @@ dependencies:
98
149
  version: '0'
99
150
  type: :runtime
100
151
  prerelease: false
101
- version_requirements: *70234733394200
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
102
158
  description: ActiveRecord extension for easier date scopes and time ranges
103
159
  email:
104
160
  - radarlistener@gmail.com
@@ -120,26 +176,33 @@ files:
120
176
  - lib/by_star/by_direction.rb
121
177
  - lib/by_star/by_fortnight.rb
122
178
  - lib/by_star/by_month.rb
179
+ - lib/by_star/by_quarter.rb
123
180
  - lib/by_star/by_week.rb
124
181
  - lib/by_star/by_weekend.rb
125
182
  - lib/by_star/by_year.rb
126
183
  - lib/by_star/instance_methods.rb
127
184
  - lib/by_star/time_ext.rb
128
185
  - lib/by_star/version.rb
129
- - spec/by_star/by_day_spec.rb
130
- - spec/by_star/by_direction_spec.rb
131
- - spec/by_star/by_fortnight_spec.rb
132
- - spec/by_star/by_month_spec.rb
133
- - spec/by_star/by_week_spec.rb
134
- - spec/by_star/by_weekend_spec.rb
135
- - spec/by_star/by_year_spec.rb
186
+ - lib/mongoid/by_star.rb
187
+ - spec/by_star/active_record/active_record_spec.rb
188
+ - spec/by_star/mongoid/mongoid_spec.rb
189
+ - spec/by_star/shared/by_day.rb
190
+ - spec/by_star/shared/by_direction.rb
191
+ - spec/by_star/shared/by_fortnight.rb
192
+ - spec/by_star/shared/by_month.rb
193
+ - spec/by_star/shared/by_quarter.rb
194
+ - spec/by_star/shared/by_week.rb
195
+ - spec/by_star/shared/by_weekend.rb
196
+ - spec/by_star/shared/by_year.rb
136
197
  - spec/database.yml
137
- - spec/fixtures/models.rb
138
- - spec/fixtures/schema.rb
198
+ - spec/fixtures/active_record/models.rb
199
+ - spec/fixtures/active_record/schema.rb
200
+ - spec/fixtures/mongoid/models.rb
201
+ - spec/fixtures/shared/seeds.rb
139
202
  - spec/spec_helper.rb
140
203
  - spec/time_ext_spec.rb
141
204
  - tmp/.gitignore
142
- homepage: http://rubygems.org/gems/by_star
205
+ homepage: http://github.com/radar/by_star
143
206
  licenses: []
144
207
  post_install_message:
145
208
  rdoc_options: []
@@ -159,8 +222,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
159
222
  version: 1.3.6
160
223
  requirements: []
161
224
  rubyforge_project: by_star
162
- rubygems_version: 1.8.15
225
+ rubygems_version: 1.8.25
163
226
  signing_key:
164
227
  specification_version: 3
165
228
  summary: ActiveRecord extension for easier date scopes and time ranges
166
229
  test_files: []
230
+ has_rdoc: