Class: Gnucash::Value

Inherits:
Object
  • Object
show all
Includes:
Comparable, Support::LightInspect
Defined in:
lib/gnucash/value.rb

Overview

Represent a currency value as an integer so that integer math can be used for accuracy in computations.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support::LightInspect

#inspect

Constructor Details

#initialize(val, div = 100) ⇒ Value

Construct a Value object.

Parameters:

  • val (String, Integer)

    Either a String in the form “1234/100” or an integer containing the raw value.

  • div (Integer) (defaults to: 100)

    The divisor value to use (when val is given as a Integer).



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gnucash/value.rb', line 28

def initialize(val, div = 100)
  if val.is_a?(String)
    if val =~ /^(-?\d+)\/(\d+)$/
      @val = $1.to_i
      @div = $2.to_i
    else
      raise "Unexpected value string: #{val.inspect}"
    end
  elsif val.is_a?(Integer)
    @val = val
    @div = div
  else
    raise "Unexpected value type: #{val.class}"
  end
end

Instance Attribute Details

#divInteger (readonly)

Returns Divisor value.

Returns:

  • (Integer)

    Divisor value.



12
13
14
# File 'lib/gnucash/value.rb', line 12

def div
  @div
end

#valInteger (readonly)

Returns The raw, undivided integer value.

Returns:

  • (Integer)

    The raw, undivided integer value.



9
10
11
# File 'lib/gnucash/value.rb', line 9

def val
  @val
end

Class Method Details

.zeroValue

Create a new Value object with value 0.

Returns:

  • (Value)

    Zero value.



17
18
19
# File 'lib/gnucash/value.rb', line 17

def self.zero
  Value.new(0)
end

Instance Method Details

#*(other) ⇒ Numeric

Multiply a Value object.

Parameters:

  • other (Numeric, Value)

    Multiplier.

Returns:

  • (Numeric)

    Result of multiplication.



88
89
90
91
92
93
94
95
96
97
# File 'lib/gnucash/value.rb', line 88

def *(other)
  if other.is_a?(Value)
    other = other.to_f
  end
  if other.is_a?(Numeric)
    to_f * other
  else
    raise "Unexpected argument (#{other.inspect})"
  end
end

#+(other) ⇒ Value

Add to a Value object.

Parameters:

Returns:

  • (Value)

    Result of addition.



49
50
51
52
53
54
55
56
57
58
# File 'lib/gnucash/value.rb', line 49

def +(other)
  if other.is_a?(Value)
    lcm_div = @div.lcm(other.div)
    Value.new((@val * (lcm_div / @div)) + (other.val * (lcm_div / other.div)), lcm_div)
  elsif other.is_a?(Numeric)
    to_f + other
  else
    raise "Unexpected argument"
  end
end

#-(other) ⇒ Value

Subtract from a Value object.

Parameters:

Returns:

  • (Value)

    Result of subtraction.



65
66
67
68
69
70
71
72
73
74
# File 'lib/gnucash/value.rb', line 65

def -(other)
  if other.is_a?(Value)
    lcm_div = @div.lcm(other.div)
    Value.new((@val * (lcm_div / @div)) - (other.val * (lcm_div / other.div)), lcm_div)
  elsif other.is_a?(Numeric)
    to_f - other
  else
    raise "Unexpected argument"
  end
end

#-@Value

Negate a Value.

Returns:

  • (Value)

    Result of negation.



79
80
81
# File 'lib/gnucash/value.rb', line 79

def -@
  Value.new(-@val, @div)
end

#/(other) ⇒ Numeric

Divide a Value object.

Parameters:

  • other (Numeric, Value)

    Divisor.

Returns:

  • (Numeric)

    Result of division.



104
105
106
107
108
109
110
111
112
113
# File 'lib/gnucash/value.rb', line 104

def /(other)
  if other.is_a?(Value)
    other = other.to_f
  end
  if other.is_a?(Numeric)
    to_f / other
  else
    raise "Unexpected argument (#{other.inspect})"
  end
end

#<=>(other) ⇒ Integer

Compare two Value objects.

Returns:

  • (Integer)

    Comparison result.



141
142
143
144
# File 'lib/gnucash/value.rb', line 141

def <=>(other)
  lcm_div = @div.lcm(other.div)
  (@val * (lcm_div / @div)) <=> (other.val * (lcm_div / other.div))
end

#==(other) ⇒ Boolean

Test two Value objects for equality.

Returns:

  • (Boolean)

    Whether the two Value objects hold the same value.



150
151
152
153
# File 'lib/gnucash/value.rb', line 150

def ==(other)
  lcm_div = @div.lcm(other.div)
  (@val * (lcm_div / @div)) == (other.val * (lcm_div / other.div))
end

#attributesArray<Symbol>

Attributes available for inspection

Returns:

  • (Array<Symbol>)

    Attributes used to build the inspection string

See Also:



159
160
161
# File 'lib/gnucash/value.rb', line 159

def attributes
  %i[val div]
end

#to_fFloat

Convert the Value to a Float.

Returns:

  • (Float)

    Value of the value as a Float.



125
126
127
# File 'lib/gnucash/value.rb', line 125

def to_f
  @val / @div.to_f
end

#to_rRational

Convert the Value to a Rational.

Returns:

  • (Rational)

    Value of the value as a Rational.

Since:

  • 1.4.0



134
135
136
# File 'lib/gnucash/value.rb', line 134

def to_r
  Rational(@val, @div)
end

#to_sString

Represent the Value as a string (two decimal places).

Returns:

  • (String)

    Representation of value.



118
119
120
# File 'lib/gnucash/value.rb', line 118

def to_s
  sprintf("%.02f", to_f)
end