lib/calrom/date_range.rb in calrom-0.3.0 vs lib/calrom/date_range.rb in calrom-0.4.0
- old
+ new
@@ -11,20 +11,24 @@
(Month.new(first.year, first.month) .. Month.new(last.year, last.month))
.each_with_index do |m,i|
if i == 0 && first.day > 1
# first month, incomplete
- end_of_month = first.next_month - first.day + 1
yield self.class.new(first, m.last)
elsif m.first.year == last.year && m.first.month == last.month && last != m.last
# last month, incomplete
yield self.class.new(m.first, last)
else
yield m
end
end
end
+
+ def spans_multiple_months?
+ first.month != last.month ||
+ first.year != last.year
+ end
end
class Year < DateRange
def initialize(year)
super Date.new(year, 1, 1), Date.new(year, 12, 31)
@@ -39,15 +43,34 @@
1.upto(12) {|month| yield Month.new(first.year, month) }
end
end
+ class ThreeMonths < DateRange
+ def initialize(year, month)
+ super first_day_of_last_month(year, month), last_day_of_next_month(year, month)
+ end
+
+ private
+
+ def first_day_of_last_month(year, month)
+ Date.new(year, month, 1).prev_month
+ end
+
+ def last_day_of_next_month(year, month)
+ n = Date.new(year, month).next_month
+
+ Date.new(n.year, n.month, -1)
+ end
+ end
+
class Month < DateRange
def initialize(year, month)
@year = year
@month = month
- super Date.new(year, month, 1), next_month_beginning - 1
+
+ super Date.new(year, month, 1), Date.new(year, month, -1)
end
def to_s
first.strftime '%B %Y'
end
@@ -57,11 +80,11 @@
yield self
end
def succ
- n = next_month_beginning
+ n = Date.new(@year, @month, 1).next_month
self.class.new(n.year, n.month)
end
def <=>(other)
years_cmp = year <=> other.year
@@ -74,19 +97,9 @@
end
protected
attr_reader :year, :month
-
- private
-
- def next_month_beginning
- if @month == 12
- Date.new(@year + 1, 1, 1)
- else
- Date.new(@year, @month + 1, 1)
- end
- end
end
class Day < DateRange
def initialize(date)
super date, date