Skip to content

Commit

Permalink
timespanned macros rules :)
Browse files Browse the repository at this point in the history
  • Loading branch information
kristianmandrup committed Sep 25, 2012
1 parent a22b7c4 commit 6cef32d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/timespan/mongoid/timespanned.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,6 +3,35 @@ module Timespanned
extend ActiveSupport::Concern extend ActiveSupport::Concern


module ClassMethods 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 # fx Account.timespan_container_delegates :period, :dates, :start, :end
# start_date= -> period.dates_start= # start_date= -> period.dates_start=
# end_date= -> period.dates_end= # end_date= -> period.dates_end=
Expand Down
11 changes: 11 additions & 0 deletions spec/timespan/mongoid/models/time_period.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,4 +8,15 @@ class TimePeriod
embedded_in :account embedded_in :account


timespan_methods :dates 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 end

0 comments on commit 6cef32d

Please sign in to comment.