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

Returns a new instance of Proper



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

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

Instance Attribute Details

#value (readonly)

Returns the value of attribute value



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

def value
  @value
end

Instance Method Details

#coerce(other)



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

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

#copy(changes = {}) ⇒ Proper

Returns:



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  false
end

#futureProper

Returns self

Returns:



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

def future
  self
end

#inspectString

Returns:

  • (String)


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

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:



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

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

#newest(date) ⇒ Proper

Returns self

Returns:



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

def newest(date)
  self
end

#oldest(date) ⇒ Proper

Returns self

Returns:



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

def oldest(date)
  self
end

#pastProper

Returns self

Returns:



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

def past
  self
end

#proper?Boolean

Returns:

  • (Boolean)


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

def proper?
  true
end

#to_dateDate

Returns:

  • (Date)


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

def to_date
  @value
end

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

Returns:

  • (Time)


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

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)


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

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)


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

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)


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

def valid?
  true
end