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.



1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
# File 'lib/when_exe/icalendar.rb', line 1415

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 =~ /^(([-+]?\d+)\*)?(.+?)([-+]\d+)?$/
    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



1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
# File 'lib/when_exe/icalendar.rb', line 1392

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