Skip to content

Commit

Permalink
Merge pull request #182 from halorrr/improve-budget-recurring-type
Browse files Browse the repository at this point in the history
add better typing to recurring field on budget returns
  • Loading branch information
mmenanno committed Jan 31, 2024
2 parents 0640a78 + ed2d42d commit 61c4c05
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 37 deletions.
5 changes: 4 additions & 1 deletion lib/lunchmoney/budget/budget.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class Budget < LunchMoney::DataObject
sig { returns(T.nilable(T::Hash[Symbol, LunchMoney::Config])) }
attr_accessor :config

sig { returns(T.nilable({ list: T::Array[LunchMoney::RecurringExpense] })) }
attr_accessor :recurring

sig do
params(
is_income: T::Boolean,
Expand All @@ -47,7 +50,7 @@ class Budget < LunchMoney::DataObject
group_id: T.nilable(Integer),
is_group: T.nilable(T::Boolean),
config: T.nilable(T::Hash[Symbol, LunchMoney::Config]),
recurring: T.untyped, # TODO: Better type this field
recurring: T.nilable({ list: T::Array[LunchMoney::RecurringExpense] }),
).void
end
def initialize(is_income:, exclude_from_budget:, exclude_from_totals:, data:, category_name:, order:, archived:,
Expand Down
4 changes: 4 additions & 0 deletions lib/lunchmoney/budget/budget_calls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def budgets(start_date:, end_date:, currency: nil)
end
end

if budget[:recurring]
budget[:recurring][:list]&.map! { |recurring| LunchMoney::RecurringExpenseBase.new(**recurring) }
end

LunchMoney::Budget.new(**budget)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

module LunchMoney
# https://lunchmoney.dev/#recurring-expenses-object
class RecurringExpense < LunchMoney::DataObject
class RecurringExpense < RecurringExpenseBase
sig { returns(Integer) }
attr_accessor :id

sig { returns(T.nilable(String)) }
attr_accessor :start_date, :end_date, :description, :original_name

sig { returns(String) }
attr_accessor :cadence, :payee, :currency, :billing_date, :type, :source, :amount, :created_at
attr_accessor :cadence, :billing_date, :type, :source, :created_at

sig { returns(T.nilable(Integer)) }
attr_accessor :plaid_account_id, :asset_id, :transaction_id, :category_id
Expand All @@ -35,12 +35,13 @@ class RecurringExpense < LunchMoney::DataObject
plaid_account_id: T.nilable(Integer),
asset_id: T.nilable(Integer),
transaction_id: T.nilable(Integer),
to_base: T.nilable(Integer),
).void
end
def initialize(cadence:, payee:, amount:, currency:, billing_date:, type:, source:, id:, created_at:,
category_id: nil, start_date: nil, end_date: nil, description: nil, original_name: nil, plaid_account_id: nil,
asset_id: nil, transaction_id: nil)
super()
asset_id: nil, transaction_id: nil, to_base: nil)
super(payee:, amount:, currency:, to_base:)
@cadence = cadence
@payee = payee
@amount = amount
Expand All @@ -58,6 +59,7 @@ def initialize(cadence:, payee:, amount:, currency:, billing_date:, type:, sourc
@plaid_account_id = plaid_account_id
@asset_id = asset_id
@transaction_id = transaction_id
@to_base = to_base
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# typed: strict
# frozen_string_literal: true

module LunchMoney
# https://lunchmoney.dev/#recurring-expenses-object
class RecurringExpenseBase < LunchMoney::DataObject
sig { returns(String) }
attr_accessor :payee, :currency, :amount

sig { returns(T.nilable(Integer)) }
attr_accessor :to_base

sig do
params(
payee: String,
amount: String,
currency: String,
to_base: T.nilable(Integer),
).void
end
def initialize(payee:, amount:, currency:, to_base:)
super()
@payee = payee
@amount = amount
@currency = currency
@to_base = to_base
end
end
end
3 changes: 2 additions & 1 deletion lib/lunchmoney/recurring_expenses/recurring_expense_calls.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# typed: strict
# frozen_string_literal: true

require_relative "recurring_expense"
require_relative "recurring_expense/recurring_expense_base"
require_relative "recurring_expense/recurring_expense"

module LunchMoney
# https://lunchmoney.dev/#recurring-expenses
Expand Down
Loading

0 comments on commit 61c4c05

Please sign in to comment.