Sha256: 1966c0af9ceb73513d6024c81861cb98b57bd4f0071f8bff5249db75fb78babf

Contents?: true

Size: 774 Bytes

Versions: 3

Compression:

Stored size: 774 Bytes

Contents

module Attentive
  class Duration < Struct.new(:years, :months, :days)

    def initialize(attributes)
      super(
        attributes.fetch(:years, 0),
        attributes.fetch(:months, 0),
        attributes.fetch(:days, 0))
    end

    def +(other)
      self.class.new(
        years: years + other.years,
        months: months + other.months,
        days: days + other.days)
    end

    def inspect
      phrases = []
      phrases.push "#{years} years" if years > 0
      phrases.push "#{months} months" if months > 0
      phrases.push "#{days} days" if days > 0
      "<#{phrases.join(" ")}>"
    end

    def after(date)
      (date >> (years * 12 + months)) + days
    end

    def before(date)
      (date >> -(years * 12 + months)) - days
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
attentive-0.3.4 lib/attentive/duration.rb
attentive-0.3.3 lib/attentive/duration.rb
attentive-0.3.2 lib/attentive/duration.rb