gnucash 1.3.1 → 1.4.0

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: 900fd81bad3ba0b11854c9337bc222b535804b02
4
- data.tar.gz: 5549a7746596aae5be8f4081a1b6ba322b2f98b0
3
+ metadata.gz: 52713dfe700ed99ecbde5b43d7c0614e86d3ec34
4
+ data.tar.gz: d59a7c2b7069bc1c355201e4c4e9f4ec832d1d55
5
5
  SHA512:
6
- metadata.gz: e5bb85d5c99510c17af02d0b649f637a4c939eb90e2392bddeadc22926699c96a28d11fb7cba43a7114d29e4d38fb413bbfb082611ca2c70f6f9e6fb13f9bd4b
7
- data.tar.gz: f0f3934018eb94ae3a989abdbb95c94aaeb3dd8f0a399e3fa85e8f26937184577d55f775b8a38a51334f845942f62ca32225041cbc651139e3db70d252c1b139
6
+ metadata.gz: ed54f96c4c23bb1960895f8c3df8918faed0b83362e73b5682251501fd8b664b3458797fe0a9abd45d5fb378b81f1dc508c825ff83c2e3b9a1a376c5c1a94e0c
7
+ data.tar.gz: a92fc8a0fcb6eff1a5e851f9e512fd8c7714aaec0d69053055a879a7548e1036a283ba7cde903c34f52d615dbd2e80076ceb3a532fc840541f293a9823bea4a2
@@ -1,5 +1,19 @@
1
1
  # ChangeLog
2
2
 
3
+ ## v1.4.0
4
+
5
+ - add Account#parent_id and #parent
6
+ - add Value#to_r to convert to Rational
7
+ - override #inspect for several Gnucash objects to improve REPL usage
8
+
9
+ ## v1.3.1
10
+
11
+ - do not round numeric results of Value computations
12
+
13
+ ## v1.3.0
14
+
15
+ - allow Value objects to be passed to / and * operators
16
+
3
17
  ## v1.2.2
4
18
 
5
19
  - Allow mixing Value objects with different divisors - fix #7
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- gnucash (1.3.1)
4
+ gnucash (1.4.0)
5
5
  nokogiri
6
6
 
7
7
  GEM
@@ -13,27 +13,27 @@ GEM
13
13
  mini_portile2 (2.3.0)
14
14
  nokogiri (1.8.1)
15
15
  mini_portile2 (~> 2.3.0)
16
- rake (12.0.0)
17
- rdoc (5.1.0)
18
- rspec (3.6.0)
19
- rspec-core (~> 3.6.0)
20
- rspec-expectations (~> 3.6.0)
21
- rspec-mocks (~> 3.6.0)
22
- rspec-core (3.6.0)
23
- rspec-support (~> 3.6.0)
24
- rspec-expectations (3.6.0)
16
+ rake (12.3.0)
17
+ rdoc (6.0.1)
18
+ rspec (3.7.0)
19
+ rspec-core (~> 3.7.0)
20
+ rspec-expectations (~> 3.7.0)
21
+ rspec-mocks (~> 3.7.0)
22
+ rspec-core (3.7.1)
23
+ rspec-support (~> 3.7.0)
24
+ rspec-expectations (3.7.0)
25
25
  diff-lcs (>= 1.2.0, < 2.0)
26
- rspec-support (~> 3.6.0)
27
- rspec-mocks (3.6.0)
26
+ rspec-support (~> 3.7.0)
27
+ rspec-mocks (3.7.0)
28
28
  diff-lcs (>= 1.2.0, < 2.0)
29
- rspec-support (~> 3.6.0)
30
- rspec-support (3.6.0)
31
- simplecov (0.15.0)
29
+ rspec-support (~> 3.7.0)
30
+ rspec-support (3.7.0)
31
+ simplecov (0.15.1)
32
32
  docile (~> 1.1.0)
33
33
  json (>= 1.8, < 3)
34
34
  simplecov-html (~> 0.10.0)
35
35
  simplecov-html (0.10.2)
36
- yard (0.9.9)
36
+ yard (0.9.12)
37
37
 
38
38
  PLATFORMS
39
39
  ruby
