Sha256: 90c38a9c1154cc4806abbe149601a503657f734b86a7941d1971ec3af5b0ebbd

Contents?: true

Size: 1.46 KB

Versions: 3

Compression:

Stored size: 1.46 KB

Contents

module Datte
  class DateParser
    def initialize(body, options = {})
      @body = body
      @options = options
      @date = Dattetime.new
    end

    def parse
      ABSOLUTE_DATES.each do |matcher|
        if md = @body.match(matcher)
          @date.update_date(md)
          break
        end
      end

      ABSOLUTE_TIMES.each do |matcher|
        if md = @body.match(matcher)
          @date.update_time(md)
          p @date
          break
        end
      end

      NOUNS.each do |matcher_s, method|
        matcher = Regexp.new(matcher_s.to_s)
        if md = @body.match(matcher)
          eval(method)
          break
        end
      end

      AFTERS.each do |matcher|
        if md = @body.match(matcher)
          @date.after(md)
          break
        end
      end

      WEEKS.each do |matcher_s, week|
        matcher = Regexp.new(matcher_s.to_s)
        if @body.match(matcher)
          WDAYS.each do |wday_matcher_s, wday|
            wday_matcher = Regexp.new(wday_matcher_s.to_s)
            if @body.match(wday_matcher)
              now_wday = DateTime.now.wday
              day = 7 * (week || 1) + wday - now_wday
              @date.after({day: day})
            end
          end
        end
      end

      return @date.to_datetime
    end

    private

    def next_day(day)
      now = DateTime.now
      @date.update_date({
        year: now.year,
        month: now.month,
        day: now.day + day
      }, {force_update: true})
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
datte-0.6.2 lib/datte/date_parser.rb
datte-0.6.1 lib/datte/date_parser.rb
datte-0.6.0 lib/datte/date_parser.rb