Skip to content

Commit

Permalink
add module for deprecation handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mmenanno committed Aug 28, 2024
1 parent 5a03aff commit 71cd75c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/lunchmoney/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

require_relative "exceptions"
require_relative "configuration"
require_relative "deprecate"

require_relative "calls/base"
require_relative "objects/object"
Expand Down
35 changes: 35 additions & 0 deletions lib/lunchmoney/deprecate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# typed: strict
# frozen_string_literal: true

module LunchMoney
module Deprecate
cattr_accessor :endpoint_deprecation_warnings, default: true

sig { params(replacement: T.nilable(String)).void }
def deprecate_endpoint(replacement = nil)
return unless endpoint_deprecation_warnings

replacement = if replacement.nil?
"There is currently no replacement for this endpoint"
else
"Please use #{replacement} instead"
end

message = "#{deprecated_endpoint} is deprecated and may be removed from LunchMoney | #{replacement}"
Kernel.warn(message)
end

private

sig { returns(String) }
def deprecated_endpoint
endpoint_call = Kernel.caller_locations.find { |call| call.to_s.include?("lunchmoney/calls") }

if endpoint_call.nil?
""
else
"LunchMoney::Api##{endpoint_call.label}"
end
end
end
end
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@
require_relative "helpers/mocha_typed"
require_relative "helpers/mock_response_helper"
require_relative "helpers/vcr_helper"

LunchMoney::Deprecate.endpoint_deprecation_warnings = false

0 comments on commit 71cd75c

Please sign in to comment.