Class: Stupidedi::Versions::Common::ElementTypes::TimeVal

Inherits:
Stupidedi::Values::SimpleElementVal show all
Defined in:
lib/stupidedi/versions/common/element_types/tm.rb

Overview

See Also:

  • B.1.1.3.1.6 Time

Direct Known Subclasses

Invalid, Valid

Defined Under Namespace

Classes: Empty, Invalid, NonEmpty, Valid

Instance Attribute Summary

Attributes inherited from Stupidedi::Values::SimpleElementVal

#position, #usage

Constructors collapse

Instance Method Summary collapse

Methods inherited from Stupidedi::Values::SimpleElementVal

#allowed?, #component?, #copy, #date?, #id?, #initialize, #leaf?, #numeric?, #simple?, #string?, #to_s, #to_x12, #valid?

Methods inherited from Stupidedi::Values::AbstractElementVal

#element?, #size

Methods inherited from Stupidedi::Values::AbstractVal

#blank?, #characters, #component?, #composite?, #definition, #descriptor, #element?, #empty?, #functional_group?, #interchange?, #invalid?, #leaf?, #loop?, #present?, #repeated?, #segment?, #separator?, #simple?, #size, #table?, #transaction_set?, #transmission?, #valid?

Methods included from Color

ansi, #ansi

Methods included from Inspect

#inspect

Constructor Details

This class inherits a constructor from Stupidedi::Values::SimpleElementVal

Class Method Details

.empty(usage, position) ⇒ TimeVal

Returns:



286
287
288
# File 'lib/stupidedi/versions/common/element_types/tm.rb', line 286

def empty(usage, position)
  self::Empty.new(usage, position)
end

.value(object, usage, position) ⇒ TimeVal

Returns:



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/stupidedi/versions/common/element_types/tm.rb', line 291

def value(object, usage, position)
  if object.is_a?(TimeVal)
    object#.copy(:usage => usage, :position => position)
  elsif object.blank?
    self::Empty.new(usage, position)
  elsif object.is_a?(String) or object.is_a?(StringVal)
    return self::Invalid.new(object, usage, position) \
      unless object =~ /^\d{4,}$/

    hour   = object.to_s.slice(0, 2).to_i
    minute = object.to_s.slice(2, 2).try{|mm| mm.to_i unless mm.blank? }
    second = object.to_s.slice(4, 2).try{|ss| ss.to_d unless ss.blank? }

    if decimal = object.to_s.slice(6..-1)
      decimal = 0 if decimal.empty?
      second += "0.#{decimal}".to_d
    end

    self::NonEmpty.new(hour, minute, second, usage, position)
  elsif object.respond_to?(:hour) and
        object.respond_to?(:min)  and
        object.respond_to?(:sec)
    sec = object.sec.to_d

    if object.respond_to?(:usec)
      sec += object.usec.to_d / 1000000
    elsif object.respond_to?(:sec_fraction)
      sec += object.sec_fraction.to_d
    end

    self::NonEmpty.new(object.hour, object.min, sec, usage, position)
  elsif object.respond_to?(:hour)    and
        object.respond_to?(:minute)  and
        object.respond_to?(:second)
    self::NonEmpty.new(object.hour, object.minute, object.second.to_d, usage, position)
  elsif object.is_a?(Array) and object.length == 3
    self::NonEmpty.new(*object, usage, position)
  else
    self::Invalid.new(object, usage, position)
  end
rescue Exceptions::InvalidElementError
  self::Invalid.new(object, usage, position)
end

Instance Method Details

#time?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/stupidedi/versions/common/element_types/tm.rb', line 32

def time?
  true
end

#too_long?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/stupidedi/versions/common/element_types/tm.rb', line 40

def too_long?
  false
end

#too_short?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/stupidedi/versions/common/element_types/tm.rb', line 36

def too_short?
  false
end