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)
-
- (Fixnum) days
This durations days.
-
- (Fixnum) hours
This durations hours.
-
- (Fixnum) minutes
This durations minutes.
-
- (Fixnum) months
This durations months.
-
- (true, false) negative
Is this duration negative?.
-
- (Fixnum) seconds
This durations seconds.
-
- (Fixnum) years
This durations years.
Instance Method Summary (collapse)
- - (Object) <(other)
- - (Object) <=(other)
- - (Object) <=>(other)
- - (Object) ==(other)
- - (Object) >(other)
- - (Object) >=(other)
- - (Boolean) eql?(other)
- - (Object) hash
-
- (TimeDuration) initialize(negative: false, years: 0, months: 0, days: 0, hours: 0, minutes: 0, seconds: 0)
constructor
A new instance of TimeDuration.
-
- (true, false) negative?
Check if this is a negative duration.
Constructor Details
- (TimeDuration) initialize(negative: false, years: 0, months: 0, days: 0, hours: 0, minutes: 0, seconds: 0)
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
- (Fixnum) days
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 |
- (Fixnum) hours
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 |
- (Fixnum) minutes
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 |
- (Fixnum) months
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 |
- (true, false) negative
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 |
- (Fixnum) seconds
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 |
- (Fixnum) years
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
- (Object) <(other)
102 103 104 |
# File 'lib/wsdl_mapper/core_ext/time_duration.rb', line 102 def <(other) (self <=> other) < 0 end |
- (Object) <=(other)
107 108 109 |
# File 'lib/wsdl_mapper/core_ext/time_duration.rb', line 107 def <=(other) (self <=> other) <= 0 end |
- (Object) <=>(other)
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 |
- (Object) ==(other)
74 75 76 |
# File 'lib/wsdl_mapper/core_ext/time_duration.rb', line 74 def ==(other) eql? other end |
- (Object) >(other)
92 93 94 |
# File 'lib/wsdl_mapper/core_ext/time_duration.rb', line 92 def >(other) (self <=> other) > 0 end |
- (Object) >=(other)
97 98 99 |
# File 'lib/wsdl_mapper/core_ext/time_duration.rb', line 97 def >=(other) (self <=> other) >= 0 end |
- (Boolean) eql?(other)
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 |
- (Object) hash
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 |
- (true, false) negative?
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 |