Class: TimeCrisis::Duration
- Inherits:
- BasicObject
- Defined in:
- lib/time_crisis/support/duration.rb
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
- #-(other) ⇒ Object
-
#-@ ⇒ Object
:nodoc:.
- #==(other) ⇒ Object
- #ago(time = ::Time.current) ⇒ Object (also: #until)
-
#initialize(value, parts) ⇒ Duration
constructor
:nodoc:.
-
#inspect ⇒ Object
:nodoc:.
-
#is_a?(klass) ⇒ Boolean
:nodoc:.
- #since(time = ::Time.current) ⇒ Object (also: #from_now)
Methods inherited from BasicObject
Constructor Details
#initialize(value, parts) ⇒ Duration
:nodoc:
7 8 9 |
# File 'lib/time_crisis/support/duration.rb', line 7 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:
83 84 85 |
# File 'lib/time_crisis/support/duration.rb', line 83 def method_missing(method, *args, &block) #:nodoc: value.send(method, *args) end |
Instance Attribute Details
#parts ⇒ Object
Returns the value of attribute parts.
5 6 7 |
# File 'lib/time_crisis/support/duration.rb', line 5 def parts @parts end |
#value ⇒ Object
Returns the value of attribute value.
5 6 7 |
# File 'lib/time_crisis/support/duration.rb', line 5 def value @value end |
Class Method Details
Instance Method Details
#+(other) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/time_crisis/support/duration.rb', line 11 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
19 20 21 |
# File 'lib/time_crisis/support/duration.rb', line 19 def -(other) self + (-other) end |
#-@ ⇒ Object
:nodoc:
23 24 25 |
# File 'lib/time_crisis/support/duration.rb', line 23 def -@ #:nodoc: Duration.new(-value, parts.map { |type,number| [type, -number] }) end |
#==(other) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/time_crisis/support/duration.rb', line 31 def ==(other) if Duration === other other.value == value else other == value end end |
#ago(time = ::Time.current) ⇒ Object Also known as: until
48 49 50 |
# File 'lib/time_crisis/support/duration.rb', line 48 def ago(time = ::Time.current) sum(-1, time) end |
#inspect ⇒ Object
:nodoc:
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/time_crisis/support/duration.rb', line 53 def inspect #:nodoc: consolidated = parts.inject(::Hash.new(0)) { |h,part| h[part.first] += part.last; 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? "#{n} #{length}" if n > 0 end.compact parts = ["0 seconds"] if parts.empty? # parts.to_sentence(:locale => :en) parts.join(', ') end |
#is_a?(klass) ⇒ Boolean
:nodoc:
27 28 29 |
# File 'lib/time_crisis/support/duration.rb', line 27 def is_a?(klass) #:nodoc: klass == Duration || super end |