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.



212
213
214
215
216
# File 'lib/daru/date_time/offsets.rb', line 212

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

Instance Method Details

#+(date_time) ⇒ Object



218
219
220
221
222
223
224
225
226
# File 'lib/daru/date_time/offsets.rb', line 218

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



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

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



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

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

#on_offset?(date_time) ⇒ Boolean

Returns:

  • (Boolean)


238
239
240
# File 'lib/daru/date_time/offsets.rb', line 238

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