Module: Runt::TExprUtils

Included in:
DIMonth, WIMonth
Defined in:
lib/runt/temporalexpression.rb

Overview

Utility methods common to some expressions

Instance Method Summary collapse

Instance Method Details

#days_left_in_month(date) ⇒ Object



247
248
249
# File 'lib/runt/temporalexpression.rb', line 247

def days_left_in_month(date)
  return max_day_of_month(date) - date.day
end

#max_day_of_month(date) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/runt/temporalexpression.rb', line 251

def max_day_of_month(date)
  # Contributed by Justin Cunningham who took it verbatim from the Rails 
  # ActiveSupport::CoreExtensions::Time::Calculations::ClassMethods module 
  # days_in_month method. 
  month = date.month
  year = date.year
  if month == 2
    !year.nil? && 
      (year % 4 == 0) && 
      ((year % 100 != 0) || 
       (year % 400 == 0)) ?  29 : 28
  elsif month <= 7
    month % 2 == 0 ? 30 : 31
  else
    month % 2 == 0 ? 31 : 30
  end
end

#week_from_end_matches?(index, date) ⇒ Boolean

Returns:

  • (Boolean)


281
282
283
284
# File 'lib/runt/temporalexpression.rb', line 281

def week_from_end_matches?(index,date)
  n = days_left_in_month(date) + 1
  week_in_month(n)==index.abs
end

#week_from_start_matches?(index, date) ⇒ Boolean

Returns:

  • (Boolean)


277
278
279
# File 'lib/runt/temporalexpression.rb', line 277

def week_from_start_matches?(index,date)
  week_in_month(date.day)==index
end

#week_in_month(day_in_month) ⇒ Object



243
244
245
# File 'lib/runt/temporalexpression.rb', line 243

def week_in_month(day_in_month)
  ((day_in_month - 1) / 7) + 1
end

#week_matches?(index, date) ⇒ Boolean

Returns:

  • (Boolean)


269
270
271
272
273
274
275
# File 'lib/runt/temporalexpression.rb', line 269

def week_matches?(index,date)
  if(index > 0)
    return week_from_start_matches?(index,date)
  else
    return week_from_end_matches?(index,date)
  end
end