Class: Ledger::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/ledgerjournal/options.rb

Overview

Options for interaction with ledger-cli

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(date_format:, decimal_comma:) ⇒ Options

Returns a new instance of Options.

Parameters:

  • date_format (String)

    like ā€˜%Y/%m/%dā€™ to pass to ledger-cli

  • decimal_comma (Boolean)

    pass true to use a comma as decimal separator, otherwise a dot is used



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_formatObject (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_commaObject (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.

Parameters:

  • locale (Symbol)

    as symbol

Returns:



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

Parameters:

  • value (Date, BigDecimal)

    to parse

Returns:

  • (String)

    string representation according to the options



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.

Parameters:

  • string (String)

    decimal amount as String (like ā€˜12.34ā€™)

Returns:

  • (BigDecimal)

    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

Parameters:

  • args (String)

    command line arguments to pass

  • stdin (String) (defaults to: nil)

    stdin text to pass

Returns:

  • (String)

    stdout result of calling ledger



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 #{cmdline_options} #{args}", stdin: stdin, stdout: output, stderr: error)
  rescue StandardError => e
    raise Error, "#{e}: #{error}"
  end
  return output
end