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.



215
216
217
218
219
# File 'lib/daru/date_time/offsets.rb', line 215

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



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

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



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

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



245
246
247
# File 'lib/daru/date_time/offsets.rb', line 245

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

#on_offset?(date_time) ⇒ Boolean

Returns:

  • (Boolean)


241
242
243
# File 'lib/daru/date_time/offsets.rb', line 241

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