Class: Useful::RubyExtensionsFromRails::Duration

Inherits:
Object
  • Object
show all
Defined in:
lib/useful/ruby_extensions_from_rails/duration.rb

Overview

Provides accurate date and time measurements using Date#advance and Time#advance, respectively. It mainly supports the methods on Numeric, such as in this example:

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:



11
12
13
# File 'lib/useful/ruby_extensions_from_rails/duration.rb', line 11

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:



93
94
95
# File 'lib/useful/ruby_extensions_from_rails/duration.rb', line 93

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

Instance Attribute Details

#partsObject

Returns the value of attribute parts.



9
10
11
# File 'lib/useful/ruby_extensions_from_rails/duration.rb', line 9

def parts
  @parts
end

#valueObject

Returns the value of attribute value.



9
10
11
# File 'lib/useful/ruby_extensions_from_rails/duration.rb', line 9

def value
  @value
end

Class Method Details

.===(other) ⇒ Object

:nodoc:



49
50
51
# File 'lib/useful/ruby_extensions_from_rails/duration.rb', line 49

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

Instance Method Details

#+(other) ⇒ Object

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



17
18
19
20
21
22
23
# File 'lib/useful/ruby_extensions_from_rails/duration.rb', line 17

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.



27
28
29
# File 'lib/useful/ruby_extensions_from_rails/duration.rb', line 27

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

#-@Object

:nodoc:



31
32
33
# File 'lib/useful/ruby_extensions_from_rails/duration.rb', line 31

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.



41
42
43
44
45
46
47
# File 'lib/useful/ruby_extensions_from_rails/duration.rb', line 41

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

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

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



62
63
64
# File 'lib/useful/ruby_extensions_from_rails/duration.rb', line 62

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

#inspectObject

:nodoc:



67
68
69
70
71
72
73
# File 'lib/useful/ruby_extensions_from_rails/duration.rb', line 67

def inspect #:nodoc:
  consolidated = parts.inject(::Hash.new(0)) { |h,part| h[part.first] += part.last; h }
  [: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.join(',')
end

#is_a?(klass) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


35
36
37
# File 'lib/useful/ruby_extensions_from_rails/duration.rb', line 35

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

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

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



55
56
57
# File 'lib/useful/ruby_extensions_from_rails/duration.rb', line 55

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