Skip to content

Improve collection ergonomics and add Base#empty? default semantics - #2

Open
mjgiarlo wants to merge 1 commit into
joshmn:mainfrom
sul-dlss-labs:association-enumerability
Open

Improve collection ergonomics and add Base#empty? default semantics#2
mjgiarlo wants to merge 1 commit into
joshmn:mainfrom
sul-dlss-labs:association-enumerability

Conversation

@mjgiarlo

Copy link
Copy Markdown

This PR improves day-to-day form-object ergonomics by expanding collection behavior on Blanks::AssociationProxy and adding default empty? semantics to Blanks::Base.

  • Kept association emptiness behavior (AssociationProxy#empty?) as a direct core API and documented adjacent collection ergonomics.
  • Updated AssociationProxy#any? to support both no-block and block forms.
  • Added AssociationProxy#reverse to return records in reverse order.
  • Added Base#empty? with default semantics: all attribute values are blank.
  • Added spec coverage for the above changes.
  • Updated README with collection proxy and empty? usage notes.
  • Added CHANGELOG entries under Unreleased.

Apps using Blanks often treat has_many associations as collections. Without block-aware any? and reverse, callers must unwrap via to_a for routine operations.

Likewise, many applications define identical empty? methods in local base forms. Moving this to Blanks::Base provides a shared default and reduces boilerplate.

  • AssociationProxy#any?:

    • proxy.any? works as before.
    • proxy.any? { |item| ... } now evaluates the block against members.
  • AssociationProxy#reverse:

    • Returns an array in reverse order.
    • Does not mutate the proxy's underlying order.
  • Base#empty?:

    • Returns true when all attributes values are blank.
    • Returns false when any attribute is present.
    • Subclasses may still override for custom emptiness semantics.
  • Backward compatible for existing any? calls without a block.

  • to_a remains available and unchanged.

  • Adding Base#empty? may affect classes that relied on NoMethodError, but aligns with common form-object expectations.

This PR improves day-to-day form-object ergonomics by expanding collection behavior on `Blanks::AssociationProxy` and adding default `empty?` semantics to `Blanks::Base`.

- Kept association emptiness behavior (`AssociationProxy#empty?`) as a direct core API and documented adjacent collection ergonomics.
- Updated `AssociationProxy#any?` to support both no-block and block forms.
- Added `AssociationProxy#reverse` to return records in reverse order.
- Added `Base#empty?` with default semantics: all attribute values are blank.
- Added spec coverage for the above changes.
- Updated README with collection proxy and `empty?` usage notes.
- Added CHANGELOG entries under Unreleased.

Apps using Blanks often treat `has_many` associations as collections. Without block-aware `any?` and `reverse`, callers must unwrap via `to_a` for routine operations.

Likewise, many applications define identical `empty?` methods in local base forms. Moving this to `Blanks::Base` provides a shared default and reduces boilerplate.

- `AssociationProxy#any?`:
  - `proxy.any?` works as before.
  - `proxy.any? { |item| ... }` now evaluates the block against members.
- `AssociationProxy#reverse`:
  - Returns an array in reverse order.
  - Does not mutate the proxy's underlying order.
- `Base#empty?`:
  - Returns true when all `attributes` values are blank.
  - Returns false when any attribute is present.
  - Subclasses may still override for custom emptiness semantics.

- Backward compatible for existing `any?` calls without a block.
- `to_a` remains available and unchanged.
- Adding `Base#empty?` may affect classes that relied on `NoMethodError`, but aligns with common form-object expectations.
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