lib/ldgr/parser.rb in ldgr-0.1.9 vs lib/ldgr/parser.rb in ldgr-0.2.0

- old
+ new

@@ -23,10 +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) attr_accessor :transactions_file, :config # Public: Creates a new Parser object # @@ -51,43 +52,23 @@ # # => {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') - YAML.load_file(path).to_h + path.exist? ? YAML.load_file(path).to_h : {} end - # Public: available commands - # - # Examples - # - # commands - # - # Returns an array of command names. - def commands - %w(add sort tag clear open) - end - - # Public: expected setup files - # - # Examples - # - # setup_files - # - # Returns an array of file names. - def self.setup_files - %w(transactions.dat accounts.dat budgets.dat aliases.dat commodities.dat setup.dat ledger.dat ldgr.yaml) - end - # Public: Kicks off the CLI # # Examples # # parse # # Returns nothing. def parse + setup + cli = OptionParser.new do |o| o.banner = "Usage #{PROGRAM_NAME} [add|sort|tag|clear|open]" o.program_name = PROGRAM_NAME o.version = VERSION @@ -101,11 +82,11 @@ o.define '-A', '--amount=AMOUNT', String, 'the amount of the transaction' o.define '-p', '--payee=PAYEE', String, 'the payee of the transaction' end command = String(cli.parse(ARGV, into: config)[0]) - send(command) if commands.include? command + send(command) if COMMANDS.include? command end # Public: Adds a transaction to the transactions_file. # # Examples @@ -236,10 +217,12 @@ end open_file(ARGV[1]) end + private :open + # Public: ldgr's default configuration options # # Examples # # defaults @@ -258,27 +241,15 @@ end # Private: Prepares users' file system for ldgr. # # Returns nothing. - def self.setup - unless config_exist? - FileUtils.mkdir_p(FILEBASE) - setup_files.each do |file| - FileUtils.touch("#{FILEBASE}#{file}") - end + def setup + setup_files = %w(transactions.dat accounts.dat budgets.dat aliases.dat commodities.dat setup.dat ledger.dat ldgr.yaml) + FileUtils.mkdir_p(FILEBASE) + setup_files.each do |file| + FileUtils.touch("#{FILEBASE}#{file}") end end - - # Private: Checks if user already has setup files created. - # - # Returns nothing. - def self.config_exist? - setup_files.each do |file| - return false unless Pathname("#{FILEBASE}#{file}").exist? - end - true - end - - setup + private :setup end end