Class: Midnight::Repeater
- Inherits:
-
Chronic::Tag
- Object
- Chronic::Tag
- Midnight::Repeater
- Defined in:
- lib/midnight/repeater.rb
Overview
:nodoc:
Class Method Summary collapse
- .scan(tokens) ⇒ Object
- .scan_for_24h_times(token) ⇒ Object
- .scan_for_day_names(token) ⇒ Object
- .scan_for_month_names(token) ⇒ Object
- .scan_for_numbers(token) ⇒ Object
- .scan_for_special_text(token) ⇒ Object
- .scan_for_units(token) ⇒ Object
-
.split_time_tokens(tokens) ⇒ Object
Split time tokens into constituent hour and minute tokens.
Class Method Details
.scan(tokens) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/midnight/repeater.rb', line 3 def self.scan(tokens) # for each token tokens.each do |token| token = self.scan_for_24h_times(token) token = self.scan_for_numbers(token) token = self.scan_for_month_names(token) token = self.scan_for_day_names(token) token = self.scan_for_special_text(token) token = self.scan_for_units(token) end tokens = self.split_time_tokens(tokens) tokens end |
.scan_for_24h_times(token) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/midnight/repeater.rb', line 24 def self.scan_for_24h_times(token) if token.word =~ /(2[0-3]|[01]?[0-9]):([0-5]?[0-9])/ token.update(:time, nil, nil) end token end |
.scan_for_day_names(token) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/midnight/repeater.rb', line 68 def self.scan_for_day_names(token) scanner = { /^su[nm](day)?s?$/ => :sunday, /^m[ou]n(day)?s?$/ => :monday, /^t(ue|eu|oo|u|)s(day)?s?$/ => :tuesday, /^tue$/ => :tuesday, /^we(dnes|nds|nns)day?s?$/ => :wednesday, /^wed$/ => :wednesday, /^th(urs|ers)day?s?$/ => :thursday, /^thu$/ => :thursday, /^fr[iy](day)?s?$/ => :friday, /^sat(t?[ue]rday)?s?$/ => :saturday, /^weekdays?$/ => :weekday, } day_sequence = { :sunday => 0, :monday => 1, :tuesday => 2, :wednesday => 3, :thursday => 4, :friday => 5, :saturday => 6, :weekday => '1-5', } scanner.each do |scanner_item, day| position_in_sequence = day_sequence[day] token.update(:weekday, scanner[scanner_item], 7, position_in_sequence) if scanner_item =~ token.word end token end |
.scan_for_month_names(token) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/midnight/repeater.rb', line 31 def self.scan_for_month_names(token) scanner = { /^jan\.?(uary)?$/ => :january, /^feb\.?(ruary)?$/ => :february, /^mar\.?(ch)?$/ => :march, /^apr\.?(il)?$/ => :april, /^may$/ => :may, /^jun\.?e?$/ => :june, /^jul\.?y?$/ => :july, /^aug\.?(ust)?$/ => :august, /^sep\.?(t\.?|tember)?$/ => :september, /^oct\.?(ober)?$/ => :october, /^nov\.?(ember)?$/ => :november, /^dec\.?(ember)?$/ => :december} month_sequence = { :january => 1, :february => 2, :march => 3, :april => 4, :may => 5, :june => 6, :july => 7, :august => 8, :september => 9, :october => 10, :november => 11, :december => 12 } scanner.each do |scanner_item, month| position_in_sequence = month_sequence[month] token.update(:month_name, scanner[scanner_item], 30, position_in_sequence) if scanner_item =~ token.word end token end |
.scan_for_numbers(token) ⇒ Object
18 19 20 21 22 |
# File 'lib/midnight/repeater.rb', line 18 def self.scan_for_numbers(token) num = Float(token.word) rescue nil token.update(:number, nil, num.to_i) if num token end |
.scan_for_special_text(token) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/midnight/repeater.rb', line 101 def self.scan_for_special_text(token) scanner = { /^other$/ => :other, /^begin(ing|ning)?$/ => :beginning, /^start$/ => :beginning, /^end$/ => :end, /^mid(d)?le$/ => :middle} scanner.keys.each do |scanner_item| token.update(:special, scanner[scanner_item], 7) if scanner_item =~ token.word end token end |
.scan_for_units(token) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/midnight/repeater.rb', line 114 def self.scan_for_units(token) scanner = { /^year(ly)?s?|annually$/ => {:type => :year, :interval => 365, :start => :today}, /^month(ly)?s?$/ => {:type => :month, :interval => 30, :start => :today}, /^fortnights?$/ => {:type => :fortnight, :interval => 365, :start => :today}, /^week(ly)?s?$/ => {:type => :week, :interval => 7, :start => :today}, /^weekends?$/ => {:type => :weekend, :interval => 7, :start => :saturday}, /^days?$/ => {:type => :day, :interval => 1, :start => :today}, /^daily?$/ => {:type => :day, :interval => 1, :start => :today}, /^minutes?$/ => {:type => :minute_word, :start => :today}, /^hour(ly)?s?$/ => {:type => :hour, :start => :today}, /^am|pm$/ => {:type => :meridiem} } scanner.keys.each do |scanner_item| if scanner_item =~ token.word token.update(scanner[scanner_item][:type], scanner[scanner_item][:start], scanner[scanner_item][:interval]) if scanner_item =~ token.word end end token end |
.split_time_tokens(tokens) ⇒ Object
Split time tokens into constituent hour and minute tokens
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/midnight/repeater.rb', line 136 def self.split_time_tokens(tokens) tokens_output = [] tokens.each do |token| if (token.type == :time) time_parts = token.word.split(':') hour_token = Midnight::Token.new(time_parts[0].to_i) minute_token = Midnight::Token.new(time_parts[1].to_i) hour_token.update(:hour) minute_token.update(:minute) tokens_output << hour_token tokens_output << minute_token else tokens_output << token end end tokens_output end |