Class: InactiveSupport::Duration
- Inherits:
- BasicObject
- Defined in:
- lib/inactive_support/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
-
#parts ⇒ Object
Returns the value of attribute parts.
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
-
.===(other) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#+(other) ⇒ Object
Adds another Duration or a Numeric to this Duration.
-
#-(other) ⇒ Object
Subtracts another Duration or a Numeric from this Duration.
-
#-@ ⇒ Object
:nodoc:.
-
#==(other) ⇒ Object
Returns true if
other
is also a Duration instance with the samevalue
, or ifother == value
. -
#ago(time = ::Time.now) ⇒ Object
(also: #until)
Calculates a new Time or Date that is as far in the past as this Duration represents.
-
#initialize(value, parts) ⇒ Duration
constructor
:nodoc:.
-
#inspect ⇒ Object
:nodoc:.
-
#is_a?(klass) ⇒ Boolean
:nodoc:.
-
#since(time = ::Time.now) ⇒ Object
(also: #from_now)
Calculates a new Time or Date that is as far in the future as this Duration represents.
Constructor Details
#initialize(value, parts) ⇒ Duration
:nodoc:
10 11 12 |
# File 'lib/inactive_support/duration.rb', line 10 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:
92 93 94 |
# File 'lib/inactive_support/duration.rb', line 92 def method_missing(method, *args, &block) #:nodoc: value.send(method, *args) end |
Instance Attribute Details
#parts ⇒ Object
Returns the value of attribute parts.
8 9 10 |
# File 'lib/inactive_support/duration.rb', line 8 def parts @parts end |
#value ⇒ Object
Returns the value of attribute value.
8 9 10 |
# File 'lib/inactive_support/duration.rb', line 8 def value @value end |
Class Method Details
Instance Method Details
#+(other) ⇒ Object
Adds another Duration or a Numeric to this Duration. Numeric values are treated as seconds.
16 17 18 19 20 21 22 |
# File 'lib/inactive_support/duration.rb', line 16 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.
26 27 28 |
# File 'lib/inactive_support/duration.rb', line 26 def -(other) self + (-other) end |
#-@ ⇒ Object
:nodoc:
30 31 32 |
# File 'lib/inactive_support/duration.rb', line 30 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 'lib/inactive_support/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.
61 62 63 |
# File 'lib/inactive_support/duration.rb', line 61 def ago(time = ::Time.now) sum(-1, time) end |
#inspect ⇒ Object
:nodoc:
66 67 68 69 70 71 72 |
# File 'lib/inactive_support/duration.rb', line 66 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.to_sentence end |
#is_a?(klass) ⇒ Boolean
:nodoc:
34 35 36 |
# File 'lib/inactive_support/duration.rb', line 34 def is_a?(klass) #:nodoc: klass == Duration || super end |