Sha256: 2cff1a844da706492043292317e9751036c273baa8ca145ff5012de2b81631e8

Contents?: true

Size: 1.06 KB

Versions: 5

Compression:

Stored size: 1.06 KB

Contents

require_relative '../conceptql'

module ConceptQL
  # Used to translate a string of terse date adjustments into a set of adjustments that are compatible with most RDBMSs
  class DateAdjuster
    attr :str
    def initialize(str)
      @str = str
    end

    # Returns an array of strings that represent date modifiers
    def adjustments
      @adjustments ||= parse(str)
    end

    private
    def lookup
      {
        'y' => 'year',
        'm' => 'month',
        'd' => 'day'
      }
    end

    def parse(str)
      ConceptQL.logger.debug(str)
      return [] if str.nil?
      return ["#{str} days"] if str.match(/^[-+]?\d+$/)
      str.downcase.scan(/([-+]?\d*[dmy])/).map do |adjustment|
        adjustment = adjustment.first
        quantity = 1
        if adjustment.match(/\d/)
          quantity = adjustment.to_i
        else
          if adjustment.chars.first == '-'
            quantity = -1
          end
        end
        unit = lookup[adjustment.chars.last]
        unit += 's' if quantity.abs > 1
        [quantity, unit].join(' ')
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
conceptql-0.0.6 lib/conceptql/date_adjuster.rb
conceptql-0.0.5 lib/conceptql/date_adjuster.rb
conceptql-0.0.4 lib/conceptql/date_adjuster.rb
conceptql-0.0.3 lib/conceptql/date_adjuster.rb
conceptql-0.0.1 lib/conceptql/date_adjuster.rb