Class: WsdlMapper::CoreExt::TimeDuration
- Inherits:
-
Object
- Object
- WsdlMapper::CoreExt::TimeDuration
- Defined in:
- lib/wsdl_mapper/core_ext/time_duration.rb
Overview
A very simple representation of time durations. The implementation is very naive. Each component (years, months, etc...) is stored separately and equality is only given, if all components are the same:
TimeDuration.new(years: 1) == TimeDuration.new(months: 12)
#=> false
Furthermore, comparison (<=>) does not work if you use unnormalized durations (e.g. 15 months instead of 1 year, 3 months):
TimeDuration.new(years: 1, months: 2) < TimeDuration.new(months: 15)
#=> false
Instance Attribute Summary collapse
-
#days ⇒ Fixnum
This durations days.
-
#hours ⇒ Fixnum
This durations hours.
-
#minutes ⇒ Fixnum
This durations minutes.
-
#months ⇒ Fixnum
This durations months.
-
#negative ⇒ true, false
Is this duration negative?.
-
#seconds ⇒ Fixnum
This durations seconds.
-
#years ⇒ Fixnum
This durations years.
Instance Method Summary collapse
- #<(other) ⇒ Object
- #<=(other) ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #>(other) ⇒ Object
- #>=(other) ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(negative: false, years: 0, months: 0, days: 0, hours: 0, minutes: 0, seconds: 0) ⇒ TimeDuration
constructor
A new instance of TimeDuration.
-
#negative? ⇒ true, false
Check if this is a negative duration.
Constructor Details
#initialize(negative: false, years: 0, months: 0, days: 0, hours: 0, minutes: 0, seconds: 0) ⇒ TimeDuration
Returns a new instance of TimeDuration.
39 40 41 42 43 44 45 46 47 |
# File 'lib/wsdl_mapper/core_ext/time_duration.rb', line 39 def initialize(negative: false, years: 0, months: 0, days: 0, hours: 0, minutes: 0, seconds: 0) @seconds = seconds @minutes = minutes @hours = hours @days = days @months = months @years = years @negative = negative end |
Instance Attribute Details
#days ⇒ Fixnum
Returns This durations days.
30 |
# File 'lib/wsdl_mapper/core_ext/time_duration.rb', line 30 attr_accessor :years, :months, :days, :hours, :minutes, :seconds, :negative |
#hours ⇒ Fixnum
Returns This durations hours.
30 |
# File 'lib/wsdl_mapper/core_ext/time_duration.rb', line 30 attr_accessor :years, :months, :days, :hours, :minutes, :seconds, :negative |
#minutes ⇒ Fixnum
Returns This durations minutes.
30 |
# File 'lib/wsdl_mapper/core_ext/time_duration.rb', line 30 attr_accessor :years, :months, :days, :hours, :minutes, :seconds, :negative |
#months ⇒ Fixnum
Returns This durations months.
30 |
# File 'lib/wsdl_mapper/core_ext/time_duration.rb', line 30 attr_accessor :years, :months, :days, :hours, :minutes, :seconds, :negative |
#negative ⇒ true, false
Returns Is this duration negative?.
30 |
# File 'lib/wsdl_mapper/core_ext/time_duration.rb', line 30 attr_accessor :years, :months, :days, :hours, :minutes, :seconds, :negative |
#seconds ⇒ Fixnum
Returns This durations seconds.
30 |
# File 'lib/wsdl_mapper/core_ext/time_duration.rb', line 30 attr_accessor :years, :months, :days, :hours, :minutes, :seconds, :negative |
#years ⇒ Fixnum
Returns This durations years.
30 31 32 |
# File 'lib/wsdl_mapper/core_ext/time_duration.rb', line 30 def years @years end |
Instance Method Details
#<(other) ⇒ Object
102 103 104 |
# File 'lib/wsdl_mapper/core_ext/time_duration.rb', line 102 def <(other) (self <=> other) < 0 end |
#<=(other) ⇒ Object
107 108 109 |
# File 'lib/wsdl_mapper/core_ext/time_duration.rb', line 107 def <=(other) (self <=> other) <= 0 end |
#<=>(other) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/wsdl_mapper/core_ext/time_duration.rb', line 79 def <=>(other) return -1 if negative? and !other.negative? return 1 if !negative? and other.negative? fields = [:years, :months, :days, :hours, :minutes, :seconds] fields.each do |field| c = send(field) <=> other.send(field) return c if c != 0 end return 0 end |
#==(other) ⇒ Object
74 75 76 |
# File 'lib/wsdl_mapper/core_ext/time_duration.rb', line 74 def ==(other) eql? other end |
#>(other) ⇒ Object
92 93 94 |
# File 'lib/wsdl_mapper/core_ext/time_duration.rb', line 92 def >(other) (self <=> other) > 0 end |
#>=(other) ⇒ Object
97 98 99 |
# File 'lib/wsdl_mapper/core_ext/time_duration.rb', line 97 def >=(other) (self <=> other) >= 0 end |
#eql?(other) ⇒ Boolean
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/wsdl_mapper/core_ext/time_duration.rb', line 61 def eql?(other) return false unless other.is_a?(TimeDuration) negative? == other.negative? && years == other.years && months == other.months && days == other.days && hours == other.hours && minutes == other.minutes && seconds == other.seconds end |
#hash ⇒ Object
56 57 58 |
# File 'lib/wsdl_mapper/core_ext/time_duration.rb', line 56 def hash [negative?, years, months, days, hours, minutes, seconds].hash end |
#negative? ⇒ true, false
Check if this is a negative duration
51 52 53 |
# File 'lib/wsdl_mapper/core_ext/time_duration.rb', line 51 def negative? !!@negative end |