Class: IsbmAdaptor::Duration
- Inherits:
-
Object
- Object
- IsbmAdaptor::Duration
- Defined in:
- lib/isbm_adaptor/duration.rb
Instance Attribute Summary collapse
-
#days ⇒ Numeric
The days component of the duration.
-
#hours ⇒ Numeric
The hours component of the duration.
-
#minutes ⇒ Numeric
The minutes component of the duration.
-
#months ⇒ Numeric
The months component of the duration.
-
#seconds ⇒ Numeric
The seconds component of the duration.
-
#years ⇒ Numeric
The years component of the duration.
Instance Method Summary collapse
-
#initialize(duration) ⇒ Duration
constructor
Creates a new Duration based on specified time components.
-
#to_hash ⇒ Hash
Creates a hash of the time components.
-
#to_s ⇒ String
ISO 8601 formatted duration.
Constructor Details
#initialize(duration) ⇒ Duration
Creates a new Duration based on specified time components.
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
#days ⇒ Numeric
Returns the days component of the duration.
10 11 12 |
# File 'lib/isbm_adaptor/duration.rb', line 10 def days @days end |
#hours ⇒ Numeric
Returns the hours component of the duration.
13 14 15 |
# File 'lib/isbm_adaptor/duration.rb', line 13 def hours @hours end |
#minutes ⇒ Numeric
Returns the minutes component of the duration.
16 17 18 |
# File 'lib/isbm_adaptor/duration.rb', line 16 def minutes @minutes end |
#months ⇒ Numeric
Returns the months component of the duration.
7 8 9 |
# File 'lib/isbm_adaptor/duration.rb', line 7 def months @months end |
#seconds ⇒ Numeric
Returns the seconds component of the duration.
19 20 21 |
# File 'lib/isbm_adaptor/duration.rb', line 19 def seconds @seconds end |
#years ⇒ Numeric
Returns 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_hash ⇒ Hash
Creates a hash of the time components. Any keys with nil values are excluded from the result.
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_s ⇒ String
Returns 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 |