Class: Daru::Offsets::Week

Inherits:
DateOffset show all
Defined in:
lib/daru/date_time/offsets.rb

Instance Method Summary collapse

Methods inherited from DateOffset

#-@

Constructor Details

#initialize(*args) ⇒ Week

Returns a new instance of Week.



223
224
225
226
227
# File 'lib/daru/date_time/offsets.rb', line 223

def initialize *args
  @n = args[0].is_a?(Hash) ? 1 : args[0]
  opts = args[-1]
  @weekday = opts[:weekday] || 0
end

Instance Method Details

#+(date_time) ⇒ Object



229
230
231
232
233
234
235
236
237
# File 'lib/daru/date_time/offsets.rb', line 229

def + date_time
  wday = date_time.wday
  distance = (@weekday - wday).abs
  if @weekday > wday
    date_time + distance + 7*(@n-1)
  else
    date_time + (7-distance) + 7*(@n -1)
  end
end

#-(date_time) ⇒ Object



239
240
241
242
243
244
245
246
247
# File 'lib/daru/date_time/offsets.rb', line 239

def - date_time
  wday = date_time.wday
  distance = (@weekday - wday).abs
  if @weekday >= wday
    date_time - ((7 - distance) + 7*(@n -1))
  else
    date_time - (distance + 7*(@n-1))
  end
end

#freq_stringObject



253
254
255
# File 'lib/daru/date_time/offsets.rb', line 253

def freq_string
  (@n == 1 ? '' : @n.to_s) + 'W' + '-' + Daru::DAYS_OF_WEEK.key(@weekday)
end

#on_offset?(date_time) ⇒ Boolean

Returns:

  • (Boolean)


249
250
251
# File 'lib/daru/date_time/offsets.rb', line 249

def on_offset? date_time
  date_time.wday == @weekday
end