Class: HledgerForecast::Transaction

Inherits:
Struct
  • Object
show all
Defined in:
lib/hledger_forecast/transaction.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#accountObject

Returns the value of attribute account

Returns:

  • (Object)

    the current value of account



12
13
14
# File 'lib/hledger_forecast/transaction.rb', line 12

def 
  @account
end

#amountObject

Returns the value of attribute amount

Returns:

  • (Object)

    the current value of amount



12
13
14
# File 'lib/hledger_forecast/transaction.rb', line 12

def amount
  @amount
end

#categoryObject

Returns the value of attribute category

Returns:

  • (Object)

    the current value of category



12
13
14
# File 'lib/hledger_forecast/transaction.rb', line 12

def category
  @category
end

#descriptionObject

Returns the value of attribute description

Returns:

  • (Object)

    the current value of description



12
13
14
# File 'lib/hledger_forecast/transaction.rb', line 12

def description
  @description
end

#frequencyObject

Returns the value of attribute frequency

Returns:

  • (Object)

    the current value of frequency



12
13
14
# File 'lib/hledger_forecast/transaction.rb', line 12

def frequency
  @frequency
end

#fromObject

Returns the value of attribute from

Returns:

  • (Object)

    the current value of from



12
13
14
# File 'lib/hledger_forecast/transaction.rb', line 12

def from
  @from
end

#roll_upObject

Returns the value of attribute roll_up

Returns:

  • (Object)

    the current value of roll_up



12
13
14
# File 'lib/hledger_forecast/transaction.rb', line 12

def roll_up
  @roll_up
end

#summary_excludeObject

Returns the value of attribute summary_exclude

Returns:

  • (Object)

    the current value of summary_exclude



12
13
14
# File 'lib/hledger_forecast/transaction.rb', line 12

def summary_exclude
  @summary_exclude
end

#tagsObject

Returns the value of attribute tags

Returns:

  • (Object)

    the current value of tags



12
13
14
# File 'lib/hledger_forecast/transaction.rb', line 12

def tags
  @tags
end

#toObject

Returns the value of attribute to

Returns:

  • (Object)

    the current value of to



12
13
14
# File 'lib/hledger_forecast/transaction.rb', line 12

def to
  @to
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



12
13
14
# File 'lib/hledger_forecast/transaction.rb', line 12

def type
  @type
end

Class Method Details

.from_row(row) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hledger_forecast/transaction.rb', line 26

def self.from_row(row)
  validate_required_fields!(row)

  from = Calculator.evaluate_from_date(row[:from])
  new(
    type: row[:type],
    frequency: row[:frequency],
    account: row[:account],
    from: from,
    to: row[:to] ? Calculator.evaluate_date(from, row[:to]) : nil,
    description: row[:description],
    category: row[:category],
    amount: Calculator.evaluate(row[:amount]),
    roll_up: row[:roll_up],
    summary_exclude: row[:summary_exclude],
    tags: row[:tag].to_s.split("|").map(&:strip).reject(&:empty?)
  )
end

.validate_required_fields!(row) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/hledger_forecast/transaction.rb', line 70

def self.validate_required_fields!(row)
  %i[type account from category amount].each do |field|
    next unless row[field].nil? || row[field].to_s.strip.empty?

    hint = row[:description] ? " (description: '#{row[:description]}')" : ""
    raise ArgumentError, "missing required field '#{field}'#{hint}"
  end
end

Instance Method Details

#annualised_amountObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/hledger_forecast/transaction.rb', line 57

def annualised_amount
  if roll_up
    amount * roll_up
  else
    amount *
      ANNUAL_MULTIPLIERS.fetch(type) {
        raise(KeyError, "Unknown type '#{type}'. Set a roll-up for custom transactions.")
      }
  end
end

#matches_tags?(filter_tags) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
55
# File 'lib/hledger_forecast/transaction.rb', line 45

def matches_tags?(filter_tags)
  return true if filter_tags.nil? || filter_tags.empty?

  exclude_tags = filter_tags.select { |t| t.start_with?("-") }.map { |t| t[1..] }
  include_tags = filter_tags.reject { |t| t.start_with?("-") }

  return false if exclude_tags.any? && (tags & exclude_tags).any?
  return (tags & include_tags).any? if include_tags.any?

  true
end

#summary_exclude?Boolean

Returns:

  • (Boolean)


68
# File 'lib/hledger_forecast/transaction.rb', line 68

def summary_exclude? = !!summary_exclude