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) ⇒ Residue

Returns a new instance of Residue.



1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
# File 'lib/when_exe/icalendar.rb', line 1460

def initialize(by_part, list, position, ref, start)
  @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



1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
# File 'lib/when_exe/icalendar.rb', line 1437

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