Class: Stupidedi::Versions::FunctionalGroups::FiftyTen::ElementTypes::DateVal::Proper

Inherits:
Stupidedi::Versions::FunctionalGroups::FiftyTen::ElementTypes::DateVal show all
Extended by:
Operators::Relational, Operators::Unary, Operators::Wrappers
Includes:
Comparable
Defined in:
lib/stupidedi/versions/functional_groups/005010/element_types/date_val.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 Stupidedi::Versions::FunctionalGroups::FiftyTen::ElementTypes::DateVal

#date?, empty, #too_short?, 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?, #functional_group?, #interchange?, #invalid?, #loop?, #present?, #repeated?, #segment?, #separator?, #simple?, #size, #table?, #transaction_set?, #transmission?

Methods included from Color

ansi, #ansi

Constructor Details

#initialize(value, usage, position) ⇒ Proper



192
193
194
195
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/date_val.rb', line 192

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

Instance Attribute Details

#value (readonly)

Returns the value of attribute value



190
191
192
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/date_val.rb', line 190

def value
  @value
end

Instance Method Details

#coerce(other)



205
206
207
208
209
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/date_val.rb', line 205

def coerce(other)
  # me, he = other.coerce(self)
  # me <OP> he
  return DateVal.value(other, usage, position), self
end

#copy(changes = {}) ⇒ Proper



198
199
200
201
202
203
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/date_val.rb', line 198

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

#empty?Boolean



215
216
217
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/date_val.rb', line 215

def empty?
  false
end

#futureProper



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

def future
  self
end

#inspectString



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/date_val.rb', line 271

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

#mapDateVal



266
267
268
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/date_val.rb', line 266

def map
  DateVal.value(yield(@value), usage, position)
end

#newest(date) ⇒ Proper



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

def newest(date)
  self
end

#oldest(date) ⇒ Proper



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

def oldest(date)
  self
end

#pastProper



261
262
263
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/date_val.rb', line 261

def past
  self
end

#proper?Boolean



219
220
221
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/date_val.rb', line 219

def proper?
  true
end

#to_dateDate



224
225
226
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/date_val.rb', line 224

def to_date
  @value
end

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



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/date_val.rb', line 229

def to_time(hour = nil, minute = nil, second = nil)
  if hour.is_a?(TimeVal) and not hour.empty?
    hour, minute, second = hour.hour, hour.minute, hour.second
  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



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/date_val.rb', line 288

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

  if truncate
    # Drop the most significant digits... they are probably bogus?
    overage = x12.length - definition.max_length
    x12.drop(overage > 0 ? overage : 0)
  else
    x12
  end
end

#too_long?Boolean



305
306
307
308
309
310
311
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/date_val.rb', line 305

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

#valid?Boolean



211
212
213
# File 'lib/stupidedi/versions/functional_groups/005010/element_types/date_val.rb', line 211

def valid?
  true
end