Class: When::V::Event::Enumerator::Logic::Residue

Inherits:
When::V::Event::Enumerator::Logic show all
Defined in:
lib/when_exe/icalendar.rb

Overview

剰余類を用いた繰り返し

曜日の指定などに用いる

Direct Known Subclasses

Weekday

Instance Attribute Summary

Attributes inherited from When::V::Event::Enumerator::Logic

#by_part, #cash, #freq_index, #list

Instance Method Summary collapse

Methods inherited from When::V::Event::Enumerator::Logic

#_bound

Constructor Details

#initialize(by_part, list, position, ref, start, dtstart) ⇒ Residue

Returns a new instance of Residue.



1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
# File 'lib/when_exe/icalendar.rb', line 1567

def initialize(by_part, list, position, ref, start, dtstart)
  @by_part = by_part
  divisor  = When::Coordinates::Pair._en_number(ref)
  @list    = list.split(/,/).map {|w|
    raise ArgumentError, "The #{by_part} rule format error" unless w =~ /\A(([-+]?\d+)\*)?(.+?)([-+]\d+)?\z/
    nth, spec, period = $~[2..4]
    nth    = (nth) ? nth.to_i : position
    period = When::TM::PeriodDuration.new((period||0).to_i, PostFreqIndex[@by_part])
    args   = [spec.to_i, divisor, (nth) ? ((nth>0) ? nth-1 : nth) : 0]
    case by_part
    when 'BYYEAR'
      start ||=  4
      start   = start % divisor
      args << {'year'=>start}
    when 'BYDAY'
      start ||= 11
      start   = start % divisor
      args << {'day'=>start} unless (start == 0)
    end
    residue = When::Coordinates::Residue.new(*args)
    [nth, residue, period]
  }
end

Instance Method Details

#_candidates(lower_bound, higher_bound) ⇒ Object



1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
# File 'lib/when_exe/icalendar.rb', line 1544

def _candidates(lower_bound, higher_bound)
  candidate = []
  @list.each do |ord|
    nth, residue, period = ord
    shift = (period[PostFreqIndex[@by_part]] != 0)
    raise ArgumentError, "n*m+/-s format not permitted" if (nth || shift) && @freq_index >= When::DAY
    if (nth)
      date  = ((nth >= 0) ? lower_bound  & residue :
                            higher_bound & residue)
      date += period if shift
      candidate << date
    else
      (lower_bound ^ residue).each do |date|
        date += period if shift
        break unless date < higher_bound
        candidate << date
      end
    end
  end
  return candidate
end