Skip to content
Browse files

timespanned macros rules :)

  • Loading branch information...
1 parent a22b7c4 commit 6cef32d9032107c49cc3f7a0e8a3f9b54a34b82f @kristianmandrup committed
Showing with 40 additions and 0 deletions.
  1. +29 −0 lib/timespan/mongoid/timespanned.rb
  2. +11 −0 spec/timespan/mongoid/models/time_period.rb
View
29 lib/timespan/mongoid/timespanned.rb
@@ -3,6 +3,35 @@ module Timespanned
extend ActiveSupport::Concern
module ClassMethods
+ def asap_method path
+ define_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}]
+ 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}]
+ end
+
+ define_method :at_least do |period|
+ period.kind_of?(Integer) ? period : period.to_i
+ {:"#{path}.to".gte => period }
+ end
+
+ define_method :at_most do |period|
+ period.kind_of?(Integer) ? period : period.to_i
+ {:"#{path}.to".lte => period }
+ end
+ end
+
# fx Account.timespan_container_delegates :period, :dates, :start, :end
# start_date= -> period.dates_start=
# end_date= -> period.dates_end=
View
11 spec/timespan/mongoid/models/time_period.rb
@@ -8,4 +8,15 @@ class TimePeriod
embedded_in :account
timespan_methods :dates
+
+ def max_asap
+ 10.days.from_now.to_i
+ end
+
+ def min_asap
+ 1.day.ago.to_i
+ end
+
+ asap_method :period
+ duration_methods 'time_period.flex'
end

0 comments on commit 6cef32d

Please sign in to comment.
Something went wrong with that request. Please try again.