Module: HledgerForecast::Calculator

Defined in:
lib/hledger_forecast/calculator.rb

Class Method Summary collapse

Class Method Details

.evaluate(amount) ⇒ Object

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
# File 'lib/hledger_forecast/calculator.rb', line 5

def self.evaluate(amount)
  return amount.to_f unless amount.is_a?(String)

  result = @calc.evaluate(amount.slice(1..-1))
  raise ArgumentError, "invalid amount '#{amount}'" if result.nil?

  result
end

.evaluate_date(from, to) ⇒ Object



25
26
27
28
29
30
# File 'lib/hledger_forecast/calculator.rb', line 25

def self.evaluate_date(from, to)
  return (from >> to) - 1 if to.is_a?(Numeric)
  return Date.parse(to) unless to.start_with?("=") || to.start_with?("+")

  (from >> @calc.evaluate(to.slice(1..-1))) - 1
end

.evaluate_from_date(value) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/hledger_forecast/calculator.rb', line 14

def self.evaluate_from_date(value)
  value = value.to_s
  return Date.parse(value) unless value.start_with?("=")

  date_str, offset_expr = value[1..].split("+", 2)
  date = Date.parse(date_str)
  return date unless offset_expr

  date >> @calc.evaluate(offset_expr).to_i
end