Class: IsbmAdaptor::Duration

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(duration) ⇒ Duration

Creates a new Duration based on specified time components.

Parameters:

  • duration (Hash)

    a customizable set of options

Options Hash (duration):

  • :years (Numeric)

    duration in years

  • :months (Numeric)

    duration in months

  • :days (Numeric)

    duration in days

  • :hours (Numeric)

    duration in hours

  • :minutes (Numeric)

    duration in minutes

  • :seconds (Numeric)

    duration in seconds



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/isbm_adaptor/duration.rb', line 29

def initialize(duration)
  duration.keys.each do |key|
    raise ArgumentError.new "Invalid key: #{key}" unless VALID_SYMBOLS.include?(key)
  end

  duration.each do |key, value|
    raise ArgumentError.new "Value for #{key} cannot be less than 0" if value < 0
  end

  @years = duration[:years]
  @months = duration[:months]
  @days = duration[:days]
  @hours = duration[:hours]
  @minutes = duration[:minutes]
  @seconds = duration[:seconds]
end

Instance Attribute Details

#daysNumeric

Returns the days component of the duration.

Returns:

  • (Numeric)

    the days component of the duration



10
11
12
# File 'lib/isbm_adaptor/duration.rb', line 10

def days
  @days
end

#hoursNumeric

Returns the hours component of the duration.

Returns:

  • (Numeric)

    the hours component of the duration



13
14
15
# File 'lib/isbm_adaptor/duration.rb', line 13

def hours
  @hours
end

#minutesNumeric

Returns the minutes component of the duration.

Returns:

  • (Numeric)

    the minutes component of the duration



16
17
18
# File 'lib/isbm_adaptor/duration.rb', line 16

def minutes
  @minutes
end

#monthsNumeric

Returns the months component of the duration.

Returns:

  • (Numeric)

    the months component of the duration



7
8
9
# File 'lib/isbm_adaptor/duration.rb', line 7

def months
  @months
end

#secondsNumeric

Returns the seconds component of the duration.

Returns:

  • (Numeric)

    the seconds component of the duration



19
20
21
# File 'lib/isbm_adaptor/duration.rb', line 19

def seconds
  @seconds
end

#yearsNumeric

Returns the years component of the duration.

Returns:

  • (Numeric)

    the years component of the duration



4
5
6
# File 'lib/isbm_adaptor/duration.rb', line 4

def years
  @years
end

Instance Method Details

#to_hashHash

Creates a hash of the time components. Any keys with nil values are excluded from the result.

Returns:

  • (Hash)

    all specified time components



73
74
75
76
77
78
79
80
81
82
# File 'lib/isbm_adaptor/duration.rb', line 73

def to_hash
  hash = {}
  hash[:years] = @years if @years
  hash[:months] = @months if @months
  hash[:days] = @days if @days
  hash[:hours] = @hours if @hours
  hash[:minutes] = @minutes if @minutes
  hash[:seconds] = @seconds if @seconds
  hash
end

#to_sString

Returns ISO 8601 formatted duration.

Returns:

  • (String)

    ISO 8601 formatted duration



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/isbm_adaptor/duration.rb', line 47

def to_s
  date = []
  date << "#{@years}Y" unless @years.nil?
  date << "#{@months}M" unless @months.nil?
  date << "#{@days}D" unless @days.nil?

  time = []
  time << "#{@hours}H" unless @hours.nil?
  time << "#{@minutes}M" unless @minutes.nil?
  time << "#{@seconds}S" unless @seconds.nil?

  result = nil

  if !date.empty? || !time.empty?
    result = 'P'
    result += date.join unless date.empty?
    result += 'T' + time.join unless time.empty?
  end

  result
end