Class: Stupidedi::Versions::Common::ElementTypes::FloatVal::NonEmpty

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

Instance Attribute Summary collapse

Attributes inherited from Stupidedi::Values::SimpleElementVal

#position, #usage

Instance Method Summary collapse

Methods included from Operators::Binary

binary_operators

Methods included from Operators::Unary

unary_operators

Methods included from Operators::Relational

relational_operators

Methods inherited from Valid

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

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

empty, #numeric?, #too_short?, value

Methods inherited from Stupidedi::Values::SimpleElementVal

#allowed?, #component?, #copy, #date?, #id?, #leaf?, #numeric?, #simple?, #string?, #time?, #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) ⇒ NonEmpty

Returns a new instance of NonEmpty.



207
208
209
210
# File 'lib/stupidedi/versions/common/element_types/r.rb', line 207

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

Instance Attribute Details

#valueBigDecimal (readonly)

Returns:

  • (BigDecimal)


203
204
205
# File 'lib/stupidedi/versions/common/element_types/r.rb', line 203

def value
  @value
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  false
end

#inspectString

:nocov:

Returns:

  • (String)


218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/stupidedi/versions/common/element_types/r.rb', line 218

def inspect
  id = definition.try 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(" R.value#{id}") + "(#{to_s})"
end

#to_sString

Returns:

  • (String)


236
237
238
239
240
241
242
# File 'lib/stupidedi/versions/common/element_types/r.rb', line 236

def to_s
  if definition.max_precision.present?
    @value.round(definition.max_precision).to_s("F")
  else
    @value.to_s("F")
  end
end

#to_x12(truncate = true) ⇒ String

While the ASC X12 standard supports the usage of exponential notation, the HIPAA guides prohibit it. In the interest of simplicity, this method will not output exponential notation, as there is currently no configuration attribute to indicate if this is allowed or not -- if this is required in the future, the best place for it to fit would be in SimpleElementUse

Returns:

  • (String)


252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/stupidedi/versions/common/element_types/r.rb', line 252

def to_x12(truncate = true)
  remaining =
    if @value.to_i.zero?
      definition.max_length
    else
      definition.max_length - @value.to_i.abs.to_s.length
    end

  # The integer part consumes all the space, so there is no room
  # for a fractional amount
  if remaining <= 0 and truncate
    int   = @value.to_i
    sign  = (int < 0) ? "-" : ""
    return sign + int.abs.to_s.take(definition.max_length)
  end

  # Don't exceed the definition's max_precision
  precision =
    if definition.max_precision.present?
      (definition.max_precision < remaining) ?
        definition.max_precision : remaining
    else
      remaining
    end

  rounded = @value.round(precision)
  sign    = (rounded < 0) ? "-" : ""

  # Leading zeros preceeding the decimal point and trailing zeros
  # following the decimal point must be supressed unless necessary
  # to satisfy a minimum length requirement or to indicate
  # precision, respectively.
  if rounded.zero?
    "0" * definition.min_length
  else
    base = rounded.abs
    base = base.is_a?(BigDecimal) ? base : BigDecimal(base) 
    sign + base.to_s("F").
      gsub(/^0+/, ""). # leading zeros
      gsub(/0+$/, ""). # trailing zeros
      gsub(/\.$/, ""). # trailing decimal point
      rjust(definition.min_length, "0")
  end
end

#too_long?Boolean

Returns:

  • (Boolean)


297
298
299
300
301
302
303
304
# File 'lib/stupidedi/versions/common/element_types/r.rb', line 297

def too_long?
  # We can truncate the fractional portion as much as needed, so
  # the only concern we have about length is regarding the digits
  # to the left of the decimal place.

  # The length of a decimal type does not include an optional sign
  definition.max_length < @value.to_i.abs.to_s.length
end