Class: Dateseq::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/dateseq.rb

Overview

Date Sequence generator

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Generator

Returns a new instance of Generator.



8
9
10
11
12
13
# File 'lib/dateseq.rb', line 8

def initialize(options)
  @format = options[:format] || '%Y%m%d'
  @sep = options[:sep] || "\n"
  @increments = options[:increments] || 1
  @mode = options[:mode] || :daily
end

Instance Method Details

#sequence(from_date, to_date) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/dateseq.rb', line 15

def sequence(from_date, to_date)
  from = Date.parse(from_date)
  to = Date.parse(to_date)
  case @mode
  when :daily
    sequence_daily(from, to)
  when :monthly
    sequence_monthly(from, to)
  end
end

#sequence_daily(from, to) ⇒ Object



26
27
28
29
30
# File 'lib/dateseq.rb', line 26

def sequence_daily(from, to)
  from.step(to, @increments).map do |date|
    date.strftime(@format)
  end
end

#sequence_monthly(from, to) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/dateseq.rb', line 32

def sequence_monthly(from, to)
  i = 0
  res = []
  while (date = from >> i) <= to
    res << date.strftime(@format)
    i += @increments
  end
  res
end

#sequence_str(from_date, to_date) ⇒ Object



42
43
44
# File 'lib/dateseq.rb', line 42

def sequence_str(from_date, to_date)
  sequence(from_date, to_date).join(@sep)
end