Class: TimeCrisis::DateRange

Inherits:
Range
  • Object
show all
Defined in:
lib/time_crisis/date_range.rb

Defined Under Namespace

Modules: Date

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ DateRange

Returns a new instance of DateRange.



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/time_crisis/date_range.rb', line 2

def initialize(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}

  # make compatibile with Range's normal arguments
  options[:begin] = args.shift unless args.empty?
  options[:end] = args.shift unless args.empty?
  options[:exclude_end] = args.shift unless args.empty?

  start = if options[:begin].nil?
    false
  elsif options[:begin].is_a?(TimeCrisis::Date)
    options[:begin]
  else
    options[:begin].to_tc_date
  end

  stop = if options[:end].nil?
    false
  elsif options[:end].is_a?(TimeCrisis::Date)
    options[:end]
  else
    options[:end].to_tc_date
  end

  unit = options[:unit] || 1
  scale = options[:scale] || 'years'
  scale = scale.pluralize if scale.respond_to?(:pluralize)

  if start && !stop
    stop = start.advance({scale.to_sym => unit, :days => -1})
  elsif !start && stop
    start = stop.advance({scale.to_sym => -unit, :days => 1})
  end

  super(start, stop, options[:exclude_end] || false)
end

Instance Method Details

#each_month(&block) ⇒ Object



76
77
78
# File 'lib/time_crisis/date_range.rb', line 76

def each_month(&block)
  each_slice_by_period(:months, 1, &block)
end

#each_slice_by_date(*args) {|self.class.new(start, self.end, self.exclude_end?, self.subopt)| ... } ⇒ Object

Yields:

  • (self.class.new(start, self.end, self.exclude_end?, self.subopt))


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/time_crisis/date_range.rb', line 43

def each_slice_by_date(*args, &block)
  dates = args.map! do |datelike|
    date = datelike.to_tc_date
    raise ArgumentError, "Date not within range: #{date}" unless self.include?(date)
    date
  end
  dates.sort!

  start = self.begin

  dates.each do |date|
    yield self.class.new(start, date, true, self.subopt)
    start = date
  end

  yield self.class.new(start, self.end, self.exclude_end?, self.subopt)
end

#each_slice_by_period(period, unit = 1) {|self.class.new(start, self.end, self.exclude_end?, self.subopt)| ... } ⇒ Object

Yields:

  • (self.class.new(start, self.end, self.exclude_end?, self.subopt))

Raises:

  • (ArgumentError)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/time_crisis/date_range.rb', line 61

def each_slice_by_period(period, unit=1, &block)
  start = self.begin
  nstart = start.advance(period.to_sym => unit)

  raise ArgumentError, "Period exceeds range" if nstart >= self.real_end

  while nstart < self.real_end
    yield self.class.new(start, nstart, true, self.subopt)
    start = nstart
    nstart = start.advance(period.to_sym => unit)
  end

  yield self.class.new(start, self.end, self.exclude_end?, self.subopt)
end

#each_year(&block) ⇒ Object



80
81
82
# File 'lib/time_crisis/date_range.rb', line 80

def each_year(&block)
  each_slice_by_period(:years, 1, &block)
end

#include?(datelike) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/time_crisis/date_range.rb', line 39

def include?(datelike)
  super(datelike.to_tc_date)
end