Class: MotionSupport::Duration

Inherits:
BasicObject
Defined in:
motion/duration.rb

Overview

Provides accurate date and time measurements using Date#advance and Time#advance, respectively. It mainly supports the methods on Numeric.

1.month.ago       # equivalent to Time.now.advance(months: -1)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, parts) ⇒ Duration

:nodoc:



9
10
11
# File 'motion/duration.rb', line 9

def initialize(value, parts) #:nodoc:
  @value, @parts = value, parts
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)

:nodoc:



100
101
102
# File 'motion/duration.rb', line 100

def method_missing(method, *args, &block) #:nodoc:
  value.send(method, *args, &block)
end

Instance Attribute Details

#partsObject

Returns the value of attribute parts.



7
8
9
# File 'motion/duration.rb', line 7

def parts
  @parts
end

#valueObject

Returns the value of attribute value.



7
8
9
# File 'motion/duration.rb', line 7

def value
  @value
end

Class Method Details

.===(other) ⇒ Object

:nodoc:



48
49
50
51
52
# File 'motion/duration.rb', line 48

def self.===(other) #:nodoc:
  other.is_a?(Duration)
rescue ::NoMethodError
  false
end

Instance Method Details

#+(other) ⇒ Object

Adds another Duration or a Numeric to this Duration. Numeric values are treated as seconds.



15
16
17
18
19
20
21
# File 'motion/duration.rb', line 15

def +(other)
  if Duration === other
    Duration.new(value + other.value, @parts + other.parts)
  else
    Duration.new(value + other, @parts + [[:seconds, other]])
  end
end

#-(other) ⇒ Object

Subtracts another Duration or a Numeric from this Duration. Numeric values are treated as seconds.



25
26
27
# File 'motion/duration.rb', line 25

def -(other)
  self + (-other)
end

#-@Object

:nodoc:



29
30
31
# File 'motion/duration.rb', line 29

def -@ #:nodoc:
  Duration.new(-value, parts.map { |type,number| [type, -number] })
end

#==(other) ⇒ Object

Returns true if other is also a Duration instance with the same value, or if other == value.



40
41
42
43
44
45
46
# File 'motion/duration.rb', line 40

def ==(other)
  if Duration === other
    other.value == value
  else
    other == value
  end
end

#ago(time = ::Time.now) ⇒ Object Also known as: until

Calculates a new Time or Date that is as far in the past as this Duration represents.



63
64
65
# File 'motion/duration.rb', line 63

def ago(time = ::Time.now)
  sum(-1, time)
end

#as_json(options = nil) ⇒ Object

:nodoc:



78
79
80
# File 'motion/duration.rb', line 78

def as_json(options = nil) #:nodoc:
  to_i
end

#inspectObject

:nodoc:



68
69
70
71
72
73
74
75
76
# File 'motion/duration.rb', line 68

def inspect #:nodoc:
  consolidated = parts.inject(::Hash.new(0)) { |h,(l,r)| h[l] += r; h }
  parts = [:years, :months, :days, :minutes, :seconds].map do |length|
    n = consolidated[length]
    "#{n} #{n == 1 ? length.to_s.singularize : length.to_s}" if n.nonzero?
  end.compact
  parts = ["0 seconds"] if parts.empty?
  parts.to_sentence
end

#is_a?(klass) ⇒ Boolean Also known as: kind_of?

:nodoc:

Returns:

  • (Boolean)


33
34
35
# File 'motion/duration.rb', line 33

def is_a?(klass) #:nodoc:
  Duration == klass || value.is_a?(klass)
end

#since(time = ::Time.now) ⇒ Object Also known as: from_now

Calculates a new Time or Date that is as far in the future as this Duration represents.



56
57
58
# File 'motion/duration.rb', line 56

def since(time = ::Time.now)
  sum(1, time)
end