Class: KDL::Types::Duration

Inherits:
Value::Custom show all
Defined in:
lib/kdl/types/duration.rb,
lib/kdl/types/duration/iso8601_parser.rb

Defined Under Namespace

Classes: ISO8601Parser

Constant Summary

Constants inherited from Value

Value::Null

Instance Attribute Summary collapse

Attributes inherited from Value::Custom

#oriinal_value

Attributes inherited from Value

#format, #type, #value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Value::Custom

#to_v1, #to_v2, #version

Methods inherited from Value

#==, #as_type, from, #inspect, #method_missing, #respond_to_missing?, #stringify_value, #to_s, #to_v2, #version

Constructor Details

#initialize(parts = {}, format: nil, type: 'duration') ⇒ Duration

Returns a new instance of Duration.



10
11
12
13
14
15
16
17
18
19
# File 'lib/kdl/types/duration.rb', line 10

def initialize(parts = {}, format: nil, type: 'duration')
  super
  @years = parts.fetch(:years, 0)
  @months = parts.fetch(:months, 0)
  @weeks = parts.fetch(:weeks, 0)
  @days = parts.fetch(:days, 0)
  @hours = parts.fetch(:hours, 0)
  @minutes = parts.fetch(:minutes, 0)
  @seconds = parts.fetch(:seconds, 0)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class KDL::Value

Instance Attribute Details

#daysObject (readonly)

Returns the value of attribute days.



8
9
10
# File 'lib/kdl/types/duration.rb', line 8

def days
  @days
end

#hoursObject (readonly)

Returns the value of attribute hours.



8
9
10
# File 'lib/kdl/types/duration.rb', line 8

def hours
  @hours
end

#minutesObject (readonly)

Returns the value of attribute minutes.



8
9
10
# File 'lib/kdl/types/duration.rb', line 8

def minutes
  @minutes
end

#monthsObject (readonly)

Returns the value of attribute months.



8
9
10
# File 'lib/kdl/types/duration.rb', line 8

def months
  @months
end

#secondsObject (readonly)

Returns the value of attribute seconds.



8
9
10
# File 'lib/kdl/types/duration.rb', line 8

def seconds
  @seconds
end

#weeksObject (readonly)

Returns the value of attribute weeks.



8
9
10
# File 'lib/kdl/types/duration.rb', line 8

def weeks
  @weeks
end

#yearsObject (readonly)

Returns the value of attribute years.



8
9
10
# File 'lib/kdl/types/duration.rb', line 8

def years
  @years
end

Class Method Details

.call(value, type = 'duration') ⇒ Object



21
22
23
24
25
26
# File 'lib/kdl/types/duration.rb', line 21

def self.call(value, type = 'duration')
  return nil unless value.is_a? ::KDL::Value::String

  parts = ISO8601Parser.new(value.value).parse!
  new(parts, type: type)
end