Class: Stupidedi::Versions::Common::ElementTypes::DateVal::Improper
- Inherits:
-
Valid
show all
- Defined in:
- lib/stupidedi/versions/common/element_types/dt.rb
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
Methods inherited from Valid
#==, #coerce, #map, #valid?
#date?, empty, value
#allowed?, #component?, #date?, #id?, #leaf?, #numeric?, #simple?, #string?, #time?, #valid?
#element?, #size
#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(year, month, day, usage, position) ⇒ Improper
Returns a new instance of Improper.
339
340
341
342
343
344
345
346
347
348
349
|
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 339
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
#day ⇒ Integer
337
338
339
|
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 337
def day
@day
end
|
#month ⇒ Integer
334
335
336
|
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 334
def month
@month
end
|
#year ⇒ Integer
331
332
333
|
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 331
def year
@year
end
|
Instance Method Details
#century(cc) ⇒ Proper
Create a proper date using the given century cc
392
393
394
395
|
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 392
def century(cc)
date = ::Date.civil(100 * cc + @year, @month, @day)
Proper.new(date, usage, position)
end
|
#copy(changes = {})
355
356
357
358
359
360
361
362
363
|
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 355
def copy(changes = {})
if [:year, :month, :day].any?{|k| changes.include?(k) }
changes[:value] = [changes.fetch(:year, @year),
changes.fetch(:month, @month),
changes.fetch(:day, @day)]
end
super(changes)
end
|
#empty? ⇒ Boolean
365
366
367
|
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 365
def empty?
false
end
|
Create a proper date which cannot be older than the current date
469
470
471
|
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 469
def future
oldest(Date.today)
end
|
#inspect ⇒ String
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
|
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 475
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
.
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
|
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 437
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
.
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
|
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 406
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
462
463
464
|
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 462
def past
newest(Date.today)
end
|
#proper? ⇒ Boolean
369
370
371
|
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 369
def proper?
false
end
|
#to_s ⇒ String
493
494
495
|
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 493
def to_s
"XX%02d%02d%02d" % [@year, @month, @day]
end
|
#to_x12(truncate = true) ⇒ String
498
499
500
|
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 498
def to_x12(truncate = true)
"%02d%02d%02d" % [@year, @month, @day]
end
|
#too_long? ⇒ Boolean
379
380
381
382
383
384
|
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 379
def too_long?
definition.max_length < 7 and @year > 99
end
|
#too_short? ⇒ Boolean
373
374
375
376
377
|
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 373
def too_short?
definition.min_length > 6
end
|
#value
351
352
353
|
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 351
def value
[@year, @month, @day]
end
|