Sha256: 25f85e1571963b0a3c12e38232273b15aab6d4b1be1e7236aaf8de47e4b944a6

Contents?: true

Size: 1.97 KB

Versions: 9

Compression:

Stored size: 1.97 KB

Contents

module Chronic

  class Scalar < Tag #:nodoc:
    def self.scan(tokens)
      # for each token
      tokens.each_index do |i|
        if t = self.scan_for_scalars(tokens[i], tokens[i + 1]) then tokens[i].tag(t) end
        if t = self.scan_for_days(tokens[i], tokens[i + 1]) then tokens[i].tag(t) end
        if t = self.scan_for_months(tokens[i], tokens[i + 1]) then tokens[i].tag(t) end
        if t = self.scan_for_years(tokens[i], tokens[i + 1]) then tokens[i].tag(t) end
      end
      tokens
    end
  
    def self.scan_for_scalars(token, post_token)
      if token.word =~ /^\d*$/
        unless post_token && %w{am pm morning afternoon evening night}.include?(post_token)
          return Scalar.new(token.word.to_i)
        end
      end
      return nil
    end
    
    def self.scan_for_days(token, post_token)
      if token.word =~ /^\d\d?$/
        unless token.word.to_i > 31 || (post_token && %w{am pm morning afternoon evening night}.include?(post_token))
          return ScalarDay.new(token.word.to_i)
        end
      end
      return nil
    end
    
    def self.scan_for_months(token, post_token)
      if token.word =~ /^\d\d?$/
        unless token.word.to_i > 12 || (post_token && %w{am pm morning afternoon evening night}.include?(post_token))
          return ScalarMonth.new(token.word.to_i)
        end
      end
      return nil
    end
    
    def self.scan_for_years(token, post_token)
      if token.word =~ /^([1-9]\d)?\d\d?$/
        unless post_token && %w{am pm morning afternoon evening night}.include?(post_token)
          return ScalarYear.new(token.word.to_i)
        end
      end
      return nil
    end
    
    def to_s
      'scalar'
    end
  end
  
  class ScalarDay < Scalar #:nodoc:
    def to_s
      super << '-day-' << @type.to_s
    end
  end
  
  class ScalarMonth < Scalar #:nodoc:
    def to_s
      super << '-month-' << @type.to_s
    end
  end
  
  class ScalarYear < Scalar #:nodoc:
    def to_s
      super << '-year-' << @type.to_s
    end
  end

end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
moorage-chronic-0.3.0 lib/chronic/scalar.rb
chronic-0.1.3 lib/chronic/scalar.rb
chronic-0.2.1 lib/chronic/scalar.rb
chronic-0.1.4 lib/chronic/scalar.rb
chronic-0.2.2 lib/chronic/scalar.rb
chronic-0.2.0 lib/chronic/scalar.rb
chronic-0.1.6 lib/chronic/scalar.rb
chronic-0.2.3 lib/chronic/scalar.rb
chronic-0.1.5 lib/chronic/scalar.rb