Browse files

- Add field-level :offset parameter

- Correct specs for Chronic, including fallback specs
  • Loading branch information...
1 parent c1382a6 commit 9a47562d0f137db6eb376a07d126c8a97d98d23f @johnnyshields johnnyshields committed Jan 12, 2014
View
9 lib/by_star/base.rb
@@ -12,13 +12,18 @@ module Base
include ByStar::ByQuarter
def between_times(start, finish, options={})
- offset = (options[:offset] || 0).seconds
+ offset = by_star_offset(options)
between_times_query(start + offset, finish + offset, options)
end
- def by_star_field(start_field = nil, end_field = nil)
+ def by_star_field(start_field = nil, end_field = nil, options = {})
@by_star_start_field ||= start_field
@by_star_end_field ||= end_field
+ @by_star_offset ||= options[:offset]
+ end
+
+ def by_star_offset(options = {})
+ (options[:offset] || @by_star_offset || 0).seconds
end
def by_star_start_field(options={})
View
6 lib/by_star/normalization.rb
@@ -21,13 +21,11 @@ def time_string(value)
def time_string_chronic(value)
Chronic.time_class = Time.zone
- time = Chronic.parse(value)
- raise ByStar::ParseError, "Chronic could not parse #{value.inspect}" unless time
- time
+ Chronic.parse(value) || raise(ByStar::ParseError, "Chronic could not parse String #{value.inspect}")
end
def time_string_fallback(value)
- Time.zone.parse(value)
+ Time.zone.parse(value) || raise(ByStar::ParseError, "Cannot parse String #{value.inspect}")
end
def week(value, options={})
View
2 spec/fixtures/active_record/models.rb
@@ -2,5 +2,5 @@ class Post < ActiveRecord::Base
end
class Event < ActiveRecord::Base
- by_star_field :start_time, :end_time
+ by_star_field :start_time, :end_time, offset: 3.hours
end
View
2 spec/fixtures/mongoid/models.rb
@@ -11,5 +11,5 @@ class Event
field :st, as: :start_time, type: Time
field :end_time, type: Time
- by_star_field :start_time, :end_time
+ by_star_field :start_time, :end_time, offset: 3.hours
end
View
1 spec/integration/active_record/active_record_spec.rb
@@ -28,6 +28,7 @@
it_behaves_like 'by week'
it_behaves_like 'by weekend'
it_behaves_like 'by year'
+ it_behaves_like 'offset parameter'
it 'should be able to order the result set' do
scope = Post.by_year(Time.zone.now.year, :order => 'created_at DESC')
View
1 spec/integration/mongoid/mongoid_spec.rb
@@ -27,6 +27,7 @@
it_behaves_like 'by week'
it_behaves_like 'by weekend'
it_behaves_like 'by year'
+ it_behaves_like 'offset parameter'
describe '#between_times' do
subject { Post.between_times(Time.parse('2014-01-01'), Time.parse('2014-01-06')) }
View
31 spec/integration/shared/offset_parameter.rb
@@ -0,0 +1,31 @@
+require 'spec_helper'
+
+shared_examples_for 'offset parameter' do
+
+ describe 'offset' do
+ it 'should memoize the offset variable' do
+ Event.instance_variable_get(:@by_star_offset).should eq 3.hours
+ Post.instance_variable_get(:@by_star_offset).should be_nil
+ end
+
+ context 'between_times with model offset' do
+ subject { Event.between_times(Time.parse('2014-01-01'), Time.parse('2014-01-10')) }
+ its(:count) { should eq 6 }
+ end
+
+ context 'between_times with offset override' do
+ subject { Event.between_times(Time.parse('2014-01-01'), Time.parse('2014-01-10'), offset: 16.hours) }
+ its(:count) { should eq 6 }
+ end
+
+ context 'by_day with model offset' do
+ subject { Event.by_day(Time.parse('2014-01-01')) }
+ its(:count) { should eq 4 }
+ end
+
+ context 'by_day with offset override' do
+ subject { Event.by_day(Time.parse('2014-12-26'), field: :start_time, offset: 5.hours) }
+ its(:count) { should eq 0 }
+ end
+ end
+end
View
3 spec/spec_helper.rb
@@ -9,11 +9,12 @@
require 'active_record'
require 'mongoid'
+require 'chronic'
require 'timecop'
require 'by_star'
# Specs should pass regardless of timezone
-Time.zone = %w(Asia/Tokyo America/New_York UTC).sample
+Time.zone = %w(Asia/Tokyo America/New_York Australia/Sydney UTC).sample
puts "Running specs in #{Time.zone} timezone..."
# Set Rails time to 2014-01-01 00:00:00
View
55 spec/unit/normalization_spec.rb
@@ -6,14 +6,42 @@
shared_examples_for 'time normalization from string' do
- context 'date String' do
- let(:input){ '2014-01-01' }
- it { should eq Time.zone.parse('2014-01-01 12:00:00') }
+ context 'when Chronic is defined' do
+
+ context 'date String' do
+ let(:input){ '2014-01-01' }
+ it { should eq Time.zone.parse('2014-01-01 12:00:00') }
+ end
+
+ context 'time String' do
+ let(:input){ '2014-01-01 15:00:00' }
+ it { should eq Time.zone.parse('2014-01-01 15:00:00') }
+ end
+
+ context 'natural language String' do
+ let(:input){ 'tomorrow at 3:30 pm' }
+ it { should eq Time.zone.parse('2014-01-02 15:30:00') }
+ end
end
- context 'time String' do
- let(:input){ '2014-01-01 15:00:00' }
- it { should eq Time.zone.parse('2014-01-01 15:00:00') }
+ context 'when Chronic is not defined' do
+
+ before { hide_const('Chronic') }
+
+ context 'date String' do
+ let(:input){ '2014-01-01' }
+ it { should eq Time.zone.parse('2014-01-01 00:00:00') }
+ end
+
+ context 'time String' do
+ let(:input){ '2014-01-01 15:00:00' }
+ it { should eq Time.zone.parse('2014-01-01 15:00:00') }
+ end
+
+ context 'natural language String' do
+ let(:input){ 'tomorrow at noon' }
+ it { ->{subject}.should raise_error(ByStar::ParseError, "Cannot parse String #{input.inspect}") }
+ end
end
end
@@ -24,7 +52,7 @@
end
context 'DateTime' do
- let(:input){ DateTime.parse("2014-01-01 15:00:00 #{Time.zone}") }
+ let(:input){ Time.zone.parse('2014-01-01 15:00:00').to_datetime }
it { should eq Time.zone.parse('2014-01-01 15:00:00') }
end
@@ -40,19 +68,6 @@
it_behaves_like 'time normalization from date/time'
end
- describe '#time_string_fallback' do
-
- it 'should parse date String to Time at beginning of day' do
- d = '2014-01-01'
- subject.time_string_fallback(d).should eq Time.zone.parse('2014-01-01 00:00:00')
- end
-
- it 'should parse time String to Time' do
- dt = '2014-01-01 15:00:00'
- subject.time_string_fallback(dt).should eq Time.zone.parse('2014-01-01 15:00:00')
- end
- end
-
describe '#week' do
subject { ByStar::Normalization.week(input, options) }
it_behaves_like 'time normalization from string'

0 comments on commit 9a47562

Please sign in to comment.