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

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

Returns a new instance of Proper.


194
195
196
197
# File 'lib/stupidedi/versions/functional_groups/004010/element_types/date_val.rb', line 194

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

Instance Attribute Details

#value (readonly)

Returns the value of attribute value


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

def value
  @value
end

Instance Method Details

#coerce(other)


207
208
209
210
211
# File 'lib/stupidedi/versions/functional_groups/004010/element_types/date_val.rb', line 207

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

#copy(changes = {}) ⇒ Proper

Returns:


200
201
202
203
204
205
# File 'lib/stupidedi/versions/functional_groups/004010/element_types/date_val.rb', line 200

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

#empty?Boolean

Returns:

  • (Boolean)

217
218
219
# File 'lib/stupidedi/versions/functional_groups/004010/element_types/date_val.rb', line 217

def empty?
  false
end

#futureProper

Returns self.

Returns:


258
259
260
# File 'lib/stupidedi/versions/functional_groups/004010/element_types/date_val.rb', line 258

def future
  self
end

#inspectString

Returns:

  • (String)

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

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

Returns:


268
269
270
# File 'lib/stupidedi/versions/functional_groups/004010/element_types/date_val.rb', line 268

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

#newest(date) ⇒ Proper

Returns self.

Returns:


253
254
255
# File 'lib/stupidedi/versions/functional_groups/004010/element_types/date_val.rb', line 253

def newest(date)
  self
end

#oldest(date) ⇒ Proper

Returns self.

Returns:


248
249
250
# File 'lib/stupidedi/versions/functional_groups/004010/element_types/date_val.rb', line 248

def oldest(date)
  self
end

#pastProper

Returns self.

Returns:


263
264
265
# File 'lib/stupidedi/versions/functional_groups/004010/element_types/date_val.rb', line 263

def past
  self
end

#proper?Boolean

Returns:

  • (Boolean)

221
222
223
# File 'lib/stupidedi/versions/functional_groups/004010/element_types/date_val.rb', line 221

def proper?
  true
end

#to_dateDate

Returns:

  • (Date)

226
227
228
# File 'lib/stupidedi/versions/functional_groups/004010/element_types/date_val.rb', line 226

def to_date
  @value
end

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

Returns:

  • (Time)

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

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

Returns:

  • (String)

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

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

Returns:

  • (Boolean)

307
308
309
310
311
312
313
# File 'lib/stupidedi/versions/functional_groups/004010/element_types/date_val.rb', line 307

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

Returns:

  • (Boolean)

213
214
215
# File 'lib/stupidedi/versions/functional_groups/004010/element_types/date_val.rb', line 213

def valid?
  true
end