Class: ISO8601Basic::Duration

Inherits:
Object
  • Object
show all
Defined in:
lib/iso8601_basic/duration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Duration

Returns a new instance of Duration.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/iso8601_basic/duration.rb', line 5

def initialize(value)
  match = /^(\+|-)? # Sign
   P(
      (
        (\d+(?:[,.]\d+)?Y)? # Years
        (\d+(?:[.,]\d+)?M)? # Months
        (\d+(?:[.,]\d+)?D)? # Days
        (T
          (\d+(?:[.,]\d+)?H)? # Hours
          (\d+(?:[.,]\d+)?M)? # Minutes
          (\d+(?:[.,]\d+)?S)? # Seconds
        )? # Time
      )
      |(\d+(?:[.,]\d+)?W) # Weeks
    ) # Duration
  $/x.match(value)

  raise ArgumentError, 'invalid duration' unless match

  @sign = (match[1] == '-' ? -1 : 1)
  { years: 4, months: 5, weeks: 11, days: 6, hours: 8, minutes: 9, seconds: 10 }.each do |key, index|
    instance_variable_set "@#{key}", match[index] ? match[index].chop.to_f * @sign : 0 
  end
end

Instance Attribute Details

#daysObject (readonly)

Returns the value of attribute days.



3
4
5
# File 'lib/iso8601_basic/duration.rb', line 3

def days
  @days
end

#hoursObject (readonly)

Returns the value of attribute hours.



3
4
5
# File 'lib/iso8601_basic/duration.rb', line 3

def hours
  @hours
end

#minutesObject (readonly)

Returns the value of attribute minutes.



3
4
5
# File 'lib/iso8601_basic/duration.rb', line 3

def minutes
  @minutes
end

#monthsObject (readonly)

Returns the value of attribute months.



3
4
5
# File 'lib/iso8601_basic/duration.rb', line 3

def months
  @months
end

#secondsObject (readonly)

Returns the value of attribute seconds.



3
4
5
# File 'lib/iso8601_basic/duration.rb', line 3

def seconds
  @seconds
end

#signObject (readonly)

Returns the value of attribute sign.



3
4
5
# File 'lib/iso8601_basic/duration.rb', line 3

def sign
  @sign
end

#weeksObject (readonly)

Returns the value of attribute weeks.



3
4
5
# File 'lib/iso8601_basic/duration.rb', line 3

def weeks
  @weeks
end

#yearsObject (readonly)

Returns the value of attribute years.



3
4
5
# File 'lib/iso8601_basic/duration.rb', line 3

def years
  @years
end

Instance Method Details

#==(other) ⇒ Object



62
63
64
# File 'lib/iso8601_basic/duration.rb', line 62

def ==(other)
  eql? other
end

#as_jsonObject



78
79
80
# File 'lib/iso8601_basic/duration.rb', line 78

def as_json
  iso8601
end

#blank?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/iso8601_basic/duration.rb', line 34

def blank?
  empty?
end

#empty?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/iso8601_basic/duration.rb', line 38

def empty?
  to_h.all? { |key, value| value.zero? }
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/iso8601_basic/duration.rb', line 58

def eql?(other)
  hash == other.hash
end

#formatObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/iso8601_basic/duration.rb', line 86

def format
  singular = {
    years:   'year',
    months:  'month',
    weeks:   'week',
    days:    'day',
    hours:   'hour',
    minutes: 'minute',
    seconds: 'second' }

  to_h.collect do |key, value|
    value = value.divmod(1)[1].zero?? value.to_i : value
    value.zero?? nil : key == 1.0 ? "#{value} #{singular[key]}" : "#{value} #{key}"
  end.compact.join(', ')
end

#hashObject



66
67
68
# File 'lib/iso8601_basic/duration.rb', line 66

def hash
  [self.class, to_a].hash
end

#inspectObject



70
71
72
# File 'lib/iso8601_basic/duration.rb', line 70

def inspect
  "#<#{self.class.name}: #{iso8601}>"
end

#iso8601Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/iso8601_basic/duration.rb', line 42

def iso8601
  return nil if blank?

  day_parts = [:years, :months, :weeks, :days].collect do |key|
    value = to_h[key].divmod(1)[1].zero?? to_h[key].to_i : to_h[key]
    "#{value}#{key.to_s[0].upcase}" unless value.zero?
  end.compact

  time_parts = [:hours, :minutes, :seconds].collect do |key|
    value = to_h[key].divmod(1)[1].zero?? to_h[key].to_i : to_h[key]
    "#{value}#{key.to_s[0].upcase}" unless value.zero?
  end.compact

  "P#{day_parts.join}#{time_parts.any?? "T#{time_parts.join}" : ''}"
end

#present?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/iso8601_basic/duration.rb', line 30

def present?
  ! blank?
end

#to_aObject



118
119
120
# File 'lib/iso8601_basic/duration.rb', line 118

def to_a
  to_h.to_a
end

#to_fObject



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/iso8601_basic/duration.rb', line 102

def to_f
  { years:   31536000,
    months:  2628000,
    weeks:   604800,
    days:    86400,
    hours:   3600,
    minutes: 60,
    secodns: 1 }.collect do |key, value|
      to_h[key] * value
    end.inject :+
end

#to_hObject



122
123
124
125
126
127
128
129
130
# File 'lib/iso8601_basic/duration.rb', line 122

def to_h
  { years:   @years,
    months:  @months,
    weeks:   @weeks,
    days:    @days,
    hours:   @hours,
    minutes: @minutes,
    seconds: @seconds }
end

#to_iObject



114
115
116
# File 'lib/iso8601_basic/duration.rb', line 114

def to_i
  to_f.to_i
end

#to_jsonObject



82
83
84
# File 'lib/iso8601_basic/duration.rb', line 82

def to_json
  "\"#{iso8601}\""
end

#to_sObject



74
75
76
# File 'lib/iso8601_basic/duration.rb', line 74

def to_s
  iso8601
end