Class: Gnucash::Value
- Inherits:
-
Object
- Object
- Gnucash::Value
- 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
-
#div ⇒ Integer
readonly
Divisor value.
-
#val ⇒ Integer
readonly
The raw, undivided integer value.
Class Method Summary collapse
-
.zero ⇒ Value
Create a new Value object with value 0.
Instance Method Summary collapse
-
#*(other) ⇒ Numeric
Multiply a Value object.
-
#+(other) ⇒ Value
Add to a Value object.
-
#-(other) ⇒ Value
Subtract from a Value object.
-
#-@ ⇒ Value
Negate a Value.
-
#/(other) ⇒ Numeric
Divide a Value object.
-
#<=>(other) ⇒ Integer
Compare two Value objects.
-
#==(other) ⇒ Boolean
Test two Value objects for equality.
-
#attributes ⇒ Array<Symbol>
Attributes available for inspection.
-
#initialize(val, div = 100) ⇒ Value
constructor
Construct a Value object.
-
#to_f ⇒ Float
Convert the Value to a Float.
-
#to_r ⇒ Rational
Convert the Value to a Rational.
-
#to_s ⇒ String
Represent the Value as a string (two decimal places).
Methods included from Support::LightInspect
Constructor Details
#initialize(val, div = 100) ⇒ Value
Construct a Value object.
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
#div ⇒ Integer (readonly)
Returns Divisor value.
12 13 14 |
# File 'lib/gnucash/value.rb', line 12 def div @div end |
#val ⇒ Integer (readonly)
Returns The raw, undivided integer value.
9 10 11 |
# File 'lib/gnucash/value.rb', line 9 def val @val end |
Class Method Details
Instance Method Details
#*(other) ⇒ Numeric
Multiply a Value object.
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.
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.
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.
79 80 81 |
# File 'lib/gnucash/value.rb', line 79 def -@ Value.new(-@val, @div) end |
#/(other) ⇒ Numeric
Divide a Value object.
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.
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.
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 |
#attributes ⇒ Array<Symbol>
Attributes available for inspection
159 160 161 |
# File 'lib/gnucash/value.rb', line 159 def attributes %i[val div] end |
#to_f ⇒ Float
Convert the Value to a Float.
125 126 127 |
# File 'lib/gnucash/value.rb', line 125 def to_f @val / @div.to_f end |
#to_r ⇒ Rational
Convert the Value to a Rational.
134 135 136 |
# File 'lib/gnucash/value.rb', line 134 def to_r Rational(@val, @div) end |
#to_s ⇒ String
Represent the Value as a string (two decimal places).
118 119 120 |
# File 'lib/gnucash/value.rb', line 118 def to_s sprintf("%.02f", to_f) end |