class Chronic::Repeater

Public Class Methods

scan(tokens, options) click to toggle source

Scan an Array of {Token}s and apply any necessary Repeater tags to each token

@param [Array<Token>] tokens Array of tokens to scan @param [Hash] options Options specified in {Chronic.parse} @return [Array] list of tokens

# File lib/chronic/repeater.rb, line 10
def self.scan(tokens, options)
  tokens.each do |token|
    if t = scan_for_season_names(token) then token.tag(t); next end
    if t = scan_for_month_names(token) then token.tag(t); next end
    if t = scan_for_day_names(token) then token.tag(t); next end
    if t = scan_for_day_portions(token) then token.tag(t); next end
    if t = scan_for_times(token) then token.tag(t); next end
    if t = scan_for_units(token) then token.tag(t); next end
  end
end
scan_for_day_names(token) click to toggle source

@param [Token] token @return [RepeaterDayName, nil]

# File lib/chronic/repeater.rb, line 55
def self.scan_for_day_names(token)
  scan_for token, RepeaterDayName,
  {
    %r^m[ou]n(day)?$/ => :monday,
    %r^t(ue|eu|oo|u|)s?(day)?$/ => :tuesday,
    %r^we(d|dnes|nds|nns)(day)?$/ => :wednesday,
    %r^th(u|ur|urs|ers)(day)?$/ => :thursday,
    %r^fr[iy](day)?$/ => :friday,
    %r^sat(t?[ue]rday)?$/ => :saturday,
    %r^su[nm](day)?$/ => :sunday
  }
end
scan_for_day_portions(token) click to toggle source

@param [Token] token @return [RepeaterDayPortion, nil]

# File lib/chronic/repeater.rb, line 70
def self.scan_for_day_portions(token)
  scan_for token, RepeaterDayPortion,
  {
    %r^ams?$/ => :am,
    %r^pms?$/ => :pm,
    %r^mornings?$/ => :morning,
    %r^afternoons?$/ => :afternoon,
    %r^evenings?$/ => :evening,
    %r^(night|nite)s?$/ => :night
  }
end
scan_for_month_names(token) click to toggle source

@param [Token] token @return [RepeaterMonthName, nil]

# File lib/chronic/repeater.rb, line 35
def self.scan_for_month_names(token)
  scan_for token, RepeaterMonthName,
  {
    %r^jan\.?(uary)?$/ => :january,
    %r^feb\.?(ruary)?$/ => :february,
    %r^mar\.?(ch)?$/ => :march,
    %r^apr\.?(il)?$/ => :april,
    %r^may$/ => :may,
    %r^jun\.?e?$/ => :june,
    %r^jul\.?y?$/ => :july,
    %r^aug\.?(ust)?$/ => :august,
    %r^sep\.?(t\.?|tember)?$/ => :september,
    %r^oct\.?(ober)?$/ => :october,
    %r^nov\.?(ember)?$/ => :november,
    %r^dec\.?(ember)?$/ => :december
  }
end
scan_for_season_names(token) click to toggle source

@param [Token] token @return [RepeaterSeasonName, nil]

# File lib/chronic/repeater.rb, line 23
def self.scan_for_season_names(token)
  scan_for token, RepeaterSeasonName,
  {
    %r^springs?$/ => :spring,
    %r^summers?$/ => :summer,
    %r^(autumn)|(fall)s?$/ => :autumn,
    %r^winters?$/ => :winter
  }
end
scan_for_times(token) click to toggle source

@param [Token] token @return [RepeaterTime, nil]

# File lib/chronic/repeater.rb, line 84
def self.scan_for_times(token)
  scan_for token, RepeaterTime, %r^\d{1,2}(:?\d{2})?([\.:]?\d{2})?$/
end
scan_for_units(token) click to toggle source

@param [Token] token @return [Repeater] A new instance of a subclass of Repeater

# File lib/chronic/repeater.rb, line 90
def self.scan_for_units(token)
  {
    %r^years?$/ => :year,
    %r^seasons?$/ => :season,
    %r^months?$/ => :month,
    %r^fortnights?$/ => :fortnight,
    %r^weeks?$/ => :week,
    %r^weekends?$/ => :weekend,
    %r^(week|business)days?$/ => :weekday,
    %r^days?$/ => :day,
    %r^hours?$/ => :hour,
    %r^minutes?$/ => :minute,
    %r^seconds?$/ => :second
  }.each do |item, symbol|
    if item =~ token.word
      klass_name = 'Repeater' + symbol.to_s.capitalize
      klass = Chronic.const_get(klass_name)
      return klass.new(symbol)
    end
  end
  return nil
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/chronic/repeater.rb, line 113
def <=>(other)
  width <=> other.width
end
next(pointer) click to toggle source

returns the next occurance of this repeatable.

# File lib/chronic/repeater.rb, line 123
def next(pointer)
  raise("Start point must be set before calling #next") unless @now
end
this(pointer) click to toggle source
# File lib/chronic/repeater.rb, line 127
def this(pointer)
  raise("Start point must be set before calling #this") unless @now
end
to_s() click to toggle source
# File lib/chronic/repeater.rb, line 131
def to_s
  'repeater'
end
width() click to toggle source

returns the width (in seconds or months) of this repeatable.

# File lib/chronic/repeater.rb, line 118
def width
  raise("Repeater#width must be overridden in subclasses")
end