lib/ldgr/parser.rb in ldgr-0.2.0 vs lib/ldgr/parser.rb in ldgr-0.2.1
- old
+ new
@@ -1,9 +1,9 @@
+# frozen_string_literal: false
require 'irb'
require 'csv'
require 'date'
-require 'highline/import'
require 'optparse'
require 'optparse/date'
require 'pathname'
require 'strscan'
require 'fileutils'
@@ -23,11 +23,11 @@
FILEBASE = Dir.home + '/.config/ledger/'
VERSION = Ldgr::VERSION
PROGRAM_NAME = 'ldgr'
MATCH = /(?=(\n\d\d\d\d-\d\d-\d\d)(=\d\d\d\d-\d\d-\d\d)*)|\z/
OTHER_MATCH = /(?=(\d\d\d\d-\d\d-\d\d)(=\d\d\d\d-\d\d-\d\d)*)/
- COMMANDS = %w(add sort tag clear open)
+ COMMANDS = %w(add sort tag clear open).freeze
attr_accessor :transactions_file, :config
# Public: Creates a new Parser object
#
@@ -42,23 +42,10 @@
def initialize(config: {})
@transactions_file = defaults.fetch(:transactions_file)
@config = defaults.merge(user_config).merge(config)
end
- # Public: User-specified config options
- #
- # Examples
- #
- # user_config
- # # => {all the config options from the user's YAML file}
- #
- # Returns a hash of user-specified config options.
- def user_config
- path = Pathname(FILEBASE + 'ldgr.yaml')
- path.exist? ? YAML.load_file(path).to_h : {}
- end
-
# Public: Kicks off the CLI
#
# Examples
#
# parse
@@ -136,15 +123,15 @@
end
if match && Date.today >= effective_date
count += 1
front = match[1]
back = match[4]
- puts "\n#{HighLine.color(transaction, :magenta)}"
+ puts transaction
question = ask('Do you want to clear this? ') do |q|
q.default = 'No'
end
- transaction.gsub!(pattern, "#{front} * #{back}") if question =~ /y/i
+ transaction.gsub!(pattern, "#{front} * #{back}") if question.match?(/y/i)
end
output << transaction
end
end
IO.write(transactions_file, output)
@@ -166,11 +153,11 @@
File.open(transactions_file, 'r') do |transactions|
transactions.each_line do |transaction|
match = pattern.match(transaction)
if match
count += 1
- puts "\n#{HighLine.color(previous, :blue)} #{HighLine.color(match[2], :red)}"
+ puts "\n#{previous} #{match[2]}"
question = ask('What account does this belong to? ') { |q| q.default = 'None' }
transaction.gsub!(match[1], " #{question.capitalize} ") if question != 'None'
end
previous = transaction.chomp
output << transaction
@@ -199,31 +186,43 @@
File.open(transactions_file, 'w') do |file|
file.puts results.sort
end
end
- # Public: Opens a settings file from ~/.config/ledger
+ private
+ # Private: User-specified config options
#
# Examples
#
+ # user_config
+ # # => {all the config options from the user's YAML file}
+ #
+ # Returns a hash of user-specified config options.
+ def user_config
+ path = Pathname(FILEBASE + 'ldgr.yaml')
+ path.exist? ? YAML.load_file(path).to_h : {}
+ end
+
+ # Private: Opens a settings file from ~/.config/ledger
+ #
+ # Examples
+ #
# open accounts
# # => accounts file opens in $EDITOR
#
# Returns nothing.
def open
def open_file(file_to_open)
checked_file = "#{FILEBASE}#{file_to_open}.dat"
- raise "#{checked_file} doesn't exist." unless Pathname(checked_file).exist?
+ fail "#{checked_file} doesn't exist." unless Pathname(checked_file).exist?
system(ENV['EDITOR'], checked_file)
end
open_file(ARGV[1])
end
- private :open
-
- # Public: ldgr's default configuration options
+ # Private: ldgr's default configuration options
#
# Examples
#
# defaults
# # => {all the configuration options}
@@ -248,8 +247,7 @@
FileUtils.mkdir_p(FILEBASE)
setup_files.each do |file|
FileUtils.touch("#{FILEBASE}#{file}")
end
end
- private :setup
end
end