lib/timespan/mongoid/timespanned.rb in timespan-0.4.9 vs lib/timespan/mongoid/timespanned.rb in timespan-0.5.0

- old
+ new

@@ -1,9 +1,43 @@ module Mongoid 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_singleton_method :asap do + {:"#{path}.from".gte => min_asap, :"#{path}.from".lte => max_asap} + end + end + + def duration_methods path + define_singleton_method :exactly do |period| + [{:"#{path}.from" => period.to_i}, {:"#{path}.to" => period.to_i}] + end + + define_singleton_method :in_between do |range| + [{:"#{path}.from" => range.min.to_i}, {:"#{path}.to" => range.max.to_i}] + end + + define_singleton_method :at_least do |period| + {:"#{path}.to".gte => period.to_i } + end + + define_singleton_method :at_most do |period| + {:"#{path}.to".lte => period.to_i } + end + end + # fx Account.timespan_container_delegates :period, :dates, :start, :end # start_date= -> period.dates_start= # end_date= -> period.dates_end= def timespan_container_delegates container, timespan, *names options = names.extract_options! \ No newline at end of file