@@ -47,4 +47,4 @@ DEPENDENCIES
47
47
  yard
48
48
 
49
49
  BUNDLED WITH
50
- 1.15.0
50
+ 1.16.1
@@ -18,6 +18,8 @@ Gem::Specification.new do |gem|
18
18
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
19
  gem.require_paths = ["lib"]
20
20
 
21
+ gem.required_ruby_version = ">= 2.0"
22
+
21
23
  gem.add_dependency "nokogiri"
22
24
 
23
25
  gem.add_development_dependency "simplecov"
@@ -1,3 +1,4 @@
1
+ require_relative "gnucash/support"
1
2
  require_relative "gnucash/account"
2
3
  require_relative "gnucash/account_transaction"
3
4
  require_relative "gnucash/book"
@@ -1,6 +1,8 @@
1
1
  module Gnucash
2
2
  # Represent a GnuCash account object.
3
3
  class Account
4
+ include Support::LightInspect
5
+
4
6
  # @return [String] The name of the account (unqualified).
5
7
  attr_reader :name
6
8
 
@@ -20,6 +22,11 @@ module Gnucash
20
22
  # @return [Boolean] Whether the account is a placeholder or not.
21
23
  attr_reader :placeholder
22
24
 
25
+ # @since 1.4.0
26
+ #
27
+ # @return [String, nil] The GUID of the parent account, if any.
28
+ attr_reader :parent_id
29
+
23
30
  # Create an Account object.
24
31
  #
25
32
  # @param book [Book] The {Gnucash::Book} containing the account.
@@ -38,7 +45,7 @@ module Gnucash
38
45
  @placeholder = node.xpath("act:slots/slot").find do |slot|
39
46
  (slot.xpath("slot:key").first.text == "placeholder" and
40
47
  slot.xpath("slot:value").first.text == "true")
41
- end
48
+ end ? true : false
42
49
  end
43
50
 
44
51
  # Return the fully qualified account name.
@@ -48,6 +55,15 @@ module Gnucash
48
55
  @full_name ||= calculate_full_name
49
56
  end
50
57
 
58
+ # Lookup for the parent account in the book.
59
+ #
60
+ # @since 1.4.0
61
+ #
62
+ # @return [Account, nil] Account object, or nil if not found.
63
+ def parent
64
+ @parent ||= @parent_id ? @book.find_account_by_id(@parent_id) : nil
65
+ end
66
+
51
67
  # Internal method used to associate a transaction with the account.
52
68
  #
53
69
  # @return [void]
@@ -105,6 +121,14 @@ module Gnucash
105
121
  @balances[idx][:value]
106
122
  end
107
123
 
124
+ # Attributes available for inspection
125
+ #
126
+ # @return [Array<Symbol>] Attributes used to build the inspection string
127
+ # @see Gnucash::Support::LightInspect
128
+ def attributes
129
+ %i[id name description type placeholder parent_id]
130
+ end
131
+
108
132
  private
109
133
 
110
134
  # @return [String] Fully qualified account name.
@@ -1,6 +1,8 @@
1
1
  module Gnucash
2
2
  # Class to link a transaction object to an Account.
3
3
  class AccountTransaction
4
+ include Support::LightInspect
5
+
4
6
  # @return [Value] The transaction value for the linked account.
5
7
  attr_reader :value
6
8
 
@@ -21,5 +23,13 @@ module Gnucash
21
23
  def method_missing(*args)
22
24
  @real_txn.send(*args)
23
25
  end
26
+
27
+ # Attributes available for inspection
28
+ #
29
+ # @return [Array<Symbol>] Attributes used to build the inspection string
30
+ # @see Gnucash::Support::LightInspect
31
+ def attributes
32
+ %i[value]
33
+ end
24
34
  end
25
35
  end
@@ -4,6 +4,8 @@ require "nokogiri"
4
4
  module Gnucash
5
5
  # Represent a GnuCash Book.
6
6
  class Book
7
+ include Support::LightInspect
8
+
7
9
  # @return [Array<Account>] Accounts in the book.
8
10
  attr_reader :accounts
9
11
 
@@ -59,6 +61,14 @@ module Gnucash
59
61
  @accounts.find { |a| a.full_name == full_name }
