Class: WsdlMapper::CoreExt::TimeDuration

Inherits:
Object
  • Object
show all
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)

Instance Method Summary (collapse)

Constructor Details

- (TimeDuration) initialize(negative: false, years: 0, months: 0, days: 0, hours: 0, minutes: 0, seconds: 0)

Returns a new instance of TimeDuration

Parameters:

  • negative (bool)

    Set if negative or not

  • years (Fixnum)

    Sets the years

  • months (Fixnum)

    Sets the months

  • days (Fixnum)

    Sets the days

  • hours (Fixnum)

    Sets the hours

  • minutes (Fixnum)

    Sets the minutes

  • seconds (Fixnum)

    Sets the seconds



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

Returns:

  • (Fixnum)

    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

Returns:

  • (Fixnum)

    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

Returns:

  • (Fixnum)

    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

Returns:

  • (Fixnum)

    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?

Returns:

  • (true, false)

    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

Returns:

  • (Fixnum)

    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

Returns:

  • (Fixnum)

    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)

Returns:

  • (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

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

Returns:

  • (true, false)


51
52
53
# File 'lib/wsdl_mapper/core_ext/time_duration.rb', line 51

def negative?
  !!@negative
end