Class: Eddy::Models::Element::N

Inherits:
Base
  • Object
show all
Defined in:
lib/eddy/models/element/n.rb

Overview

Numeric (implies the number of decimal points, e.g., N2 would be two decimal positions)

Instance Attribute Summary collapse

Attributes inherited from Base

#description, #id, #max, #min, #name, #ref, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#doc_comment, #req, #req=

Constructor Details

#initialize(min:, max:, req: nil, ref: nil, val: nil, decimals: 0) ⇒ void

Parameters:

  • min (Integer)
  • max (Integer)
  • req (String) (defaults to: nil)

    (nil)

  • ref (String) (defaults to: nil)

    (nil)

  • val (Numeric) (defaults to: nil)

    (nil)

  • decimals (Integer) (defaults to: 0)

    (0)



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/eddy/models/element/n.rb', line 18

def initialize(
  min:,
  max:,
  req: nil,
  ref: nil,
  val: nil,
  decimals: 0
)
  @min = min
  @max = max
  self.req = req
  self.ref = ref
  self.decimals = decimals
  self.value = val
end

Instance Attribute Details

#decimalsInteger

Implied number of decimal points.

Returns:

  • (Integer)


9
10
11
# File 'lib/eddy/models/element/n.rb', line 9

def decimals
  @decimals
end

Class Method Details

.process_value(val, decimals, min: 1, max: nil) ⇒ String

Convert a Float or Integer value to a valid EDI string representation. Stas Spiridonov is a wizard.

Parameters:

  • val (Numeric)

    Original value.

  • decimals (Integer)

    Implied number of decimal points.

  • min (Integer) (defaults to: 1)

    (1) Minimum length for a valid value.

  • max (Integer) (defaults to: nil)

    (nil) Maximum length for a valid value.

Returns:

  • (String)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/eddy/models/element/n.rb', line 74

def self.process_value(val, decimals, min: 1, max: nil)
  case val
  when Integer
    if (val - val.floor) == 0
      return sprintf("%0#{min}d", val.truncate)
    else
      return sprintf("%0#{min}d", val.round(2))
    end
  when Float
    return (val * (10.0**decimals)).round.to_s
  else
    raise ArgumentError, "'val' must be a Float or an Integer."
  end
end

Instance Method Details

#process_valueString

Returns:

  • (String)


57
58
59
60
61
62
63
64
# File 'lib/eddy/models/element/n.rb', line 57

def process_value()
  return self.class.process_value(
    @val,
    self.decimals,
    min: self.min,
    max: self.max,
  )
end

#valueString

Returns:

  • (String)

Raises:



52
53
54
# File 'lib/eddy/models/element/n.rb', line 52

def value()
  return super()
end

#value=(arg) ⇒ void

This method returns an undefined value.

Parameters:

  • arg (Numeric)

Raises:



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/eddy/models/element/n.rb', line 37

def value=(arg)
  if arg == :skip
    @val = :skip
    return
  end
  if arg.nil?()
    @val = nil
    return
  end
  raise Eddy::Errors::TypeValidationError.new(element: self, arg: arg) unless arg.is_a?(Numeric)
  @val = arg
end