Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

using class lv timespan search macros for adv search :)

  • Loading branch information...
commit 0de96babc40a39295bff953d05ae744befbd9dcc 1 parent 6cef32d
Kristian Mandrup authored
33 lib/timespan/mongoid/timespanned.rb
View
@@ -3,32 +3,37 @@ module Timespanned
extend ActiveSupport::Concern
module ClassMethods
+ attr_writer :max_asap, :min_asap
+
+ def max_asap time = nil
+ @max_asap ||= (time || 10.days.from_now).to_i
+ end
+
+ def min_asap time = nil
+ @min_asap ||= (time || 1.day.ago).to_i
+ end
+
def asap_method path
- define_method :asap do
+ define_singleton_method :asap do
{:"#{path}.from".gte => min_asap, :"#{path}.from".lte => max_asap}
end
end
def duration_methods path
- define_method :exactly do |period|
- period.kind_of?(Integer) ? period : period.to_i
- [{:"#{path}.from" => period}, {:"#{path}.to" => period}]
+ define_singleton_method :exactly do |period|
+ [{:"#{path}.from" => period.to_i}, {:"#{path}.to" => period.to_i}]
end
- define_method :in_between do |range|
- range_min = range.min.kind_of?(Integer) ? range.min : range.min.to_i
- range_max = range.max.kind_of?(Integer) ? range.max : range.max.to_i
- [{:"#{path}.from" => range_min}, {:"#{path}.to" => range_max}]
+ define_singleton_method :in_between do |range|
+ [{:"#{path}.from" => range.min.to_i}, {:"#{path}.to" => range.max.to_i}]
end
- define_method :at_least do |period|
- period.kind_of?(Integer) ? period : period.to_i
- {:"#{path}.to".gte => period }
+ define_singleton_method :at_least do |period|
+ {:"#{path}.to".gte => period.to_i }
end
- define_method :at_most do |period|
- period.kind_of?(Integer) ? period : period.to_i
- {:"#{path}.to".lte => period }
+ define_singleton_method :at_most do |period|
+ {:"#{path}.to".lte => period.to_i }
end
end
61 spec/timespan/mongoid/advanced_search_spec.rb
View
@@ -54,35 +54,11 @@ def max_asap
time_period: TimePeriod.new(flex: (3..6).minutes)
end
- def yesterday
- 1.day.ago.to_i
- end
-
- def where_hash
- {:'period.from'.gte => yesterday, :'period.from'.lte => max_asap}
- end
-
- def exactly
- [{:'time_period.flex.from' => period}, {:'time_period.flex.to' => period}]
- end
-
- def in_between
- [{:'time_period.flex.from' => min_period}, {:'time_period.flex.to' => max_period}]
- end
-
- def at_least type = :min
- {:'time_period.flex.to'.gte => send("#{type}_period") }
- end
-
- def at_most type = :max
- {:'time_period.flex.to'.lte => send("#{type}_period") }
- end
-
describe 'ASAP 2 minutes' do
let(:period) { 2.minutes.to_i }
let(:criteria) do
- Account.where(where_hash).or(exactly)
+ Account.where(TimePeriod.asap).or(TimePeriod.exactly period)
end
# it 'should have a nice criteria' do
@@ -90,6 +66,14 @@ def at_most type = :max
# criteria.selector["period.from"].should be_a Hash
# end
+ it 'should have custom max_asap' do
+ TimePeriod.max_asap.should == 14.days.from_now.to_i
+ end
+
+ it 'should have custom min_asap' do
+ TimePeriod.min_asap.should == 2.days.ago.to_i
+ end
+
it 'should find #1, #2, #3, #4, #5' do
criteria.to_a.map(&:name).should include '1', '2', '5', '7'
end
@@ -107,7 +91,7 @@ def at_most type = :max
let(:period) { 3.minutes.to_i }
let(:criteria) do
- Account.where(where_hash).or(exactly)
+ Account.where(TimePeriod.asap).or(TimePeriod.exactly period)
end
it 'should find #4, #10' do
@@ -120,7 +104,7 @@ def at_most type = :max
let(:period) { 4.minutes.to_i }
let(:criteria) do
- Account.where(where_hash).or(exactly)
+ Account.where(TimePeriod.asap).or(TimePeriod.exactly period)
end
it 'should find #2, #3' do
@@ -129,11 +113,10 @@ def at_most type = :max
end
describe 'ASAP 1-4 minutes' do
- let(:min_period) { 1.minute }
- let(:max_period) { 4.minutes }
+ let(:period) { 1.minute..4.minutes }
let(:criteria) do
- Account.where(where_hash).or(in_between)
+ Account.where(TimePeriod.asap).or(TimePeriod.in_between period)
end
it 'should find #2, #3' do
@@ -146,8 +129,7 @@ def at_most type = :max
end
describe 'ASAP 3-5 minutes' do
- let(:min_period) { 3.minutes }
- let(:max_period) { 5.minutes }
+ let(:period) { 3.minutes..5.minutes }
it 'should have a nice criteria' do
# puts "ASAP 3-5 minutes"
@@ -158,7 +140,7 @@ def at_most type = :max
end
let(:criteria) do
- Account.where(where_hash).or(in_between)
+ Account.where(TimePeriod.asap).or(TimePeriod.in_between period)
end
it 'should find #2, #3' do
@@ -169,11 +151,10 @@ def at_most type = :max
end
describe 'ASAP 5-7 minutes' do
- let(:min_period) { 5.minutes }
- let(:max_period) { 7.minutes }
+ let(:period) { 5.minutes..7.minutes }
let(:criteria) do
- Account.where(where_hash).or(in_between)
+ Account.where(TimePeriod.asap).or(TimePeriod.in_between period)
end
it 'should find #2, #3' do
@@ -184,10 +165,10 @@ def at_most type = :max
end
describe 'ASAP at least 4 minutes' do
- let(:min_period) { 4.minutes }
+ let(:period) { 4.minutes }
let(:criteria) do
- Account.where where_hash.merge(at_least)
+ Account.where TimePeriod.asap.merge(TimePeriod.at_least period)
end
it 'should find #2, #3' do
@@ -199,10 +180,10 @@ def at_most type = :max
end
describe 'ASAP at most 3 minutes' do
- let(:max_period) { 3.minutes }
+ let(:period) { 3.minutes }
let(:criteria) do
- Account.where where_hash.merge(at_most)
+ Account.where TimePeriod.asap.merge(TimePeriod.at_most period)
end
it 'should find #2, #3' do
10 spec/timespan/mongoid/models/time_period.rb
View
@@ -9,13 +9,9 @@ class TimePeriod
timespan_methods :dates
- def max_asap
- 10.days.from_now.to_i
- end
-
- def min_asap
- 1.day.ago.to_i
- end
+ # override defaults
+ max_asap 14.days.from_now
+ min_asap 2.days.ago
asap_method :period
duration_methods 'time_period.flex'
Please sign in to comment.
Something went wrong with that request. Please try again.