Class: Stupidedi::Versions::Common::ElementTypes::FixnumVal::NonEmpty

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

Overview

Non-empty numeric value. Shouldn't be directly instantiated -- instead, use the value constructors.

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::Binary

binary_operators

Methods inherited from Valid

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

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

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.


214
215
216
217
# File 'lib/stupidedi/versions/common/element_types/nn.rb', line 214

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

Instance Attribute Details

#valueBigDecimal (readonly)

Returns:

  • (BigDecimal)

210
211
212
# File 'lib/stupidedi/versions/common/element_types/nn.rb', line 210

def value
  @value
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)

219
220
221
# File 'lib/stupidedi/versions/common/element_types/nn.rb', line 219

def empty?
  false
end

#inspectString

:nocov:

Returns:

  • (String)

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

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("Nn.value#{id}") + "(#{@value.to_s("F").gsub(/\.0+$/, "")})"
end

#to_sString

Returns:

  • (String)

250
251
252
253
# File 'lib/stupidedi/versions/common/element_types/nn.rb', line 250

def to_s
  # The number of fractional digits is implied by usage.precision
  @value.to_s("F").gsub(/\.0+$/, "")
end

#to_x12(truncate = true) ⇒ String

Returns:

  • (String)

256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/stupidedi/versions/common/element_types/nn.rb', line 256

def to_x12(truncate = true)
  nn   = (@value * (10 ** definition.precision)).to_i
  sign = (nn < 0) ? "-" : ""

  # Leading zeros must be suppressed unless necessary to satisfy a
  # minimum length requirement
  if truncate
    sign = sign + nn.abs.to_s.
      take(definition.max_length).
      rjust(definition.min_length, "0")
  else
    sign = sign + nn.abs.to_s.
      rjust(definition.min_length, "0")
  end
end

#too_long?Boolean

Returns:

  • (Boolean)

223
224
225
226
227
228
# File 'lib/stupidedi/versions/common/element_types/nn.rb', line 223

def too_long?
  nn = (@value * (10 ** definition.precision)).to_i

  # The length of a numeric type does not include an optional sign
  definition.max_length < nn.abs.to_s.length
end