Class: Stupidedi::Versions::FunctionalGroups::TwoThousandOne::ElementTypes::DateVal::Improper
Overview
Date with a partially-specified year (two digits, missing century). Shouldn’t be directly instantiated – instead, use the constuctor method value
Instance Attribute Summary collapse
#position, #usage
Instance Method Summary
collapse
#date?, empty, value
#allowed?, #component?, #date?, #id?, #leaf?, #numeric?, #simple?, #string?, #time?
#element?, #size
#blank?, #characters, #component?, #composite?, #definition, #element?, #functional_group?, #interchange?, #invalid?, #loop?, #present?, #repeated?, #segment?, #separator?, #simple?, #size, #table?, #transaction_set?, #transmission?
Methods included from Color
ansi, #ansi
Constructor Details
#initialize(year, month, day, usage, position) ⇒ Improper
Returns a new instance of Improper.
331
332
333
334
335
336
337
338
339
340
341
|
# File 'lib/stupidedi/versions/functional_groups/002001/element_types/date_val.rb', line 331
def initialize(year, month, day, usage, position)
@year, @month, @day = year, month, day
unless @year.between?(0, 99) and @month.between?(1, 12) and @day.between?(1, 31)
raise Exceptions::InvalidElementError,
"invalid date year: #{year}, month: #{month}, day: #{day}"
end
super(usage, position)
end
|
Instance Attribute Details
329
330
331
|
# File 'lib/stupidedi/versions/functional_groups/002001/element_types/date_val.rb', line 329
def day
@day
end
|
326
327
328
|
# File 'lib/stupidedi/versions/functional_groups/002001/element_types/date_val.rb', line 326
def month
@month
end
|
323
324
325
|
# File 'lib/stupidedi/versions/functional_groups/002001/element_types/date_val.rb', line 323
def year
@year
end
|
Instance Method Details
#==(other) ⇒ Boolean
495
496
497
498
499
500
|
# File 'lib/stupidedi/versions/functional_groups/002001/element_types/date_val.rb', line 495
def ==(other)
eql?(other) or
(@day == other.day and
@year == other.year and
@month == other.month)
end
|
#century(cc) ⇒ Proper
Create a proper date using the given century ‘cc`
371
372
373
374
|
# File 'lib/stupidedi/versions/functional_groups/002001/element_types/date_val.rb', line 371
def century(cc)
date = ::Date.civil(100 * cc + @year, @month, @day)
Proper.new(date, usage, position)
end
|
#copy(changes = {}) ⇒ Improper
344
345
346
347
348
349
350
351
|
# File 'lib/stupidedi/versions/functional_groups/002001/element_types/date_val.rb', line 344
def copy(changes = {})
Improper.new \
changes.fetch(:year, @year),
changes.fetch(:month, @month),
changes.fetch(:day, @day),
changes.fetch(:usage, usage),
changes.fetch(:position, position)
end
|
#empty? ⇒ Boolean
357
358
359
|
# File 'lib/stupidedi/versions/functional_groups/002001/element_types/date_val.rb', line 357
def empty?
false
end
|
Create a proper date which cannot be older than the current date
448
449
450
|
# File 'lib/stupidedi/versions/functional_groups/002001/element_types/date_val.rb', line 448
def future
oldest(Date.today)
end
|
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
|
# File 'lib/stupidedi/versions/functional_groups/002001/element_types/date_val.rb', line 453
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("DT.value#{id}") << "(XX#{"%02d-%02d-%02d" % [@year, @month, @day]})"
end
|
#newest(date) ⇒ Proper
Create a proper date which cannot be newer than the given ‘date` and cannot be older than one year before the given `date`.
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
# File 'lib/stupidedi/versions/functional_groups/002001/element_types/date_val.rb', line 416
def newest(date)
cc, yy = date.year.divmod(100)
if @year < yy
century(cc)
elsif @year > yy
century(cc - 1)
else
if @month < date.month
century(cc)
elsif @month > date.month
century(cc - 1)
else
if @day <= date.day
century(cc)
else
century(cc - 1)
end
end
end
end
|
#oldest(date) ⇒ Proper
Create a proper date which cannot be older than the given ‘date` and cannot be newer than one year after the given `date`.
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
# File 'lib/stupidedi/versions/functional_groups/002001/element_types/date_val.rb', line 385
def oldest(date)
cc, yy = date.year.divmod(100)
if @year < yy
century(cc + 1)
elsif @year > yy
century(cc)
else
if @month < date.month
century(cc + 1)
elsif @month > date.month
century(cc)
else
if @day < date.day
century(cc + 1)
else
century(cc)
end
end
end
end
|
Create a proper date which cannot be newer than the current date
441
442
443
|
# File 'lib/stupidedi/versions/functional_groups/002001/element_types/date_val.rb', line 441
def past
newest(Date.today)
end
|
#proper? ⇒ Boolean
361
362
363
|
# File 'lib/stupidedi/versions/functional_groups/002001/element_types/date_val.rb', line 361
def proper?
false
end
|
470
471
472
|
# File 'lib/stupidedi/versions/functional_groups/002001/element_types/date_val.rb', line 470
def to_s
"XX%02d%02d%02d" % [@year, @month, @day]
end
|
#to_x12(truncate = true) ⇒ String
475
476
477
478
|
# File 'lib/stupidedi/versions/functional_groups/002001/element_types/date_val.rb', line 475
def to_x12(truncate = true)
x12 = "%02d%02d%02d" % [@year, @month, @day]
truncate ? x12.slice(0, definition.max_length) : x12
end
|
#too_long? ⇒ Boolean
486
487
488
489
490
491
|
# File 'lib/stupidedi/versions/functional_groups/002001/element_types/date_val.rb', line 486
def too_long?
definition.max_length < 7 and @year > 99
end
|
#too_short? ⇒ Boolean
480
481
482
483
484
|
# File 'lib/stupidedi/versions/functional_groups/002001/element_types/date_val.rb', line 480
def too_short?
definition.min_length > 6
end
|
#valid? ⇒ Boolean
353
354
355
|
# File 'lib/stupidedi/versions/functional_groups/002001/element_types/date_val.rb', line 353
def valid?
true
end
|