File: duration.rb

package info (click to toggle)
ruby-kdl 1.0.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 480 kB
  • sloc: ruby: 6,667; yacc: 72; sh: 5; makefile: 4
file content (28 lines) | stat: -rw-r--r-- 807 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
require 'kdl/types/duration/iso8601_parser'

module KDL
  module Types
    class Duration < Value
      attr_reader :years, :months, :weeks, :days, :hours, :minutes, :seconds

      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

      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
    end
    MAPPING['duration'] = Duration
  end
end