Class: Ledger::Options
- Inherits:
-
Object
- Object
- Ledger::Options
- Defined in:
- lib/ledgerjournal/options.rb
Overview
Options for interaction with ledger-cli
Instance Attribute Summary collapse
-
#date_format ⇒ Object
readonly
Returns the value of attribute date_format.
-
#decimal_comma ⇒ Object
readonly
Returns the value of attribute decimal_comma.
Class Method Summary collapse
-
.for_locale(locale) ⇒ Ledger::Options
Returns default options by locale.
Instance Method Summary collapse
-
#format(value) ⇒ String
Formats the given value as String.
-
#initialize(date_format:, decimal_comma:) ⇒ Options
constructor
A new instance of Options.
-
#parse_amount(string) ⇒ BigDecimal
The given string as BigDecimal, parsed according the decimal_comma setting.
-
#run(args, stdin: nil) ⇒ String
Runs ledger-cli.
Constructor Details
#initialize(date_format:, decimal_comma:) ⇒ Options
Returns a new instance of Options.
11 12 13 14 |
# File 'lib/ledgerjournal/options.rb', line 11 def initialize(date_format:, decimal_comma:) @date_format = date_format @decimal_comma = decimal_comma end |
Instance Attribute Details
#date_format ⇒ Object (readonly)
Returns the value of attribute date_format.
6 7 8 |
# File 'lib/ledgerjournal/options.rb', line 6 def date_format @date_format end |
#decimal_comma ⇒ Object (readonly)
Returns the value of attribute decimal_comma.
7 8 9 |
# File 'lib/ledgerjournal/options.rb', line 7 def decimal_comma @decimal_comma end |
Class Method Details
.for_locale(locale) ⇒ Ledger::Options
Returns default options by locale. Currently supported are :en or :de.
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/ledgerjournal/options.rb', line 19 def self.for_locale(locale) case locale.to_sym when :en Options.new(date_format: '%Y/%m/%d', decimal_comma: false) when :de Options.new(date_format: '%d.%m.%Y', decimal_comma: true) else raise Error, "Unknown locale for ledger options: #{locale}, supported are :en, :de" end end |
Instance Method Details
#format(value) ⇒ String
Formats the given value as String
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/ledgerjournal/options.rb', line 39 def format(value) if value.instance_of? Date return value.strftime(@date_format) elsif value.instance_of? BigDecimal str = '%.2f' % value str.gsub!('.', ',') if @decimal_comma return str else raise Error, "Unknown value type #{value.class}" end end |
#parse_amount(string) ⇒ BigDecimal
Returns the given string as BigDecimal, parsed according the decimal_comma setting.
32 33 34 |
# File 'lib/ledgerjournal/options.rb', line 32 def parse_amount(string) BigDecimal(@decimal_comma ? string.gsub('.', '').gsub(',', '.') : string) end |
#run(args, stdin: nil) ⇒ String
Runs ledger-cli
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/ledgerjournal/options.rb', line 55 def run(args, stdin: nil) output = String.new error = String.new begin Open4.spawn("ledger #{} #{args}", stdin: stdin, stdout: output, stderr: error) rescue StandardError => e raise Error, "#{e}: #{error}" end return output end |