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

Inherits:
Stupidedi::Versions::FunctionalGroups::FiftyTen::ElementTypes::DateVal show all
Extended by:
Forwardable, 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

Returns a new instance of Proper



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

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

Instance Attribute Details

#value (readonly)

Returns the value of attribute value



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

def value
  @value
end

Instance Method Details

#coerce(other)



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

def coerce(other)
  # me, he = other.coerce(self)
  # me <OP> he
  if other.respond_to?(:to_date)
    return DateVal.value(other, usage, position), self
  else
    raise TypeError,
      "cannot coerce DateVal to #{other.class}"
  end
end

#copy(changes = {}) ⇒ Proper

Returns:



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  false
end

#futureProper

Returns self

Returns:



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

def future
  self
end

#inspectString

Returns:



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

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:



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

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

#newest(date) ⇒ Proper

Returns self

Returns:



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

def newest(date)
  self
end

#oldest(date) ⇒ Proper

Returns self

Returns:



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

def oldest(date)
  self
end

#pastProper

Returns self

Returns:



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

def past
  self
end

#proper?Boolean

Returns:

  • (Boolean)


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

def proper?
  true
end

#to_dateDate

Returns:



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

def to_date
  @value
end

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

Returns:



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

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:



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

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)


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

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)


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

def valid?
  true
end