Class: Stupidedi::Versions::FunctionalGroups::FiftyTen::ElementTypes::StringVal::NonEmpty

Inherits:
Stupidedi::Versions::FunctionalGroups::FiftyTen::ElementTypes::StringVal show all
Extended by:
Forwardable, Operators::Relational, Operators::Unary, Operators::Wrappers
Includes:
Comparable
Defined in:
lib/stupidedi/versions/functional_groups/005010/element_types/string_val.rb

Overview

Non-empty string value. Shouldn’t be directly instantiated – instead, use the value constructor.

Instance Attribute Summary collapse

Attributes inherited from Stupidedi::Values::SimpleElementVal

#position, #usage

Instance Method Summary collapse

Methods included from Operators::Wrappers

wrappers

Methods included from Operators::Unary

unary_operators

Methods included from Operators::Relational

relational_operators

Methods inherited from Stupidedi::Versions::FunctionalGroups::FiftyTen::ElementTypes::StringVal

empty, #map, #string?, value

Methods inherited from Stupidedi::Values::SimpleElementVal

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

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?

Methods included from Color

ansi, #ansi

Constructor Details

#initialize(string, usage, position) ⇒ NonEmpty

Returns a new instance of NonEmpty.



222
223
224
225
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/string_val.rb', line 222

def initialize(string, usage, position)
  @value = string
  super(usage, position)
end

Instance Attribute Details

#valueString (readonly)

Returns:



194
195
196
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/string_val.rb', line 194

def value
  @value
end

Instance Method Details

#coerce(other) ⇒ Object



235
236
237
238
239
240
241
242
243
244
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/string_val.rb', line 235

def coerce(other)
  # me, he = other.coerce(self)
  # me <OP> he
  if other.respond_to?(:to_str)
    return copy(:value => other.to_str), self
  else
    raise TypeError,
      "cannot coerce StringVal to #{other.class}"
  end
end

#copy(changes = {}) ⇒ StringVal

Returns:



228
229
230
231
232
233
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/string_val.rb', line 228

def copy(changes = {})
  StringVal.value \
    changes.fetch(:value, @value),
    changes.fetch(:usage, usage),
    changes.fetch(:position, position)
end

#inspectString

Returns:



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/string_val.rb', line 261

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

  ansi.element("AN.value#{id}") << "(#{@value})"
end

#to_date(format) ⇒ Object



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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/string_val.rb', line 281

def to_date(format)
  case format
  when "D8"  # CCYYMMDD
    Date.civil(@value.slice(0, 4).to_i, # year
               @value.slice(4, 2).to_i, # month
               @value.slice(6, 2).to_i) # day
  when "DB"  # MMDDCCYY
    Date.civil(@value.slice(4, 4).to_i,
               @value.slice(0, 2).to_i,
               @value.slice(2, 2).to_i)
  when "DDT" # CCYYMMDD-CCYYMMDDHHMM
    Time.utc(@value.slice(0, 4).to_i,     # year
             @value.slice(4, 2).to_i,     # month
             @value.slice(6, 2).to_i) ..  # day
    Time.utc(@value.slice(9, 4).to_i,
             @value.slice(13, 2).to_i,
             @value.slice(15, 2).to_i,
             @value.slice(17, 2).to_i,    # hour
             @value.slice(19, 2).to_i)    # minute
  when "DT"  # CCYYMMDDHHMM
    Time.utc(@value.slice(0, 4).to_i,
             @value.slice(4, 2).to_i,
             @value.slice(6, 2).to_i,
             @value.slice(8, 2).to_i,
             @value.slice(10, 2).to_i)
  when "DTD" # CCYYMMDDHHMM-CCYYMMDD
    Time.utc(@value.slice(0, 4).to_i,
             @value.slice(4, 2).to_i,
             @value.slice(6, 2).to_i,
             @value.slice(8, 2).to_i,
             @value.slice(10, 2).to_i) ..
    Time.utc(@value.slice(13, 4).to_i,
             @value.slice(15, 2).to_i,
             @value.slice(17, 2).to_i)
  when "DTS" # CCYYMMDDHHMMSS-CCYYMMDDHHMMSS
    Time.utc(@value.slice(0, 4).to_i,
             @value.slice(4, 2).to_i,
             @value.slice(6, 2).to_i,
             @value.slice(8, 2).to_i,
             @value.slice(10, 2).to_i,
             @value.slice(12, 2).to_i) ..
    Time.utc(@value.slice(15, 4).to_i,
             @value.slice(19, 2).to_i,
             @value.slice(21, 2).to_i,
             @value.slice(23, 2).to_i,
             @value.slice(25, 2).to_i,
             @value.slice(27, 2).to_i)
  when "RD"  # MMDDCCYY-MMDDCCYY
    Date.civil(@value.slice(4, 4).to_i,
               @value.slice(0, 2).to_i,
               @value.slice(2, 2).to_i) ..
    Date.civil(@value.slice(13, 4).to_i,
               @value.slice(9, 2).to_i,
               @value.slice(11, 2).to_i)
  when "RD8" # CCYYMMDD-CCYYMMDD
    Date.civil(@value.slice(0, 4).to_i,
               @value.slice(4, 2).to_i,
               @value.slice(6, 2).to_i) ..
    Date.civil(@value.slice(9, 4).to_i,
               @value.slice(13, 2).to_i,
               @value.slice(15, 2).to_i)
  when "RDT" # CCYYMMDDHHMM-CCYYMMDDHHMM
    Time.utc(@value.slice(0, 4).to_i,
             @value.slice(4, 2).to_i,
             @value.slice(6, 2).to_i,
             @value.slice(8, 2).to_i,
             @value.slice(10, 2).to_i) ..
    Time.utc(@value.slice(13, 4).to_i,
             @value.slice(15, 2).to_i,
             @value.slice(17, 2).to_i,
             @value.slice(19, 2).to_i,
             @value.slice(21, 2).to_i)
  when "RTS" # CCYYMMDDHHMMSS
    Time.utc(@value.slice(0, 4).to_i,
             @value.slice(4, 2).to_i,
             @value.slice(6, 2).to_i,
             @value.slice(8, 2).to_i,
             @value.slice(10, 2).to_i)
  else
    raise ArgumentError,
      "Format code #{format} is not recognized"
  end
end

#to_x12(truncate = true) ⇒ String

Returns:



255
256
257
258
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/string_val.rb', line 255

def to_x12(truncate = true)
  x12 = @value.ljust(definition.min_length, " ")
  truncate ? x12.take(definition.max_length) : x12
end

#too_long?Boolean

Returns:

  • (Boolean)


246
247
248
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/string_val.rb', line 246

def too_long?
  @value.lstrip.length > definition.max_length
end

#too_short?Boolean

Returns:

  • (Boolean)


250
251
252
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/string_val.rb', line 250

def too_short?
  @value.lstrip.length < definition.min_length
end

#valid?Boolean

Returns:

  • (Boolean)


277
278
279
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/string_val.rb', line 277

def valid?
  true
end