gnucash 1.2.1 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/gnucash/value.rb +25 -10
- data/lib/gnucash/version.rb +1 -1
- data/spec/gnucash/value_spec.rb +6 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f48cb558de94a0cc405d31710c076c1b0d58de28
|
4
|
+
data.tar.gz: 5196aee0a2081291d4515c1830115d1a74cd3610
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 284f0728714930290f64939f6ca4f112394e5c4edd0b9c805c53876f59a93198c84881e7fbd74e8c70bed5dff051b014d26122ed32d53af0ec3b059d811f1032
|
7
|
+
data.tar.gz: 6e3f0ecbe2419c4ac2a774cffabd844c2f59463fd9a3af77e9ff0febad969d83a3ee931e6aeaf74f87922aab7be9f95e5a41b5cc827d1c24624ffd1f367b922b
|
data/Gemfile.lock
CHANGED
data/lib/gnucash/value.rb
CHANGED
@@ -4,9 +4,12 @@ module Gnucash
|
|
4
4
|
class Value
|
5
5
|
include Comparable
|
6
6
|
|
7
|
-
# @return [
|
7
|
+
# @return [Integer] The raw, undivided integer value.
|
8
8
|
attr_reader :val
|
9
9
|
|
10
|
+
# @return [Integer] Divisor value.
|
11
|
+
attr_reader :div
|
12
|
+
|
10
13
|
# Create a new Value object with value 0.
|
11
14
|
#
|
12
15
|
# @return [Value] Zero value.
|
@@ -16,11 +19,11 @@ module Gnucash
|
|
16
19
|
|
17
20
|
# Construct a Value object.
|
18
21
|
#
|
19
|
-
# @param val [String,
|
22
|
+
# @param val [String, Integer]
|
20
23
|
# Either a String in the form "1234/100" or an integer containing the
|
21
24
|
# raw value.
|
22
|
-
# @param div [
|
23
|
-
# The divisor value to use (when +val+ is given as a
|
25
|
+
# @param div [Integer]
|
26
|
+
# The divisor value to use (when +val+ is given as a Integer).
|
24
27
|
def initialize(val, div = 100)
|
25
28
|
if val.is_a?(String)
|
26
29
|
if val =~ /^(-?\d+)\/(\d+)$/
|
@@ -29,7 +32,7 @@ module Gnucash
|
|
29
32
|
else
|
30
33
|
raise "Unexpected value string: #{val.inspect}"
|
31
34
|
end
|
32
|
-
elsif val.is_a?(
|
35
|
+
elsif val.is_a?(Integer)
|
33
36
|
@val = val
|
34
37
|
@div = div
|
35
38
|
else
|
@@ -44,7 +47,8 @@ module Gnucash
|
|
44
47
|
# @return [Value] Result of addition.
|
45
48
|
def +(other)
|
46
49
|
if other.is_a?(Value)
|
47
|
-
|
50
|
+
lcm_div = @div.lcm(other.div)
|
51
|
+
Value.new((@val * (lcm_div / @div)) + (other.val * (lcm_div / other.div)), lcm_div)
|
48
52
|
elsif other.is_a?(Numeric)
|
49
53
|
(to_f + other).round(2)
|
50
54
|
else
|
@@ -59,7 +63,8 @@ module Gnucash
|
|
59
63
|
# @return [Value] Result of subtraction.
|
60
64
|
def -(other)
|
61
65
|
if other.is_a?(Value)
|
62
|
-
|
66
|
+
lcm_div = @div.lcm(other.div)
|
67
|
+
Value.new((@val * (lcm_div / @div)) - (other.val * (lcm_div / other.div)), lcm_div)
|
63
68
|
elsif other.is_a?(Numeric)
|
64
69
|
(to_f - other).round(2)
|
65
70
|
else
|
@@ -78,7 +83,7 @@ module Gnucash
|
|
78
83
|
#
|
79
84
|
# @param other [Numeric] Multiplier.
|
80
85
|
#
|
81
|
-
# @return [
|
86
|
+
# @return [Numeric] Result of multiplication.
|
82
87
|
def *(other)
|
83
88
|
if other.is_a?(Numeric)
|
84
89
|
(to_f * other).round(2)
|
@@ -91,7 +96,7 @@ module Gnucash
|
|
91
96
|
#
|
92
97
|
# @param other [Numeric] Divisor.
|
93
98
|
#
|
94
|
-
# @return [
|
99
|
+
# @return [Numeric] Result of division.
|
95
100
|
def /(other)
|
96
101
|
if other.is_a?(Numeric)
|
97
102
|
(to_f / other).round(2)
|
@@ -118,7 +123,17 @@ module Gnucash
|
|
118
123
|
#
|
119
124
|
# @return [Integer] Comparison result.
|
120
125
|
def <=>(other)
|
121
|
-
|
126
|
+
lcm_div = @div.lcm(other.div)
|
127
|
+
(@val * (lcm_div / @div)) <=> (other.val * (lcm_div / other.div))
|
128
|
+
end
|
129
|
+
|
130
|
+
# Test two Value objects for equality.
|
131
|
+
#
|
132
|
+
# @return [Boolean]
|
133
|
+
# Whether the two Value objects hold the same value.
|
134
|
+
def ==(other)
|
135
|
+
lcm_div = @div.lcm(other.div)
|
136
|
+
(@val * (lcm_div / @div)) == (other.val * (lcm_div / other.div))
|
122
137
|
end
|
123
138
|
end
|
124
139
|
end
|
data/lib/gnucash/version.rb
CHANGED
data/spec/gnucash/value_spec.rb
CHANGED
@@ -81,6 +81,12 @@ module Gnucash
|
|
81
81
|
expect((Value.new("1234/100") > Value.new("222/100"))).to be_truthy
|
82
82
|
end
|
83
83
|
|
84
|
+
it "converts between values with different divisors" do
|
85
|
+
expect(Value.new("1234/10000") < Value.new("100/100")).to be_truthy
|
86
|
+
expect(Value.new(7, 100) + Value.new(17, 1000)).to eq Value.new(87, 1000)
|
87
|
+
expect(Value.new(80, 100) - Value.new(5, 50)).to eq Value.new(70, 100)
|
88
|
+
end
|
89
|
+
|
84
90
|
context "errors" do
|
85
91
|
it "raises an error when attempting to add an unknown object type" do
|
86
92
|
expect { Value.new(33) + nil }.to raise_error /Unexpected argument/i
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gnucash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josh Holtrop
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-09-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|