Sha256: e87f5e39fcd772555b7483225375aaae5ae8f52b6cd241bcb89c17e6ddb18749

Contents?: true

Size: 1.24 KB

Versions: 6

Compression:

Stored size: 1.24 KB

Contents

module Dentaku
  class DateArithmetic
    def initialize(date)
      @base = date
    end

    def add(duration)
      case duration
      when Numeric
        @base + duration
      when Dentaku::AST::Duration::Value
        case duration.unit
        when :year
          Time.local(@base.year + duration.value, @base.month, @base.day).to_datetime
        when :month
          @base >> duration.value
        when :day
          @base + duration.value
        end
      else
        raise Dentaku::ArgumentError.for(:incompatible_type, value: duration, for: Numeric),
          "'#{duration || duration.class}' is not coercible for date arithmetic"
      end
    end

    def sub(duration)
      case duration
      when DateTime, Numeric
        @base - duration
      when Dentaku::AST::Duration::Value
        case duration.unit
        when :year
          Time.local(@base.year - duration.value, @base.month, @base.day).to_datetime
        when :month
          @base << duration.value
        when :day
          @base - duration.value
        end
      else
        raise Dentaku::ArgumentError.for(:incompatible_type, value: duration, for: Numeric),
          "'#{duration || duration.class}' is not coercible for date arithmetic"
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
dentaku_zevo-3.5.2 lib/dentaku/date_arithmetic.rb
dentaku-3.5.1 lib/dentaku/date_arithmetic.rb
dentaku-3.5.0 lib/dentaku/date_arithmetic.rb
dentaku-3.4.2 lib/dentaku/date_arithmetic.rb
dentaku-3.4.1 lib/dentaku/date_arithmetic.rb
dentaku-3.4.0 lib/dentaku/date_arithmetic.rb