Class: Date

Inherits:
Object show all
Defined in:
lib/qualitysmith_extensions/date/month_ranges.rb,
lib/qualitysmith_extensions/date/iso8601.rb,
lib/qualitysmith_extensions/date/deprecated.rb

Overview

Attempts in part to make the Date class do everything (or at least some of the things) that the Time class can do (ActiveSupport::CoreExtensions::Time::Calculations). Maybe that would be better accomplished with a method_missing() that calls to_time.send(:the_same_method) if Time.respond_to?(:that_method).

Instance Method Summary collapse

Instance Method Details

#date_for_reportObject

These should be moved elsewhere because they are subjective and depend on the context where they’re used.



15
16
17
# File 'lib/qualitysmith_extensions/date/deprecated.rb', line 15

def date_for_report
  strftime("%b %d, %Y")   # Example: "Jun 18, 2006"
end

#iso8601Object



13
14
15
16
# File 'lib/qualitysmith_extensions/date/iso8601.rb', line 13

def iso8601
  # Useful for SQL dates, among other things
  to_time().iso8601()[0..9]
end

#month_for_reportObject



18
19
20
# File 'lib/qualitysmith_extensions/date/deprecated.rb', line 18

def month_for_report
  strftime("%B %Y")   # Example: "June 2006"
end

#month_step(max, step, &block) ⇒ Object

This is based on the implementation of Time.step. The main difference is that it uses >>= (increment by 1 month) instead of += (increment by 1 day).



18
19
20
21
22
23
24
25
26
# File 'lib/qualitysmith_extensions/date/month_ranges.rb', line 18

def month_step(max, step, &block)  # { |date| ...}
  time = self
  op = [:-,:<=,:>=][step<=>0]
  while time.__send__(op, max)
    block.call time
    time >>= step
  end
  self
end

#months_upto(max, &block) ⇒ Object

Step forward one month at a time until we reach max (inclusive), yielding each date as we go



29
30
31
# File 'lib/qualitysmith_extensions/date/month_ranges.rb', line 29

def months_upto(max, &block)  # { |date| ...}
  month_step(max, +1, &block)
end

#next_monthObject



37
38
39
40
# File 'lib/qualitysmith_extensions/date/month_ranges.rb', line 37

def next_month
  # Uses http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Time/Calculations.html#M000336
  self.to_time.next_month
end

#to_monthObject



33
34
35
# File 'lib/qualitysmith_extensions/date/month_ranges.rb', line 33

def to_month
  Month.new(year, month)
end