Class: TimeCrisis::Duration

Inherits:
BasicObject
Defined in:
lib/time_crisis/support/duration.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BasicObject

#raise

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

#partsObject

Returns the value of attribute parts.



5
6
7
# File 'lib/time_crisis/support/duration.rb', line 5

def parts
  @parts
end

#valueObject

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

.===(other) ⇒ Object

:nodoc:



39
40
41
# File 'lib/time_crisis/support/duration.rb', line 39

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

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

#inspectObject

: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:

Returns:

  • (Boolean)


27
28
29
# File 'lib/time_crisis/support/duration.rb', line 27

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

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



43
44
45
# File 'lib/time_crisis/support/duration.rb', line 43

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