Permalink
Browse files

* Rename `between` to `between_times` internally, as Mongoid already …

…defines `between`. ActiveRecord has an alias of `between` to interface stays consistent.
  • Loading branch information...
1 parent 7bb77df commit b435d20adc9e8c956c50a9e95753a3368027f141 @johnnyshields johnnyshields committed Jan 11, 2014
View
9 CHANGELOG.md
@@ -9,9 +9,10 @@
* Consolidate all normalization/parsing functions into new Normalization module
* Remove meta-programming methods, e.g. `send("by_week_#{time_klass}")`
* Make Chronic gem optional; use it only if user has included it externally
-* by_week always returns a calendar week (i.e. beginning Monday or as specified by Rails setting), regardless of whether Date or Fixnum is given as a parameter.
-* by_week and by_calendar_month now supports optional :start_day option (:monday, :tuesday, etc)
-* Separate by_calendar_month into it's own class
-* by_weekend can now take a fixnum (parsing logic is same as by_week)
+* `by_week` always returns a calendar week (i.e. beginning Monday or as specified by Rails setting), regardless of whether Date or Fixnum is given as a parameter.
+* `by_week` and `by_calendar_month` now supports optional `:start_day` option (:monday, :tuesday, etc)
+* Separate `by_calendar_month` into it's own class
+* Rename `between` to `between_times` internally, as Mongoid already defines `between`. ActiveRecord has an alias of `between` to interface stays consistent.
+* `by_weekend` can now take a fixnum (parsing logic is same as by_week)
* Add Time kernel extensions for fortnight and calendar_month
* Add Johnny Shields as a gem co-author
View
5 lib/by_star/base.rb
@@ -11,6 +11,11 @@ module Base
include ByStar::ByDay
include ByStar::ByQuarter
+ def between_times(start, finish, options={})
+ offset = (options[:offset] || 0).seconds
+ between_times_query(start + offset, finish + offset, options)
+ end
+
def by_star_field(start_field = nil, end_field = nil)
@by_star_start_field ||= start_field
@by_star_end_field ||= end_field
View
2 lib/by_star/by_calendar_month.rb
@@ -5,7 +5,7 @@ def by_calendar_month(*args)
with_by_star_options(*args) do |time, options|
time = ByStar::Normalization.month(time, options)
start_day = Array(options[:start_day])
- between(time.beginning_of_calendar_month(*start_day), time.end_of_calendar_month(*start_day), options)
+ between_times(time.beginning_of_calendar_month(*start_day), time.end_of_calendar_month(*start_day), options)
end
end
end
View
2 lib/by_star/by_day.rb
@@ -4,7 +4,7 @@ module ByDay
def by_day(*args)
with_by_star_options(*args) do |time, options|
time = ByStar::Normalization.time(time)
- between(time.beginning_of_day, time.end_of_day, options)
+ between_times(time.beginning_of_day, time.end_of_day, options)
end
end
View
2 lib/by_star/by_fortnight.rb
@@ -4,7 +4,7 @@ module ByFortnight
def by_fortnight(*args)
with_by_star_options(*args) do |time, options|
time = ByStar::Normalization.fortnight(time, options)
- between(time.beginning_of_fortnight, time.end_of_fortnight, options)
+ between_times(time.beginning_of_fortnight, time.end_of_fortnight, options)
end
end
end
View
2 lib/by_star/by_month.rb
@@ -4,7 +4,7 @@ module ByMonth
def by_month(*args)
with_by_star_options(*args) do |time, options|
time = ByStar::Normalization.month(time, options)
- between(time.beginning_of_month, time.end_of_month, options)
+ between_times(time.beginning_of_month, time.end_of_month, options)
end
end
end
View
2 lib/by_star/by_quarter.rb
@@ -4,7 +4,7 @@ module ByQuarter
def by_quarter(*args)
with_by_star_options(*args) do |time, options|
time = ByStar::Normalization.quarter(time, options)
- between(time.beginning_of_quarter, time.end_of_quarter, options)
+ between_times(time.beginning_of_quarter, time.end_of_quarter, options)
end
end
end
View
2 lib/by_star/by_week.rb
@@ -5,7 +5,7 @@ def by_week(*args)
with_by_star_options(*args) do |time, options|
time = ByStar::Normalization.week(time, options)
start_day = Array(options[:start_day])
- between(time.beginning_of_week(*start_day), time.end_of_week(*start_day), options)
+ between_times(time.beginning_of_week(*start_day), time.end_of_week(*start_day), options)
end
end
end
View
2 lib/by_star/by_weekend.rb
@@ -4,7 +4,7 @@ module ByWeekend
def by_weekend(*args)
with_by_star_options(*args) do |time, options|
time = ByStar::Normalization.week(time, options)
- between(time.beginning_of_weekend, time.end_of_weekend, options)
+ between_times(time.beginning_of_weekend, time.end_of_weekend, options)
end
end
end
View
2 lib/by_star/by_year.rb
@@ -4,7 +4,7 @@ module ByYear
def by_year(*args)
with_by_star_options(*args) do |time, options|
time = ByStar::Normalization.year(time, options)
- between(time.beginning_of_year, time.end_of_year, options)
+ between_times(time.beginning_of_year, time.end_of_year, options)
end
end
end
View
11 lib/by_star/orm/active_record/by_star.rb
@@ -8,18 +8,17 @@ module ClassMethods
# Returns all records between a given start and finish time.
#
# Currently only supports Time objects.
- def between(start, finish, options={})
- start_field = by_star_start_field
- end_field = by_star_end_field
+ def between_times_query(start, finish, options={})
scope = if options[:strict] || by_star_start_field == by_star_end_field
- where("#{start_field} >= ? AND #{end_field} <= ?", start, finish)
+ where("#{by_star_start_field} >= ? AND #{by_star_end_field} <= ?", start, finish)
else
- where("#{end_field} > ? AND #{start_field} < ?", start, finish)
+ where("#{by_star_end_field} > ? AND #{by_star_start_field} < ?", start, finish)
end
scope = scope.order(options[:order]) if options[:order]
scope
end
- alias_method :between_times, :between
+
+ alias_method :between, :between_times
protected
View
9 lib/by_star/orm/mongoid/by_star.rb
@@ -9,18 +9,15 @@ module ByStar
module ClassMethods
include ::ByStar::Base
- def between(start, finish, options={})
- start_field = by_star_start_field
- end_field = by_star_end_field
+ def between_times_query(start, finish, options={})
scope = if options[:strict] || by_star_start_field == by_star_end_field
- gte(start_field => start).lte(end_field => finish)
+ gte(by_star_start_field => start).lte(by_star_end_field => finish)
else
- gt(end_field => start).lt(start_field => finish)
+ gt(by_star_end_field => start).lt(by_star_start_field => finish)
end
scope = scope.order_by(field => options[:order]) if options[:order]
scope
end
- alias_method :between_times, :between
def by_star_end_field_with_mongoid(options = {})
database_field_name by_star_end_field_without_mongoid(options)
View
12 spec/integration/mongoid/mongoid_spec.rb
@@ -27,18 +27,18 @@
it_behaves_like "by weekend"
it_behaves_like "by year"
- describe "#between" do
+ describe "#between_times" do
it "should return a Mongoid::Critera object" do
- Post.between(Date.today - 2, Date.today).class.should == Mongoid::Criteria
+ Post.between_times(Date.today - 2, Date.today).class.should == Mongoid::Criteria
end
it "should return a result set between two times" do
- Post.between(Date.today - 2, Date.today).count.should == 1
+ Post.between_times(Date.today - 2, Date.today).count.should == 1
end
end
- describe "#between_times" do
- it "should be an alias of #between" do
- Post.between_times(Date.today - 2, Date.today).should == Post.between(Date.today - 2, Date.today)
+ describe "#between" do
+ it "should not override Origin#between method" do
+ Post.between(created_at: (Date.today - 2)..Date.today).count.should == 1
end
end
end if testing_mongoid?
View
3 spec/spec_helper.rb
@@ -16,8 +16,7 @@
# Define time zone before loading test_helper
-zone = "UTC"
-Time.zone = zone
+Time.zone = 'UTC'
# Freeze time to Jan 1st of this year
Timecop.travel(Time.zone.local(Time.zone.now.year, 1, 1, 0, 0, 1, 0))

0 comments on commit b435d20

Please sign in to comment.