Class: LedgerRest::Ledger::Transaction
- Defined in:
- lib/ledger-rest/ledger/transaction.rb
Class Method Summary collapse
-
.parse(str) ⇒ Object
Parse a ledger transaction string into a ‘Transaction` object.
Instance Method Summary collapse
- #check ⇒ Object
-
#initialize(params = {}) ⇒ Transaction
constructor
A new instance of Transaction.
- #to_ledger ⇒ Object
-
#valid? ⇒ Boolean
Return true if the ‘Transaction#to_ledger` is a valid ledger string.
Methods inherited from Hash
#symbolize_keys, #symbolize_keys!
Constructor Details
#initialize(params = {}) ⇒ Transaction
Returns a new instance of Transaction.
12 13 14 |
# File 'lib/ledger-rest/ledger/transaction.rb', line 12 def initialize(params = {}) self.merge!(params) end |
Class Method Details
.parse(str) ⇒ Object
Parse a ledger transaction string into a ‘Transaction` object.
7 8 9 |
# File 'lib/ledger-rest/ledger/transaction.rb', line 7 def parse(str) LedgerRest::Ledger::Parser.parse(str) end |
Instance Method Details
#check ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/ledger-rest/ledger/transaction.rb', line 27 def check IO.popen("#{Ledger.bin} -f - stats 2>&1", 'r+') do |f| f.write to_ledger f.close_write f.readlines end end |
#to_ledger ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/ledger-rest/ledger/transaction.rb', line 35 def to_ledger if self[:date].nil? || self[:payee].nil? || self[:postings].nil? return nil end result = '' result << self[:date] if self[:effective_date] result << "=#{self[:effective_date]}" end if self[:cleared] result << ' *' elsif self[:pending] result << ' !' end result << " (#{self[:code]})" if self[:code] result << " #{self[:payee]}" result << "\n" self[:postings].each do |posting| if posting[:comment] result << " ; #{posting[:comment]}\n" next end next unless posting[:account] if posting[:virtual] if posting[:balance] result << " [#{posting[:account]}]" else result << " (#{posting[:account]})" end else result << " #{posting[:account]}" end if posting[:amount].nil? result << "\n" next end result << " #{'%.2f' % posting[:amount]}#{posting[:commodity]}" if posting[:per_unit_cost] result << " @@ #{'%.2f' % posting[:per_unit_cost]}#{posting[:per_unit_commodity]}" elsif posting[:posting_cost] result << " @ #{'%.2f' % posting[:posting_cost]}#{posting[:posting_cost_commodity]}" end if posting[:actual_date] || posting[:effective_date] result << ' ; [' result << posting[:actual_date] if posting[:actual_date] if posting[:effective_date] result << "=#{posting[:effective_date]}" end result << ']' end result << "\n" end result end |
#valid? ⇒ Boolean
Return true if the ‘Transaction#to_ledger` is a valid ledger string.
17 18 19 20 21 22 23 24 25 |
# File 'lib/ledger-rest/ledger/transaction.rb', line 17 def valid? result = IO.popen("#{Ledger.bin} -f - stats 2>&1", 'r+') do |f| f.write to_ledger f.close_write f.readlines end $?.success? && !result.empty? end |