Class: Diter

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
bin/diter

Overview

Diter enumerate dates with some filters.

require 'diter'

from = Date.new(2016, 1, 1)
to = Date.new(2016, 1, 31)

Diter[from, to, wday: 3].each do |date|
  puts date.strftime('%Y/%m/%d')
end
#=>
#  2016/01/06
#  2016/01/13
#  2016/01/20
#  2016/01/27

Instance Method Summary collapse

Constructor Details

#initialize(from, to, options = {}) ⇒ Diter

Return a new instance of Diter.

If from is greater than to, it enumerate dates in reverse order.

Parameters:

  • from (Date)

    the start of dates

  • to (Date)

    the end of dates

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :wday (Integer) — default: nil

    filter by the day of a week.

  • :mday (Integer) — default: nil

    filter by the day of a month.



38
39
40
41
# File 'bin/diter', line 38

def initialize(from, to, options = {})
  @range = from < to ? from..to : (to..from).reverse_each
  @options = options
end

Instance Method Details

#each {|date| ... } ⇒ Diter

Calls the given block once for each date.

Return self if no block is given.

Yield Parameters:

  • date (Date)

Returns:



49
50
51
52
53
54
55
56
57
58
# File 'bin/diter', line 49

def each
  return self unless block_given?

  @range.each do |date|
    next unless filter_wday(date)
    next unless filter_mday(date)

    yield date
  end
end