Class: Eddy::Models::Element::N
- Defined in:
- lib/eddy/models/element/n.rb
Overview
Numeric (implies the number of decimal points, e.g., N2 would be two decimal positions)
Direct Known Subclasses
Elements::E28, Elements::E329, Elements::E351, Elements::E354, Elements::E386, Elements::E610, Elements::E784, Elements::E80, Elements::E96, Elements::E97, Elements::I12, Elements::I16
Instance Attribute Summary collapse
-
#decimals ⇒ Integer
Implied number of decimal points.
Attributes inherited from Base
#description, #id, #max, #min, #name, #ref, #type
Class Method Summary collapse
-
.process_value(val, decimals, min: 1, max: nil) ⇒ String
Convert a Float or Integer value to a valid EDI string representation.
Instance Method Summary collapse
- #initialize(min:, max:, req: nil, ref: nil, val: nil, decimals: 0) ⇒ void constructor
- #process_value ⇒ String
- #value ⇒ String
- #value=(arg) ⇒ void
Methods inherited from Base
Constructor Details
#initialize(min:, max:, req: nil, ref: nil, val: nil, decimals: 0) ⇒ void
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
#decimals ⇒ Integer
Implied number of decimal points.
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.
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_value ⇒ 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 |
#value ⇒ String
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.
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 |