Class: TimeCrisis::DateRange
- Defined in:
- lib/time_crisis/date_range.rb
Defined Under Namespace
Modules: Date
Instance Method Summary collapse
- #each_month(&block) ⇒ Object
- #each_slice_by_date(*args) {|self.class.new(start, self.end, self.exclude_end?, self.subopt)| ... } ⇒ Object
- #each_slice_by_period(period, unit = 1) {|self.class.new(start, self.end, self.exclude_end?, self.subopt)| ... } ⇒ Object
- #each_year(&block) ⇒ Object
- #include?(datelike) ⇒ Boolean
-
#initialize(*args) ⇒ DateRange
constructor
A new instance of DateRange.
Constructor Details
#initialize(*args) ⇒ DateRange
Returns a new instance of DateRange.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/time_crisis/date_range.rb', line 2 def initialize(*args) = args.last.is_a?(Hash) ? args.pop : {} # make compatibile with Range's normal arguments [:begin] = args.shift unless args.empty? [:end] = args.shift unless args.empty? [:exclude_end] = args.shift unless args.empty? start = if [:begin].nil? false elsif [:begin].is_a?(TimeCrisis::Date) [:begin] else [:begin].to_tc_date end stop = if [:end].nil? false elsif [:end].is_a?(TimeCrisis::Date) [:end] else [:end].to_tc_date end unit = [:unit] || 1 scale = [:scale] || 'years' scale = scale.pluralize if scale.respond_to?(:pluralize) if start && !stop stop = start.advance({scale.to_sym => unit, :days => -1}) elsif !start && stop start = stop.advance({scale.to_sym => -unit, :days => 1}) end super(start, stop, [:exclude_end] || false) end |
Instance Method Details
#each_month(&block) ⇒ Object
76 77 78 |
# File 'lib/time_crisis/date_range.rb', line 76 def each_month(&block) each_slice_by_period(:months, 1, &block) end |
#each_slice_by_date(*args) {|self.class.new(start, self.end, self.exclude_end?, self.subopt)| ... } ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/time_crisis/date_range.rb', line 43 def each_slice_by_date(*args, &block) dates = args.map! do |datelike| date = datelike.to_tc_date raise ArgumentError, "Date not within range: #{date}" unless self.include?(date) date end dates.sort! start = self.begin dates.each do |date| yield self.class.new(start, date, true, self.subopt) start = date end yield self.class.new(start, self.end, self.exclude_end?, self.subopt) end |
#each_slice_by_period(period, unit = 1) {|self.class.new(start, self.end, self.exclude_end?, self.subopt)| ... } ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/time_crisis/date_range.rb', line 61 def each_slice_by_period(period, unit=1, &block) start = self.begin nstart = start.advance(period.to_sym => unit) raise ArgumentError, "Period exceeds range" if nstart >= self.real_end while nstart < self.real_end yield self.class.new(start, nstart, true, self.subopt) start = nstart nstart = start.advance(period.to_sym => unit) end yield self.class.new(start, self.end, self.exclude_end?, self.subopt) end |
#each_year(&block) ⇒ Object
80 81 82 |
# File 'lib/time_crisis/date_range.rb', line 80 def each_year(&block) each_slice_by_period(:years, 1, &block) end |
#include?(datelike) ⇒ Boolean
39 40 41 |
# File 'lib/time_crisis/date_range.rb', line 39 def include?(datelike) super(datelike.to_tc_date) end |