Class: DayRange

Inherits:
Range
  • Object
show all
Defined in:
lib/day_range.rb,
lib/day_range/version.rb

Overview

A Range-like object that represents a range of days.

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"1.0.0"

Instance Method Summary collapse

Instance Method Details

#daysObject

Returns the number of days in the timeframe.



10
11
12
# File 'lib/day_range.rb', line 10

def days
  last - first + 1
end

#every(step) ⇒ Object

Returns an enumerator that steps over the days in the range with specific intervals.

step can be specified similarily to DateTime#advance argument

Usage examples

day_range.every(days: 42)
day_range.every(weeks: 1)
day_range.every(months: 1)
day_range.every(days: 1, months: 2, years: 3)


26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/day_range.rb', line 26

def every(step)
  c_date = first
  finish_date = last
  comparison_operator = exclude_end? ? :< : :<=

  result = []
  while c_date.send(comparison_operator, finish_date)
    result << c_date
    yield c_date if block_given?
    c_date = advance(c_date, step)
  end
  result
end

#nextObject

Returns a DayRange with the same length as this one, starting on the day after this one ends.



42
43
44
45
46
47
# File 'lib/day_range.rb', line 42

def next
  self.class.new(
    last.next_day,
    last + days
  )
end

#previousObject

Returns a DayRange with the same length as this one, ending on the day before this one starts.



51
52
53
54
55
56
# File 'lib/day_range.rb', line 51

def previous
  self.class.new(
    first - days,
    first.prev_day
  )
end