Module: RST::Calendar::CalendarHelper

Included in:
Calendar, CalendarEvent
Defined in:
lib/modules/calendar/calendar.rb

Overview

Some helper-methods useful when dealing with dates

Instance Method Summary collapse

Instance Method Details

#ensure_date(param) ⇒ Date

You can use ‘today’ or any format which Date.parse can handle.

Parameters:

  • param (nil|String|Time|Date)

Returns:

  • (Date)

    always returns a Date regardless of the type of input



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/modules/calendar/calendar.rb', line 49

def ensure_date(param)
  if param.is_a?(Date) || param.is_a?(::Time)
    param
  elsif param =~ /today/i || param.nil?
    Date.today 
  elsif param =~ /\d+[a-zA-Z]/i
    get_today_plus(param)
  else 
    Date.parse(param)
  end
end

#get_today_plus(param) ⇒ Object

Get Today + param

Parameters:

  • param (String)

    nDWM n=Number Day Weeks Months



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/modules/calendar/calendar.rb', line 63

def get_today_plus(param)
  offset = 0
  param.scan(/(\d+)([a-z])/i) do |count,unit|
    offset = case unit[0].downcase
    when 'd'
      count.to_i.days
    when 'w'
      count.to_i.weeks
    when 'm'
      count.to_i.months
    else
      raise "Unknown unit #{unit}. Valid units are d,w,m or days,weeks,months"
    end
  end
  Date.parse( (Time.now + offset).to_s )
end