Class: Stupidedi::Versions::FunctionalGroups::FortyTen::ElementTypes::FixnumVal::NonEmpty

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

unary_operators

Methods included from Operators::Relational

relational_operators

Methods included from Operators::Binary

binary_operators

Methods inherited from Stupidedi::Versions::FunctionalGroups::FortyTen::ElementTypes::FixnumVal

empty, #numeric?, #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) ⇒ NonEmpty

Returns a new instance of NonEmpty.


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

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

Instance Attribute Details

#valueBigDecimal (readonly)

Returns:

  • (BigDecimal)

197
198
199
# File 'lib/stupidedi/versions/functional_groups/004010/element_types/fixnum_val.rb', line 197

def value
  @value
end

Instance Method Details

#coerce(other)


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

def coerce(other)
  # self', other' = other.coerce(self)
  # self' * other'
  return copy(:value => other.to_d), self
end

#copy(changes = {}) ⇒ NonEmpty

Returns:


208
209
210
211
212
213
# File 'lib/stupidedi/versions/functional_groups/004010/element_types/fixnum_val.rb', line 208

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

#empty?Boolean

Returns:

  • (Boolean)

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

def empty?
  false
end

#inspectString

Returns:

  • (String)

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

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}") << "(#{to_s})"
end

#mapFixnumVal

Returns:


275
276
277
# File 'lib/stupidedi/versions/functional_groups/004010/element_types/fixnum_val.rb', line 275

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

#to_sString

Returns:

  • (String)

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

def to_s  # The number of fractional digits is implied by usage.precision

  (@value * (10 ** definition.precision)).to_i.to_s
end

#to_x12(truncate = true) ⇒ String

Returns:

  • (String)

253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/stupidedi/versions/functional_groups/004010/element_types/fixnum_val.rb', line 253

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)

267
268
269
270
271
272
# File 'lib/stupidedi/versions/functional_groups/004010/element_types/fixnum_val.rb', line 267

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

#valid?Boolean

Returns:

  • (Boolean)

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

def valid?
  true
end