Class: RDF::Literal::Duration

Inherits:
RDF::Literal show all
Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/rdf-xsd-e532a0ab7189/lib/rdf/xsd/duration.rb

Overview

A duration literal.

Constant Summary collapse

DATATYPE =
RDF::XSD.duration
GRAMMAR =
%r(\A
  (?<si>-)?
  P(?:(?:(?:(?:(?<yr>\d+)Y)(?:(?<mo>\d+)M)?(?:(?<da>\d+)D)?)
      |  (?:(?:(?<mo>\d+)M)(?:(?<da>\d+)D)?)
      |  (?:(?<da>\d+)D)
      )
      (?:T(?:(?:(?:(?<hr>\d+)H)(?:(?<mi>\d+)M)?(?<se>\d+(?:\.\d+)?S)?)
          |  (?:(?:(?<mi>\d+)M)(?:(?<se>\d+(?:\.\d+)?)S)?)
          |  (?:(?<se>\d+(?:\.\d+)?)S)
          )
      )?
   |(?:T(?:(?:(?:(?<hr>\d+)H)(?:(?<mi>\d+)M)?(?<se>\d+(?:\.\d+)?S)?)
        |  (?:(?:(?<mi>\d+)M)(?:(?<se>\d+(?:\.\d+)?)S)?)
        |   (?:(?<se>\d+(?:\.\d+)?)S)
        )
    )
   )
\z)x.freeze

Instance Method Summary collapse

Methods inherited from RDF::Literal

#inspect, #valid_extended?

Constructor Details

#initialize(value, datatype: nil, lexical: nil, **options) ⇒ Duration

  • Given a Numeric, assumes that it is milliseconds
  • Given a String, parse as xsd:duration
  • Hash form is used for internal representation

Parameters:

Options Hash (**options):

  • :lexical (String) — default: nil


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-xsd-e532a0ab7189/lib/rdf/xsd/duration.rb', line 36

def initialize(value, datatype: nil, lexical: nil, **options)
  super
  @object   = case value
  when Hash
    value = value.dup
    value[:yr] ||= value[:years]
    value[:mo] ||= value[:months]
    value[:da] ||= value[:days]
    value[:hr] ||= value[:hours]
    value[:mi] ||= value[:minutes]
    value[:se] ||= value[:seconds]
    
    value
  when Duration, Numeric
    {:se => value.to_f}
  else
    parse(value.to_s)
  end
end

Instance Method Details

#==(other) ⇒ Object

Equal compares as DateTime objects



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-xsd-e532a0ab7189/lib/rdf/xsd/duration.rb', line 149

def ==(other)
  # If lexically invalid, use regular literal testing
  return super unless self.valid?

  case other
  when Duration
    return super unless other.valid?
    self.to_f == other.to_f
  when String
    self.to_s(:xml) == other
  when Numeric
    self.to_f == other
  when Literal::DateTime, Literal::Time, Literal::Date
    false
  else
    super
  end
end

#canonicalize!RDF::Literal

Converts this literal into its canonical lexical representation.

Also normalizes elements



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-xsd-e532a0ab7189/lib/rdf/xsd/duration.rb', line 63

def canonicalize!
  @string = @humanize = nil
  if @object[:se].to_i > 60
    m_r = (@object[:se].to_f / 60) - 1
    @object[:se] -=  m_r * 60
    @object[:mi] = @object[:mi].to_i + m_r
  end
  if @object[:mi].to_i > 60
    h_r = (@object[:mi].to_i / 60) - 1
    @object[:mi] -=  h_r * 60
    @object[:hr] = @object[:hr].to_i +  h_r
  end
  if @object[:hr].to_i > 24
    d_r = (@object[:hr].to_i / 24) - 1
    @object[:hr] -=  d_r * 24
    @object[:da] = @object[:da].to_i + d_r
  end
  if @object[:da].to_i > 30
    m_r = (@object[:da].to_i / 30) - 1
    @object[:da] -=  m_r * 30
    @object[:mo] = @object[:mo].to_i + m_r
  end
  if @object[:mo].to_i > 12
    y_r = (@object[:mo].to_i / 12) - 1
    @object[:mo] -=  y_r * 12
    @object[:yr] = @object[:yr].to_i + y_r
  end
  @object.to_s  # side-effect
  self
end

#humanize(lang = :en) ⇒ Object

Returns a human-readable value for the interval



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-xsd-e532a0ab7189/lib/rdf/xsd/duration.rb', line 128

def humanize(lang = :en)
  @humanize ||= {}
  @humanize[lang] ||= begin
    # Just english, for now
    ar = []
    ar << plural(@object[:yr], "year")
    ar << plural(@object[:mo], "month")
    ar << plural(@object[:da], "day")
    ar << plural(@object[:hr], "hour")
    ar << plural(@object[:mi], "minute")
    ar << plural(sec_str, "second") if @object[:se]
    ar = ar.compact
    last = ar.pop
    first = ar.join(" ")
    res = first.empty? ? last : "#{first} and #{last}"
    @object[:si] == '-' ? "#{res} ago" : res
  end
end

#plural(v, str) ⇒ Object



122
123
124
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-xsd-e532a0ab7189/lib/rdf/xsd/duration.rb', line 122

def plural(v, str)
  "#{v} #{str}#{v.to_i == 1 ? '' : 's'}" if v
end

#to_fFloat

Returns:



169
170
171
172
173
174
175
176
177
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-xsd-e532a0ab7189/lib/rdf/xsd/duration.rb', line 169

def to_f
  ( @object[:yr].to_i * 365 * 24 * 3600 +
    @object[:mo].to_i * 30 * 24 * 3600 +
    @object[:da].to_i * 24 * 3600 +
    @object[:hr].to_i * 3600 +
    @object[:mi].to_i * 60 +
    @object[:se].to_f
  ) * (@object[:si] == '-' ? -1 : 1)
end

#to_iInteger

Returns:



180
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-xsd-e532a0ab7189/lib/rdf/xsd/duration.rb', line 180

def to_i; Integer(self.to_f); end

#to_sString

Returns the value as a string.

Returns:



109
110
111
112
113
114
115
116
117
118
119
120
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-xsd-e532a0ab7189/lib/rdf/xsd/duration.rb', line 109

def to_s
  @string ||= begin
    str = @object[:si] == '-' ? "-P" : "P"
    str << "%dY" % @object[:yr].to_i if @object[:yr]
    str << "%dM" % @object[:mo].to_i if @object[:mo]
    str << "%dD" % @object[:da].to_i if @object[:da]
    str << "T" if @object[:hr] || @object[:mi] || @object[:se]
    str << "%dH" % @object[:hr].to_i if @object[:hr]
    str << "%dM" % @object[:mi].to_i if @object[:mi]
    str << "#{sec_str}S" if @object[:se]
  end
end

#valid?Boolean

Returns true if the value adheres to the defined grammar of the datatype.

Special case for date and dateTime, for which '0000' is not a valid year

Returns:

  • (Boolean)


101
102
103
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-xsd-e532a0ab7189/lib/rdf/xsd/duration.rb', line 101

def valid?
  !!(value =~ GRAMMAR)
end