60
62
  end
61
63
 
64
+ # Attributes available for inspection
65
+ #
66
+ # @return [Array<Symbol>] Attributes used to build the inspection string
67
+ # @see Gnucash::Support::LightInspect
68
+ def attributes
69
+ %i[start_date end_date]
70
+ end
71
+
62
72
  private
63
73
 
64
74
  # @return [void]
@@ -0,0 +1,8 @@
1
+ require_relative "support/light_inspect"
2
+
3
+ module Gnucash
4
+ # Support functionality.
5
+ module Support
6
+ include LightInspect
7
+ end
8
+ end
@@ -0,0 +1,23 @@
1
+ module Gnucash
2
+ module Support
3
+ # Allows lightweight inspection os Gnucash models by avoiding fetch XML nodes.
4
+ module LightInspect
5
+
6
+ # Attributes available for inspection
7
+ #
8
+ # @return [Array<Symbol>] Attributes used to build the inspection string
9
+ def attributes
10
+ []
11
+ end
12
+
13
+ # Custom implementation using only selected attributes.
14
+ #
15
+ # @return [String] Stringified version of the object
16
+ # @see #attributes
17
+ def inspect
18
+ inspection = attributes.map { |att| "#{att}: #{send(att)}" }.join(", ")
19
+ "#<#{self.class} #{inspection}>"
20
+ end
21
+ end
22
+ end
23
+ end
@@ -7,6 +7,8 @@ module Gnucash
7
7
  # Splits are created as AccountTransaction objects which are associated
8
8
  # with an individual account.
9
9
  class Transaction
10
+ include Support::LightInspect
11
+
10
12
  # @return [Date] The date of the transaction.
11
13
  attr_reader :date
12
14
 
@@ -46,5 +48,13 @@ module Gnucash
46
48
  }
47
49
  end
48
50
  end
51
+
52
+ # Attributes available for inspection
53
+ #
54
+ # @return [Array<Symbol>] Attributes used to build the inspection string
55
+ # @see Gnucash::Support::LightInspect
56
+ def attributes
57
+ %i[id date description splits]
58
+ end
49
59
  end
50
60
  end
@@ -3,6 +3,7 @@ module Gnucash
3
3
  # for accuracy in computations.
4
4
  class Value
5
5
  include Comparable
6
+ include Support::LightInspect
6
7
 
7
8
  # @return [Integer] The raw, undivided integer value.
8
9
  attr_reader :val
@@ -125,6 +126,15 @@ module Gnucash
125
126
  @val / @div.to_f
126
127
  end
127
128
 
129
+ # Convert the Value to a Rational.
130
+ #
131
+ # @since 1.4.0
132
+ #
133
+ # @return [Rational] Value of the value as a Rational.
134
+ def to_r
135
+ Rational(@val, @div)
136
+ end
137
+
128
138
  # Compare two Value objects.
129
139
  #
130
140
  # @return [Integer] Comparison result.
@@ -141,5 +151,13 @@ module Gnucash
141
151
  lcm_div = @div.lcm(other.div)
142
152
  (@val * (lcm_div / @div)) == (other.val * (lcm_div / other.div))
143
153
  end
154
+
155
+ # Attributes available for inspection
156
+ #
157
+ # @return [Array<Symbol>] Attributes used to build the inspection string
158
+ # @see Gnucash::Support::LightInspect
159
+ def attributes
160
+ %i[val div]
161
+ end
144
162
  end
145
163
  end
@@ -1,4 +1,4 @@
1
1
  module Gnucash
2
2
  # gem version
3
- VERSION = "1.3.1"
3
+ VERSION = "1.4.0"
4
4
  end
@@ -3,6 +3,7 @@ module Gnucash
3
3
  before(:all) do
4
4
  # just read the file once
5
5
  @book = Gnucash.open("spec/books/sample.gnucash")
6
+ @root = @book.find_account_by_full_name("Root Account")
6
7
  @assets = @book.find_account_by_full_name("Assets")
7
8
  @checking = @book.find_account_by_full_name("Assets:Current Assets:Checking Account")
8
9
  @income = @book.find_account_by_full_name("Income")
