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

Inherits:
Valid show all
Defined in:
lib/stupidedi/versions/common/element_types/tm.rb

Instance Attribute Summary collapse

Attributes inherited from Stupidedi::Values::SimpleElementVal

#position, #usage

Instance Method Summary collapse

Methods inherited from Valid

#==, #coerce, #map, #valid?

Methods inherited from Stupidedi::Versions::Common::ElementTypes::TimeVal

empty, #time?, #too_long?, value

Methods inherited from Stupidedi::Values::SimpleElementVal

#allowed?, #component?, #date?, #id?, #leaf?, #numeric?, #simple?, #string?, #time?, #too_long?, #valid?

Methods inherited from Stupidedi::Values::AbstractElementVal

#element?, #size

Methods inherited from Stupidedi::Values::AbstractVal

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

Methods included from Color

ansi, #ansi

Constructor Details

#initialize(hour, minute, second, usage, position) ⇒ NonEmpty

Returns a new instance of NonEmpty.



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/stupidedi/versions/common/element_types/tm.rb', line 182

def initialize(hour, minute, second, usage, position)
  @hour, @minute, @second = hour, minute, second

  valid   = (  hour.nil? or hour.between?(0, 24))
  valid &&= (minute.nil? or not hour.nil?)
  valid &&= (minute.nil? or minute.between?(0, 60))
  valid &&= (second.nil? or not minute.nil?)
  valid &&= (second.nil? or second.between?(0, 60))

  super(usage, position)

  unless valid
    raise Exceptions::InvalidElementError,
      "invalid time #{inspect}"
  end
end

Instance Attribute Details

#hourInteger (readonly)

Returns:

  • (Integer)


174
175
176
# File 'lib/stupidedi/versions/common/element_types/tm.rb', line 174

def hour
  @hour
end

#minuteInteger (readonly)

Returns:

  • (Integer)


177
178
179
# File 'lib/stupidedi/versions/common/element_types/tm.rb', line 177

def minute
  @minute
end

#secondInteger, BigDecimal (readonly)

Returns:

  • (Integer, BigDecimal)


180
181
182
# File 'lib/stupidedi/versions/common/element_types/tm.rb', line 180

def second
  @second
end

Instance Method Details

#copy(changes = {})



199
200
201
202
203
204
205
206
207
# File 'lib/stupidedi/versions/common/element_types/tm.rb', line 199

def copy(changes = {})
  if [:hour, :minute, :second].any?{|x| changes.include?(x) }
    changes[:value] = [changes.fetch(:hour, @hour),
                       changes.fetch(:minute, @minute),
                       changes.fetch(:second, @second)]
  end

  super(changes)
end

#empty?Boolean

Returns:

  • (Boolean)


213
214
215
# File 'lib/stupidedi/versions/common/element_types/tm.rb', line 213

def empty?
  false
end

#inspectString

:nocov:

Returns:

  • (String)


237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/stupidedi/versions/common/element_types/tm.rb', line 237

def inspect
  id = definition.bind do |d|
    "[#{"% 5s" % d.id}: #{d.name}]".bind do |s|
      if usage.forbidden?
        ansi.forbidden(s)
      elsif usage.required?
        ansi.required(s)
      else
        ansi.optional(s)
      end
    end
  end

  hh =   @hour.try{|h| "%02d" % h }  || "hh"
  mm = @minute.try{|m| "%02d" % m }  || "mm"
  ss = @second.try{|s| s.to_s("F") } || "ss"
  ss = ss.gsub(/^0*\.|\.0*$/, "")

  ansi.element("TM.value#{id}") + "(#{hh}:#{mm}:#{ss})"
end

#to_s(hh = "hh", mm = "mm", ss = "ss") ⇒ String

Returns:

  • (String)


260
261
262
263
264
265
# File 'lib/stupidedi/versions/common/element_types/tm.rb', line 260

def to_s(hh = "hh", mm = "mm", ss = "ss")
  hh =   @hour.try{|h| "%02d" % h } || hh
  mm = @minute.try{|m| "%02d" % m } || mm
  ss = @second.try{|s| "%02f" % s } || ss
  "#{hh}:#{mm}:#{ss}".gsub(/\.?0*$|:$/, "")
end

#to_time(date, minute = nil, second = nil) ⇒ Time

Returns:

  • (Time)


222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/stupidedi/versions/common/element_types/tm.rb', line 222

def to_time(date, minute = nil, second = nil)
  minute = @minute || minute
  second = @second || second

  if not second.nil?
    Time.utc(date.year, date.month, date.day, @hour, minute, second.to_f)
  elsif not minute.nil?
    Time.utc(date.year, date.month, date.day, @hour, minute)
  else
    Time.utc(date.year, date.month, date.day, @hour)
  end
end

#to_x12(truncate = true) ⇒ String

Returns:

  • (String)


268
269
270
271
272
273
274
275
276
277
# File 'lib/stupidedi/versions/common/element_types/tm.rb', line 268

def to_x12(truncate = true)
  hh =   @hour.try{|h| "%02d" % h }
  mm = @minute.try{|m| "%02d" % m }
  ss = @second.try{|s| "%02d" % s }
  ff = @second.try{|s| s.frac.to_s("F").gsub(/^0*\.|0+$/, "") }

  x12 = "#{hh}#{mm}#{ss}#{ff}"

  truncate ? x12.take(definition.max_length) : x12
end

#too_short?Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/stupidedi/versions/common/element_types/tm.rb', line 217

def too_short?
  to_x12(false).length < definition.min_length
end

#value



209
210
211
# File 'lib/stupidedi/versions/common/element_types/tm.rb', line 209

def value
  [@hour, @minute, @second]
end