Skip to content

Readability pass: guard clauses + small refactors across lib/#2741

Open
ericproulx wants to merge 1 commit into
masterfrom
chore/readability-guard-clauses
Open

Readability pass: guard clauses + small refactors across lib/#2741
ericproulx wants to merge 1 commit into
masterfrom
chore/readability-guard-clauses

Conversation

@ericproulx
Copy link
Copy Markdown
Contributor

Summary

Mostly cosmetic. Lifts if/elsif/else chains into guard clauses across dsl/, middleware/, router/, formatter/, and a few small extractions. No behavior change except where noted below.

Guard-clause lifts (the short-circuit moves above the if, main flow sits at top indentation):

  • DSL::Logger#logger, DSL::InsideRoute#stream / #return_no_content
  • Formatter::SerializableHash#serialize
  • Middleware::Base#merge_default_options
  • Middleware::Error#redispatch
  • Middleware::Formatter#call! / #negotiate_content_type
  • Router::Pattern#build_path_from_pattern
  • ServeStream::SendfileResponse#respond_to?
  • Validations::ParamsScope#configure_declared_params
  • Exceptions::ErrorResponse.coerce (case/when reformatted to multi-line)

Small extractions:

  • DSL::Entity#entity_class_for_obj: extract object_class(object) private helper.
  • DSL::InsideRoute#status: collapse Symbol/Integer branches; extract default_status private helper.
  • DSL::InsideRoute#stream: extract stream_body(value) private helper.
  • DSL::InsideRoute#return_no_content: drop redundant status 204body false already sets it via the setter.

Tiny: return nil unlessreturn unless in Grape::API::Boolean.build and PrimitiveCoercer#call.

Behavior note

DSL::InsideRoute#status with an unknown symbol now raises with Rack::Utils.status_code's native message ("Unrecognized status code :foo_bar") instead of Grape's pre-validation message ("Status code :foo_bar is invalid."). One spec updated to match.

Test plan

  • bundle exec rspec — 2313 examples, 0 failures
  • bundle exec rubocop — clean on touched files
  • CI green

🤖 Generated with Claude Code

@ericproulx ericproulx force-pushed the chore/readability-guard-clauses branch from 02c0571 to da11b68 Compare May 23, 2026 18:39
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 23, 2026

Danger Report

No issues found.

View run

@ericproulx ericproulx requested a review from dblock May 23, 2026 18:39
Mostly cosmetic, no behavior change except where noted.

Guard-clause lifts (lift the short-circuit above the if/elsif/else,
let the main flow sit at top indentation):

- `Grape::DSL::Logger#logger`, `DSL::InsideRoute#stream`/`#return_no_content`
- `Grape::Formatter::SerializableHash#serialize`
- `Grape::Middleware::Base#merge_default_options`
- `Grape::Middleware::Error#redispatch`
- `Grape::Middleware::Formatter` (`call!`, `negotiate_content_type`)
- `Grape::Router::Pattern#build_path_from_pattern`
- `Grape::ServeStream::SendfileResponse#respond_to?`
- `Grape::Validations::ParamsScope#configure_declared_params`
- `Grape::Exceptions::ErrorResponse.coerce` (case/when reformatted)

Small extractions:

- `DSL::Entity#entity_class_for_obj`: extract `object_class(object)`
  private helper.
- `DSL::InsideRoute#status`: collapse Symbol/Integer branches and
  extract `default_status` private helper. Error message for an
  unknown status symbol now bubbles up `Rack::Utils.status_code`'s
  own message ("Unrecognized status code :foo_bar") instead of
  Grape's pre-validation message ("Status code :foo_bar is invalid.");
  spec updated.
- `DSL::InsideRoute#stream`: extract `stream_body(value)` private
  helper.
- `DSL::InsideRoute#return_no_content`: drop the redundant `status 204`
  call — `body false` already sets it via the `body` setter.

Minor: `return nil unless` → `return unless` in `Grape::API::Boolean.build`
and `PrimitiveCoercer#call`.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@ericproulx ericproulx force-pushed the chore/readability-guard-clauses branch from da11b68 to 4c9bbaf Compare May 26, 2026 18:50
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.

2 participants