Skip to content

ApiVersionChecker - Allow declarations before the methods - #404

Merged
alex-leb merged 2 commits into
masterfrom
api-version-checker-declarations
Oct 14, 2026
Merged

alex-leb merged 2 commits into
masterfrom
api-version-checker-declarations

Conversation

@alex-leb

Copy link
Copy Markdown
Contributor

enforces_api_version and requires_api_version looked each method up with instance_method as soon as they ran, so declaring them at the top of a class, before the methods, raised NameError and broke the specs. This lets the declarations sit at the top of the class.

Change

  • Each declaration now prepends a small ApiVersionChecker::Check module whose methods run the version check and then call the class's own method through super. The class methods don't have to exist yet, so the declaration works before or after them. Arguments, keyword arguments and blocks are passed through as before.
  • The enforces_api_version declarations in the 8 wrapper classes (AccountWebhooks, AnalyticsTags, ApiKeys, DomainKeys, Domains, MailingLists, Routes, Webhooks) move to the top, right after include ApiVersionChecker.

Guarding against typos

Declaring before the methods means a misspelled name no longer fails when the class loads; it would just leave the intended method unchecked. Check records the names it wraps, and a new spec loads every class that includes the checker and fails if any declared name isn't defined in that class. With :mtach added to the Routes declaration, it fails with Mailgun::Routes#mtach.

Testing

  • Helper spec: 3 new examples for declarations placed before the methods, plus the guard; 24 examples, 0 failures.
  • Full suite, run without credentials: 423 examples, 0 failures; line coverage 96.39%; RuboCop clean.
  • The guard loads only the classes that include the checker. Loading the whole gem would pull in the legacy Mailgun::Tags class, which no spec covers (25% line coverage), and drop the suite below the 95% coverage minimum.

enforces_api_version and requires_api_version looked the methods up
with instance_method as soon as they ran, so declaring them at the top
of a class raised NameError. They now prepend a module whose methods
run the check and then call the class's own method through super, so
the declaration works before or after the methods it names.

- Move the enforces_api_version declarations to the top of the 8
  wrapper classes, right after include ApiVersionChecker
- Add helper specs for declarations placed before the methods
Declaring before the methods means a misspelled name no longer raises
when the class loads; it just leaves the intended method unchecked.

- Prepend a named ApiVersionChecker::Check module that records the
  method names it wraps
- Add a spec that loads the gem and fails if any class declares a
  method it doesn't define
@alex-leb
alex-leb merged commit c6de5bd into master Oct 14, 2026
8 checks passed
@alex-leb
alex-leb deleted the api-version-checker-declarations branch October 14, 2026 17:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant