class Range
Constants
- DAYS
- DEFAULT_TIME_GRANULARITY
- HOURS
Adds some enumerable capabilities to Time ranges
(Normally they raise a “Can't iterate time ranges”)
It works by assuming days while iterating the time range, but you can pass an optional parameter
Public Instance Methods
collect(step = DEFAULT_TIME_GRANULARITY) { |current| ... }
click to toggle source
Map / Collect over a Time range. A better implementation would be redefining 'succ' on Time. However, the ruby source code (At least 1.9.2-p0) hardcodes a check for Type,
so it would not work even if we provide our own 'succ' for Time
.
Calls superclass method
# File lib/mongoid/tracking/core_ext/range.rb, line 17 def collect(step = DEFAULT_TIME_GRANULARITY) return super() unless first.is_a?(Time) return collect(step) {|c| c} unless block_given? # Pretty much a standard implementation of Map/Collect here ary, current, op = [], first, (exclude_end? ? :< : :<=) while current.send(op, last) ary << yield(current) current = current + step end ary end
Also aliased as: map
diff(granularity = DEFAULT_TIME_GRANULARITY)
click to toggle source
Diff returns the number of elements in the Range
, much like 'count'. Again, redefining 'succ' would be a better idea (see above). However, I think redefining 'succ' makes this O(n) while this is O(1)
# File lib/mongoid/tracking/core_ext/range.rb, line 35 def diff(granularity = DEFAULT_TIME_GRANULARITY) if first.is_a?(Time) @diff ||= (last - first) / granularity + (exclude_end? ? 0 : 1) @diff.to_i else @diff ||= count end end
hour_collect(&block)
click to toggle source
# File lib/mongoid/tracking/core_ext/range.rb, line 49 def hour_collect(&block) collect(HOURS, &block) end
Also aliased as: hour_map
hour_diff()
click to toggle source
Helper methods for non default parameters
# File lib/mongoid/tracking/core_ext/range.rb, line 45 def hour_diff diff(HOURS) end