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



273
274
275
# File 'lib/runt/temporalexpression.rb', line 273

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

#max_day_of_month(date) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/runt/temporalexpression.rb', line 277

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)


307
308
309
310
# File 'lib/runt/temporalexpression.rb', line 307

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)


303
304
305
# File 'lib/runt/temporalexpression.rb', line 303

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

#week_in_month(day_in_month) ⇒ Object



269
270
271
# File 'lib/runt/temporalexpression.rb', line 269

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

#week_matches?(index, date) ⇒ Boolean

Returns:

  • (Boolean)


295
296
297
298
299
300
301
# File 'lib/runt/temporalexpression.rb', line 295

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