Class: Stupidedi::Versions::FunctionalGroups::FortyTen::ElementTypes::TimeVal

Inherits:
Stupidedi::Values::SimpleElementVal show all
Defined in:
lib/stupidedi/versions/functional_groups/004010/element_types/time_val.rb

Overview

See Also:

  • B.1.1.3.1.6 Time

Direct Known Subclasses

Empty, Invalid, NonEmpty

Defined Under Namespace

Classes: Empty, Invalid, NonEmpty

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_x12

Methods inherited from Stupidedi::Values::AbstractElementVal

#element?, #size

Methods inherited from Stupidedi::Values::AbstractVal

#blank?, #characters, #component?, #composite?, #definition, #element?, #empty?, #functional_group?, #interchange?, #invalid?, #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



276
277
278
# File 'lib/stupidedi/versions/functional_groups/004010/element_types/time_val.rb', line 276

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

.value(object, usage, position) ⇒ TimeVal



281
282
283
284
285
286
287
288
289
290
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
# File 'lib/stupidedi/versions/functional_groups/004010/element_types/time_val.rb', line 281

def value(object, usage, position)
  if 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+$/

    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)
      second += "0.#{decimal}".to_d
    end

    self::NonEmpty.new(hour, minute, second, usage, position)

  elsif object.is_a?(Time)
    self::NonEmpty.new(object.hour, object.min,
                          object.sec.to_d \
                          + (object.usec.to_d / 1000000),
                          usage, position)

  elsif object.is_a?(DateTime)
    self::NonEmpty.new(object.hour, object.min,
                          object.sec.to_d \
                          + object.sec_fraction.to_d,
                          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



35
36
37
# File 'lib/stupidedi/versions/functional_groups/004010/element_types/time_val.rb', line 35

def time?
  true
end

#too_long?Boolean



43
44
45
# File 'lib/stupidedi/versions/functional_groups/004010/element_types/time_val.rb', line 43

def too_long?
  false
end

#too_short?Boolean



39
40
41
# File 'lib/stupidedi/versions/functional_groups/004010/element_types/time_val.rb', line 39

def too_short?
  false
end