libledger 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a389bd843cbd1fe00130adbd228914fc2be6e8bc
4
+ data.tar.gz: 7736b6c5b3d80730f46f3e852cbdada4312691db
5
+ SHA512:
6
+ metadata.gz: 5017dbc9256136b0af509801457a91d8156865ecfd1743c1b6e75b7392e4be630afb6c62e6248db4d7bdc5d7591959fe1417f1e4b87e8e6f8aafcabdbf098fce
7
+ data.tar.gz: 1c14be5f9762e5d5c7fbb526ae2e900684f864a07110cd93da30f02c2338037cc9cfce848952e6fd15d94a5a3143a3b299f3442505fcba87ce47164ca2a29445
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ pkg/*.gem
2
+ coverage/
3
+ .coveralls.yml
4
+ .bundle
5
+ Gemfile.lock
data/.prospectus ADDED
@@ -0,0 +1,11 @@
1
+ item do
2
+ expected do
3
+ static
4
+ set 'green'
5
+ end
6
+
7
+ actual do
8
+ gemnasium
9
+ slug 'akerl/libledger'
10
+ end
11
+ end
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format Fuubar
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ Encoding:
2
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Les Aker
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,22 @@
1
+ libledger
2
+ =========
3
+
4
+ [![Gem Version](https://img.shields.io/gem/v/libledger.svg)](https://rubygems.org/gems/libledger)
5
+ [![Dependency Status](https://img.shields.io/gemnasium/akerl/libledger.svg)](https://gemnasium.com/akerl/libledger)
6
+ [![Build Status](https://img.shields.io/circleci/project/akerl/libledger.svg)](https://circleci.com/gh/akerl/libledger)
7
+ [![Coverage Status](https://img.shields.io/codecov/c/github/akerl/libledger.svg)](https://codecov.io/github/akerl/libledger)
8
+ [![Code Quality](https://img.shields.io/codacy/.svg)](https://www.codacy.com/app/akerl/libledger)
9
+ [![MIT Licensed](https://img.shields.io/badge/license-MIT-green.svg)](https://tldrlegal.com/license/mit-license)
10
+
11
+ Library for interacting with [Ledger](http://www.ledger-cli.org/) files.
12
+
13
+ ## Usage
14
+
15
+ ## Installation
16
+
17
+ gem install libledger
18
+
19
+ ## License
20
+
21
+ libledger is released under the MIT License. See the bundled LICENSE file for details.
22
+
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'rubocop/rake_task'
4
+
5
+ desc 'Run tests'
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ desc 'Run Rubocop on the gem'
9
+ RuboCop::RakeTask.new(:rubocop) do |task|
10
+ task.patterns = ['lib/**/*.rb', 'spec/**/*.rb']
11
+ task.fail_on_error = true
12
+ end
13
+
14
+ task default: [:spec, :rubocop, :build, :install]
data/circle.yml ADDED
@@ -0,0 +1,12 @@
1
+ dependencies:
2
+ override:
3
+ - 'rvm-exec 1.9.3-p551 bundle install'
4
+ - 'rvm-exec 2.0.0-p645 bundle install'
5
+ - 'rvm-exec 2.1.6 bundle install'
6
+ - 'rvm-exec 2.2.2 bundle install'
7
+ test:
8
+ override:
9
+ - 'rvm-exec 1.9.3-p551 bundle exec rake'
10
+ - 'rvm-exec 2.0.0-p645 bundle exec rake'
11
+ - 'rvm-exec 2.1.6 bundle exec rake'
12
+ - 'rvm-exec 2.2.2 bundle exec rake'
data/lib/libledger.rb ADDED
@@ -0,0 +1,15 @@
1
+ ##
2
+ # This module parses the Ledger format
3
+ module Ledger
4
+ class << self
5
+ ##
6
+ # Insert a helper .new() method for creating a new Ledger object
7
+
8
+ def new(*args)
9
+ self::Journal.new(*args)
10
+ end
11
+ end
12
+ end
13
+
14
+ require 'libledger/entry'
15
+ require 'libledger/journal'
@@ -0,0 +1,94 @@
1
+ require 'date'
2
+
3
+ module Ledger
4
+ ENTRY_SUBJECT_LINE_REGEX = %r{^(\d+/\d+/\d+) (?:([*!]) )?(.*)$}
5
+ ENTRY_ACTION_LINE_REGEX = /^\s+(\w+[^( )\t]*)(?:\s+(.*))?$/
6
+
7
+ ##
8
+ # Declaration for entry object
9
+ class Entry
10
+ def initialize(params = {})
11
+ @data = params
12
+ end
13
+
14
+ def name
15
+ @name ||= @data[:name]
16
+ end
17
+
18
+ def state
19
+ @state ||= @data[:state]
20
+ end
21
+
22
+ def state_as_symbol
23
+ return @state_as_symbol if @state_as_symbol
24
+ @state = case state
25
+ when '*'
26
+ :cleared
27
+ when '!'
28
+ :pending
29
+ else
30
+ raise "Unexpected state on #{name}: #{state}"
31
+ end
32
+ end
33
+
34
+ def date
35
+ return @date if @date
36
+ @date ||= @data[:date] if @data[:date].is_a? Date
37
+ @date ||= Date.parse(@data[:date])
38
+ end
39
+
40
+ def actions
41
+ @actions ||= @data[:actions]
42
+ end
43
+
44
+ def to_s
45
+ subject_line + action_lines.join("\n") + "\n"
46
+ end
47
+
48
+ private
49
+
50
+ def subject_line
51
+ "#{date.strftime('%Y/%m/%d')} #{state} #{name}\n"
52
+ end
53
+
54
+ def action_lines
55
+ actions.map do |x|
56
+ line = " #{x[:name]}"
57
+ line += ' ' * action_padding(x) + x[:amount] if x[:amount]
58
+ line
59
+ end
60
+ end
61
+
62
+ def action_padding(action, width = 62)
63
+ # Minus 4 for intent at front of line
64
+ width - 4 - action[:name].size - action[:amount].size
65
+ end
66
+
67
+ class << self
68
+ def from_lines(lines)
69
+ params = parse_first_line(lines.shift)
70
+ params[:actions] = lines.map { |x| parse_action_line x }
71
+ Entry.new(params)
72
+ end
73
+
74
+ private
75
+
76
+ def parse_first_line(line)
77
+ date, state, name = line.match(ENTRY_SUBJECT_LINE_REGEX).captures
78
+ {
79
+ date: date,
80
+ state: state,
81
+ name: name
82
+ }
83
+ end
84
+
85
+ def parse_action_line(line)
86
+ name, amount = line.match(ENTRY_ACTION_LINE_REGEX).captures
87
+ {
88
+ name: name,
89
+ amount: amount
90
+ }
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,35 @@
1
+ module Ledger
2
+ ##
3
+ # Declaration for list of entries
4
+ class Journal
5
+ attr_reader :entries
6
+
7
+ def initialize(params = {})
8
+ @entries = params[:entries] || []
9
+ end
10
+
11
+ def to_s
12
+ @entries.map(&:to_s).join("\n")
13
+ end
14
+
15
+ class << self
16
+ def from_files(files)
17
+ Journal.new(
18
+ entries: files.map { |x| from_file(x).entries }.flatten
19
+ )
20
+ end
21
+
22
+ def from_file(file)
23
+ entries = read_file_chunks(file).map { |x| Entry.from_lines(x) }
24
+ Journal.new(entries: entries)
25
+ end
26
+
27
+ private
28
+
29
+ def read_file_chunks(file)
30
+ chunks = File.read(file).split("\n").chunk_while { |_, x| x =~ /^\s/ }
31
+ chunks.select { |x| x.size > 1 }
32
+ end
33
+ end
34
+ end
35
+ end
data/libledger.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'libledger'
3
+ s.version = '0.0.1'
4
+ s.date = Time.now.strftime('%Y-%m-%d')
5
+
6
+ s.summary = 'Library for interacting with Ledger files'
7
+ s.description = "Library for interacting with Ledger files"
8
+ s.authors = ['Les Aker']
9
+ s.email = 'me@lesaker.org'
10
+ s.homepage = 'https://github.com/akerl/libledger'
11
+ s.license = 'MIT'
12
+
13
+ s.files = `git ls-files`.split
14
+ s.test_files = `git ls-files spec/*`.split
15
+
16
+ s.add_development_dependency 'rubocop', '~> 0.39.0'
17
+ s.add_development_dependency 'rake', '~> 11.1.0'
18
+ s.add_development_dependency 'codecov', '~> 0.1.1'
19
+ s.add_development_dependency 'rspec', '~> 3.4.0'
20
+ s.add_development_dependency 'fuubar', '~> 2.0.0'
21
+ end
@@ -0,0 +1 @@
1
+ require 'spec_helper'
@@ -0,0 +1,11 @@
1
+ if ENV['CI'] == 'true'
2
+ require 'simplecov'
3
+ require 'codecov'
4
+ SimpleCov.formatter = SimpleCov::Formatter::Codecov
5
+ SimpleCov.start do
6
+ add_filter '/spec/'
7
+ end
8
+ end
9
+
10
+ require 'rspec'
11
+ require 'libledger'
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: libledger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Les Aker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubocop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.39.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.39.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 11.1.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 11.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: codecov
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.1.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.1.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.4.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.4.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: fuubar
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 2.0.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 2.0.0
83
+ description: Library for interacting with Ledger files
84
+ email: me@lesaker.org
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - ".gitignore"
90
+ - ".prospectus"
91
+ - ".rspec"
92
+ - ".rubocop.yml"
93
+ - Gemfile
94
+ - LICENSE
95
+ - README.md
96
+ - Rakefile
97
+ - circle.yml
98
+ - lib/libledger.rb
99
+ - lib/libledger/entry.rb
100
+ - lib/libledger/journal.rb
101
+ - libledger.gemspec
102
+ - spec/libledger_spec.rb
103
+ - spec/spec_helper.rb
104
+ homepage: https://github.com/akerl/libledger
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.5.1
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Library for interacting with Ledger files
128
+ test_files:
129
+ - spec/libledger_spec.rb
130
+ - spec/spec_helper.rb