@@ -21,6 +22,18 @@ module Gnucash
21
22
  expect(@checking.full_name).to eq "Assets:Current Assets:Checking Account"
22
23
  end
23
24
 
25
+ it "gives access to parent account id" do
26
+ expect(@salary.parent_id).to eq @income.id
27
+ expect(@income.parent_id).to eq @root.id
28
+ expect(@root.parent_id).to eq nil
29
+ end
30
+
31
+ it "gives access to parent account" do
32
+ expect(@salary.parent).to eq @income
33
+ expect(@income.parent).to eq @root
34
+ expect(@root.parent).to eq nil
35
+ end
36
+
24
37
  it "gives access to the final balance" do
25
38
  expect(@checking.final_balance).to eq Value.new(19743000)
26
39
  end
@@ -49,5 +62,9 @@ module Gnucash
49
62
  expect(@income.placeholder).to be_truthy
50
63
  expect(@salary.placeholder).to be_falsey
51
64
  end
65
+
66
+ it "avoid inspection of heavier attributes" do
67
+ expect(@salary.inspect).to eq "#<Gnucash::Account id: efebb6cb617971b0a7f62e9d5a204789, name: Salary, description: Salary, type: INCOME, placeholder: false, parent_id: 35ab61d46f5404895bf5d4949f8a5593>"
68
+ end
52
69
  end
53
70
  end
@@ -0,0 +1,16 @@
1
+ module Gnucash
2
+ describe AccountTransaction do
3
+ before(:all) do
4
+ @book = Gnucash.open("spec/books/sample.gnucash")
5
+ @txn = @book.find_account_by_full_name("Assets:Current Assets:Cash in Wallet").transactions.first
6
+ end
7
+
8
+ it "keeps track of the transaction description" do
9
+ expect(@txn.description).to eq "Opening Balance"
10
+ end
11
+
12
+ it "avoid inspection of heavier attributes" do
13
+ expect(@txn.inspect).to eq "#<Gnucash::AccountTransaction value: 100.00>"
14
+ end
15
+ end
16
+ end
@@ -34,6 +34,10 @@ module Gnucash
34
34
  it "lets you find an account by full name" do
35
35
  expect(@subject.find_account_by_full_name("Assets:Current Assets:Savings Account").id).to eq "67e6e7daadc35716eb6152769373e974"
36
36
  end
37
+
38
+ it "avoid inspection of heavier attributes" do
39
+ expect(@subject.inspect).to eq "#<Gnucash::Book start_date: 2007-01-01, end_date: 2012-12-28>"
40
+ end
37
41
  end
38
42
  end
39
43
  end
@@ -0,0 +1,15 @@
1
+ module Gnucash
2
+ module Support
3
+ describe LightInspect do
4
+ class Klass
5
+ include LightInspect
6
+ end
7
+
8
+ describe "#attributes" do
9
+ it "returns an empty Array if not overridden" do
10
+ expect(Klass.new.attributes).to eq([])
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -29,11 +29,10 @@ module Gnucash
29
29
  context "without errors" do
30
30
  before(:all) do
31
31
  @book = Gnucash.open("spec/books/sample.gnucash")
32
- @txn = @book.find_account_by_full_name("Assets:Current Assets:Cash in Wallet").transactions.first
33
32
  end
34
33
 
35
- it "keeps track of the transaction description" do
36
- expect(@txn.description).to eq "Opening Balance"
34
+ it "avoid inspection of heavier attributes" do
35
+ expect(@book.transactions.first.inspect).to eq "#<Gnucash::Transaction id: 12efba30f14dc6cd4c3ffe2994de8284, date: 2007-01-01, description: Opening Balance, splits: [{:account=>#<Gnucash::Account id: 849f778995e8ecf8d4b96940afbbdcd7, name: Checking Account, description: Checking Account, type: BANK, placeholder: false, parent_id: c8e868259d70f6491f9e70ffdf6634ee>, :value=>#<Gnucash::Value val: 30000, div: 100>}, {:account=>#<Gnucash::Account id: 23bea6468ee7b4acb4db4b3f54598a71, name: Opening Balances, description: Opening Balances, type: EQUITY, placeholder: false, parent_id: ea7fe8b8abd560bef49826f68387ca78>, :value=>#<Gnucash::Value val: -30000, div: 100>}]>"
37
36
  end
