lib/reckon/app.rb in reckon-0.3.4 vs lib/reckon/app.rb in reckon-0.3.5
- old
+ new
@@ -157,10 +157,11 @@
def money_for(index)
value = money_column_indices.inject("") { |m, i| m + columns[i][index] }
value = value.gsub(/\./, '').gsub(/,/, '.') if options[:comma_separates_cents]
cleaned_value = value.gsub(/[^\d\.]/, '').to_f
cleaned_value *= -1 if value =~ /[\(\-]/
+ cleaned_value = -(cleaned_value) if options[:inverse]
cleaned_value
end
def pretty_money_for(index, negate = false)
pretty_money(money_for(index), negate)
@@ -332,11 +333,19 @@
end
end
def parse
data = options[:string] || File.read(options[:file])
- @csv_data = (RUBY_VERSION =~ /^1\.9/ ? CSV : FasterCSV).parse(data.strip, :col_sep => options[:csv_separator] || ',')
+
+ if RUBY_VERSION =~ /^1\.9/ || RUBY_VERSION =~ /^2/
+ data = data.force_encoding(options[:encoding] || 'BINARY').encode('UTF-8', :invalid => :replace, :undef => :replace, :replace => '?')
+ csv_engine = CSV
+ else
+ csv_engine = FasterCSV
+ end
+
+ @csv_data = csv_engine.parse data.strip, :col_sep => options[:csv_separator] || ','
csv_data.shift if options[:contains_header]
csv_data
end
def self.parse_opts(args = ARGV)
@@ -351,10 +360,14 @@
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
options[:verbose] = v
end
+ opts.on("-i", "--inverse", "Use the negative of each amount") do |v|
+ options[:inverse] = v
+ end
+
opts.on("-p", "--print-table", "Print out the parsed CSV in table form") do |p|
options[:print_table] = p
end
opts.on("-o", "--output-file FILE", "The ledger file to append to") do |o|
@@ -377,9 +390,13 @@
options[:csv_separator] = csv_separator
end
opts.on("", "--comma-separates-cents", "Use comma instead of period to deliminate dollars from cents when parsing ($100,50 instead of $100.50)") do |c|
options[:comma_separates_cents] = c
+ end
+
+ opts.on("", "--encoding", "Specify an encoding for the CSV file") do |e|
+ options[:encoding] = e
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit