Class: Date
- Inherits:
-
Object
- Object
- Date
- Defined in:
- lib/date_plus.rb
Instance Method Summary collapse
- #end_of_month ⇒ Object
- #end_of_week ⇒ Object
- #end_of_year ⇒ Object
- #future_instance_of_day(day, instances_away = 1) ⇒ Object
- #future_instance_of_month(month_name, instances_away = 1) ⇒ Object
- #future_instance_of_weekday(weekday, instances_away = 1) ⇒ Object
- #future_year(instances_away) ⇒ Object
- #month_name ⇒ Object
-
#next_instance_of(options) ⇒ Object
options can accept any method that responds to date so long as the criteria matches the output of that method.
- #start_of_month ⇒ Object
- #start_of_week ⇒ Object
- #start_of_year ⇒ Object
- #weekday_name ⇒ Object
Instance Method Details
#end_of_month ⇒ Object
30 31 32 |
# File 'lib/date_plus.rb', line 30 def end_of_month Date.civil(year, month, -1) end |
#end_of_week ⇒ Object
26 27 28 |
# File 'lib/date_plus.rb', line 26 def end_of_week self + (6 - cwday) end |
#end_of_year ⇒ Object
34 35 36 |
# File 'lib/date_plus.rb', line 34 def end_of_year Date.civil(year, -1, -1) end |
#future_instance_of_day(day, instances_away = 1) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/date_plus.rb', line 53 def future_instance_of_day(day, instances_away=1) valid_day_or_error(day) fixnum_or_nil_or_error(instances_away) self.step(self + (31 * instances_away), step=+1).reverse_each do |current_day| return current_day if current_day.day === day end end |
#future_instance_of_month(month_name, instances_away = 1) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/date_plus.rb', line 61 def future_instance_of_month(month_name, instances_away=1) valid_month_or_error(month_name) fixnum_or_nil_or_error(instances_away) months = [] first_of_month = Date.today.start_of_month (instances_away * 12).times do months.push(first_of_month) first_of_month = first_of_month.next_month end months.reverse.each {|first_of_month| return first_of_month if first_of_month.month == Date.parse(month_name).month } end |
#future_instance_of_weekday(weekday, instances_away = 1) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/date_plus.rb', line 45 def future_instance_of_weekday(weekday, instances_away=1) raise 'Argument passed is not a weekday' unless weekday.class == String || !weekday_names.include?(weekday.downcase.capitalize) fixnum_or_nil_or_error(instances_away) self.step(self + (7 * instances_away), step=+1).reverse_each do |current_day| return current_day if current_day.weekday_name == weekday.downcase.capitalize end end |
#future_year(instances_away) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/date_plus.rb', line 73 def future_year(instances_away) fixnum_nil_or_error(instances_away) year = Date.today.start_of_year years = [] (instances_away + 1).times {years.push(year); year = year.next_year} return years.last end |
#month_name ⇒ Object
10 11 12 |
# File 'lib/date_plus.rb', line 10 def month_name self.strftime('%B') end |
#next_instance_of(options) ⇒ Object
options can accept any method that responds to date so long as the criteria matches the output of that method
40 41 42 43 |
# File 'lib/date_plus.rb', line 40 def next_instance_of() .each { |k, v| [k] = v.downcase.capitalize if v.class == String } return check_dates_by_year(self, end_of_year, ) end |
#start_of_month ⇒ Object
18 19 20 |
# File 'lib/date_plus.rb', line 18 def start_of_month self - day + 1 end |
#start_of_week ⇒ Object
14 15 16 |
# File 'lib/date_plus.rb', line 14 def start_of_week self - cwday end |
#start_of_year ⇒ Object
22 23 24 |
# File 'lib/date_plus.rb', line 22 def start_of_year self - yday + 1 end |
#weekday_name ⇒ Object
6 7 8 |
# File 'lib/date_plus.rb', line 6 def weekday_name self.strftime('%A') end |