38
37
  end
39
38
  end
@@ -1,7 +1,7 @@
1
1
  module Gnucash
2
2
  describe Value do
3
- describe '.zero' do
4
- it 'creates a Value object with value 0' do
3
+ describe ".zero" do
4
+ it "creates a Value object with value 0" do
5
5
  expect(Value.zero.val).to eq 0
6
6
  end
7
7
  end
@@ -23,6 +23,16 @@ module Gnucash
23
23
  expect(Value.new("1000231455/100").to_s).to eq "10002314.55"
24
24
  end
25
25
 
26
+ it "converts the value to the expected float representation" do
27
+ expect(Value.new("5678/100").to_f).to be_within(0.00000001).of(56.78)
28
+ expect(Value.new("1000231455/100").to_f).to be_within(0.00000001).of(10002314.55)
29
+ end
30
+
31
+ it "converts the value to the expected rational representation" do
32
+ expect(Value.new("5678/55").to_r).to eq Rational(5678, 55)
33
+ expect(Value.new("1000231455/49").to_r).to eq Rational(1000231455, 49)
34
+ end
35
+
26
36
  it "allows adding two value objects" do
27
37
  a = Value.new("1234/100")
28
38
  b = Value.new("2345/100")
@@ -101,6 +111,10 @@ module Gnucash
101
111
  expect(Value.new(80, 100) - Value.new(5, 50)).to eq Value.new(70, 100)
102
112
  end
103
113
 
114
+ it "avoid inspection of heavier attributes" do
115
+ expect(Value.new("1234/10000").inspect).to eq "#<Gnucash::Value val: 1234, div: 10000>"
116
+ end
117
+
104
118
  context "errors" do
105
119
  it "raises an error when attempting to add an unknown object type" do
106
120
  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.3.1
4
+ version: 1.4.0
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-04 00:00:00.000000000 Z
11
+ date: 2018-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -114,13 +114,17 @@ files:
114
114
  - lib/gnucash/account.rb
115
115
  - lib/gnucash/account_transaction.rb
116
116
  - lib/gnucash/book.rb
117
+ - lib/gnucash/support.rb
118
+ - lib/gnucash/support/light_inspect.rb
117
119
  - lib/gnucash/transaction.rb
118
120
  - lib/gnucash/value.rb
119
121
  - lib/gnucash/version.rb
120
122
  - spec/books/sample-text.gnucash
121
123
  - spec/books/sample.gnucash
122
124
  - spec/gnucash/account_spec.rb
125
+ - spec/gnucash/account_transaction_spec.rb
123
126
  - spec/gnucash/book_spec.rb
127
+ - spec/gnucash/support/light_inspect_spec.rb
124
128
  - spec/gnucash/transaction_spec.rb
125
129
  - spec/gnucash/value_spec.rb
126
130
  - spec/gnucash_spec.rb
@@ -137,7 +141,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
137
141
  requirements:
138
142
  - - ">="
139
143
  - !ruby/object:Gem::Version
140
- version: '0'
144
+ version: '2.0'
141
145
  required_rubygems_version: !ruby/object:Gem::Requirement
142
146
  requirements:
143
147
  - - ">="
@@ -145,7 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
145
149
  version: '0'
146
150
  requirements: []
147
151
  rubyforge_project:
148
- rubygems_version: 2.5.2
152
+ rubygems_version: 2.6.14
149
153
  signing_key:
150
154
  specification_version: 4
151
155
  summary: Extract data from XML GnuCash data files
@@ -153,7 +157,9 @@ test_files:
153
157
  - spec/books/sample-text.gnucash
154
158
  - spec/books/sample.gnucash
155
159
  - spec/gnucash/account_spec.rb
160
+ - spec/gnucash/account_transaction_spec.rb
156
161
  - spec/gnucash/book_spec.rb
162
+ - spec/gnucash/support/light_inspect_spec.rb
157
163
  - spec/gnucash/transaction_spec.rb
158
164
  - spec/gnucash/value_spec.rb
159
165
  - spec/gnucash_spec.rb