Repository URL to install this package:
|
Version:
2.0.1 ▾
|
# frozen_string_literal: true
require_relative 'duration/iso8601_parser'
module FacultyHelpers
# Helpers to handle Postgres Intervals in ruby.
# This will only work with dates, since that is all that is required right now.
# This is a much simplified version of how rails handles durations - I'm sure there are some edge cases that are
# not handled here. But this should be enough for us to use date durations without including the whole of
# activesupport.
class Duration
def self.parse(string)
new(Duration::ISO8601Parser.new(string).parse!)
end
def initialize(parts)
@parts = parts
end
def -@
Duration.new(@parts.transform_values(&:-@))
end
def add_to(date)
date >>= (@parts[:years] * 12) if @parts[:years]
date >>= @parts[:months] if @parts[:months]
date += @parts[:weeks] * 7 if @parts[:weeks]
date += @parts[:days] if @parts[:days]
date
end
def subtract_from(date)
(-self).add_to(date)
end
end
end