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:

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

+ (TimeVal) empty(usage, position)



273
274
275
# File 'lib/stupidedi/versions/functional_groups/004010/element_types/time_val.rb', line 273

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

+ (TimeVal) value(object, usage, position)



278
279
280
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
# File 'lib/stupidedi/versions/functional_groups/004010/element_types/time_val.rb', line 278

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

- (Boolean) time?



32
33
34
# File 'lib/stupidedi/versions/functional_groups/004010/element_types/time_val.rb', line 32

def time?
  true
end

- (Boolean) too_long?



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

def too_long?
  false
end

- (Boolean) too_short?



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

def too_short?
  false
end