Class: Stupidedi::Versions::Common::ElementTypes::DateVal::Proper

Inherits:
Valid show all
Extended by:
Operators::Relational, Operators::Unary, Operators::Wrappers
Defined in:
lib/stupidedi/versions/common/element_types/dt.rb

Overview

Date with a fully-specified year (with century). 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::Relational

relational_operators

Methods included from Operators::Unary

unary_operators

Methods included from Operators::Wrappers

wrappers

Methods inherited from Valid

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

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

#date?, empty, #too_short?, value

Methods inherited from Stupidedi::Values::SimpleElementVal

#allowed?, #component?, #copy, #date?, #id?, #leaf?, #numeric?, #simple?, #string?, #time?, #to_s, #too_short?, #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(value, usage, position) ⇒ Proper

Returns a new instance of Proper.


214
215
216
217
218
219
220
221
222
223
224
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 214

def initialize(value, usage, position)
  @value = value

  # Check that date is reasonably valid
  unless @value.year.between?(0, 9999) and @value.month.between?(1, 12) and @value.day.between?(1, 31)
    raise Exceptions::InvalidElementError,
      "invalid date: year(#{year}) month(#{month}) day(#{day})"
  end

  super(usage, position)
end

Instance Attribute Details

#value (readonly)

Returns the value of attribute value


212
213
214
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 212

def value
  @value
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)

226
227
228
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 226

def empty?
  false
end

#futureProper

Returns self.

Returns:


271
272
273
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 271

def future
  self
end

#inspectString

:nocov:

Returns:

  • (String)

282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 282

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}") + "(#{"%04d-%02d-%02d" % [year, month, day]})"
end

#newest(date) ⇒ Proper

Returns self.

Returns:


266
267
268
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 266

def newest(date)
  self
end

#oldest(date) ⇒ Proper

Returns self.

Returns:


261
262
263
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 261

def oldest(date)
  self
end

#pastProper

Returns self.

Returns:


276
277
278
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 276

def past
  self
end

#proper?Boolean

Returns:

  • (Boolean)

230
231
232
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 230

def proper?
  true
end

#to_dateDate

Returns:

  • (Date)

235
236
237
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 235

def to_date
  @value
end

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

Returns:

  • (Time)

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

def to_time(hour = nil, minute = nil, second = nil)
  if minute.nil? and second.nil? and hour.respond_to?(:hour)
    if hour.respond_to?(:minute) and hour.respond_to?(:second)
      hour, minute, second = hour.hour, hour.minute, hour.second
    elsif hour.respond_to?(:min) and hour.respond_to?(:sec)
      hour, minute, second = hour.hour, hour.min, hour.sec
    end
  end

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

#to_x12(truncate = true) ⇒ String

Returns:

  • (String)

300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 300

def to_x12(truncate = true)
  x12 =
    if definition.max_length < 8
      "%02d%02d%02d" % [year % 100, month, day]
    else
      "%04d%02d%02d" % [year % 10000, month, day]
    end

  if truncate
    x12.slice(-definition.max_length..-1)
  else
    x12
  end
end

#too_long?Boolean

Returns:

  • (Boolean)

315
316
317
318
319
320
321
# File 'lib/stupidedi/versions/common/element_types/dt.rb', line 315

def too_long?
  if definition.max_length < 8
    definition.max_length - 2 < year.to_s.length
  else
    definition.max_length - 4 < year.to_s.length
  end
end