gnucash 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f91912ae36d38bc2c602c735564159fd14114e43
4
- data.tar.gz: c95bc53e838d5595872d3edcb16945ba84e9d0d6
3
+ metadata.gz: f48cb558de94a0cc405d31710c076c1b0d58de28
4
+ data.tar.gz: 5196aee0a2081291d4515c1830115d1a74cd3610
5
5
  SHA512:
6
- metadata.gz: 129f20266029b1d5e2529d3c23084b0d75f13988b87dceb8aad9b58e236ef5001a3014a857d584716aa1914543ee96d7cf449fc51226e896fd713788afe56a49
7
- data.tar.gz: 479734374bb70a35026880996ef489923d18544c8b85fe82c2c8bdd4dbb1dba8590c6a60f72cc0edbf44b678ca3eb7593572f4a0303445901a5784314108a442
6
+ metadata.gz: 284f0728714930290f64939f6ca4f112394e5c4edd0b9c805c53876f59a93198c84881e7fbd74e8c70bed5dff051b014d26122ed32d53af0ec3b059d811f1032
7
+ data.tar.gz: 6e3f0ecbe2419c4ac2a774cffabd844c2f59463fd9a3af77e9ff0febad969d83a3ee931e6aeaf74f87922aab7be9f95e5a41b5cc827d1c24624ffd1f367b922b
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- gnucash (1.2.1)
4
+ gnucash (1.2.2)
5
5
  nokogiri
6
6
 
7
7
  GEM
@@ -4,9 +4,12 @@ module Gnucash
4
4
  class Value
5
5
  include Comparable
6
6
 
7
- # @return [Fixnum] The raw, undivided integer value.
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, Fixnum]
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 [Fixnum]
23
- # The divisor value to use (when +val+ is given as a Fixnum).
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?(Fixnum)
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
- Value.new(@val + other.val)
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
- Value.new(@val - other.val)
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 [Value] Result of multiplication.
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 [Value] Result of division.
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
- @val <=> other.val
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
@@ -1,4 +1,4 @@
1
1
  module Gnucash
2
2
  # gem version
3
- VERSION = "1.2.1"
3
+ VERSION = "1.2.2"
4
4
  end
@@ -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.1
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-08-25 00:00:00.000000000 Z
11
+ date: 2017-09-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri