Class: Nacha::Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/nacha/numeric.rb

Instance Method Summary collapse

Constructor Details

#initialize(val = nil) ⇒ Numeric

Returns a new instance of Numeric.



4
5
6
# File 'lib/nacha/numeric.rb', line 4

def initialize(val = nil)
  self.value = val
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

from the input data. If an operation is attempted, then @op_value should be checked to see if the operation is valid for it, not necessarily the potentially string @value



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/nacha/numeric.rb', line 48

def method_missing(method_name, *args, &block)
  if @op_value.respond_to?(method_name)
    old_op_value = @op_value.dup
    # rubocop:disable GitlabSecurity/PublicSend
    if method_name.to_s.end_with?('!')
      @op_value.send(method_name, *args, &block)
      return_value = @op_value
    else
      return_value = @op_value.send(method_name, *args, &block)
    end
    # rubocop:enable GitlabSecurity/PublicSend

    if old_op_value != return_value
      @value = return_value
      @op_value = return_value
    end

    @value
  else
    super
  end
end

Instance Method Details

#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/nacha/numeric.rb', line 40

def respond_to_missing?(method_name, _include_private = false)
  @op_value.respond_to?(method_name)
end

#to_iObject



8
9
10
11
12
13
14
15
16
# File 'lib/nacha/numeric.rb', line 8

def to_i
  return 0 if @value.nil?

  if @value.is_a?(String) && @value.match(/\A *\z/)
    @value # blank strings should return as blank
  else
    @value.to_i
  end
end

#to_sObject



36
37
38
# File 'lib/nacha/numeric.rb', line 36

def to_s
  @value ? @value.to_i.to_s : nil
end

#value=(val) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/nacha/numeric.rb', line 18

def value=(val)
  if val.is_a?(String)
    @value = val.dup
    if !val.strip.empty?
      @op_value = BigDecimal(val.strip)
      @value = val.dup
    else
      @op_value = BigDecimal('0')
    end
  elsif val.nil?
    @value = BigDecimal('0')
    @op_value = @value
  else
    @value = BigDecimal(val)
    @op_value = @value
  end
end