Sha256: bfa7202999becd0e93acc51bfd40a8c65f53b3159eba3b0077fb6368fe94c942

Contents?: true

Size: 925 Bytes

Versions: 14

Compression:

Stored size: 925 Bytes

Contents

require 'date'
require 'working_hours/computation'

module WorkingHours
  class Duration

    attr_accessor :value, :kind

    SUPPORTED_KINDS = [:days, :hours, :minutes, :seconds]

    def initialize(value, kind)
      raise ArgumentError.new("Invalid working time unit: #{kind}") unless SUPPORTED_KINDS.include?(kind)
      @value = value
      @kind = kind
    end

    # Computation methods
    def until(time = ::Time.current)
      ::WorkingHours.send("add_#{@kind}", time, -@value)
    end
    alias :ago :until

    def since(time = ::Time.current)
      ::WorkingHours.send("add_#{@kind}", time, @value)
    end
    alias :from_now :since

    # Value object methods
    def -@
      Duration.new(-value, kind)
    end

    def ==(other)
      self.class == other.class and kind == other.kind and value == other.value
    end
    alias :eql? :==

    def hash
      [self.class, kind, value].hash
    end

  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
working_hours-1.5.0 lib/working_hours/duration.rb
working_hours-1.4.1 lib/working_hours/duration.rb
working_hours-1.4.0 lib/working_hours/duration.rb
working_hours-1.3.2 lib/working_hours/duration.rb
working_hours-1.3.1 lib/working_hours/duration.rb
working_hours-1.3.0 lib/working_hours/duration.rb
working_hours-1.2.0 lib/working_hours/duration.rb
working_hours-1.1.4 lib/working_hours/duration.rb
working_hours-1.1.3 lib/working_hours/duration.rb
working_hours-1.1.2 lib/working_hours/duration.rb
working_hours-1.1.1 lib/working_hours/duration.rb
working_hours-1.1.0 lib/working_hours/duration.rb
working_hours-1.0.4 lib/working_hours/duration.rb
working_hours-1.0.3 lib/working_hours/duration.rb