Skip to content

Include Enumerable in Collection (#each_with_object etc.) - #128

Merged
silug merged 2 commits into
mainfrom
include-enumerable-in-collection
Jun 11, 2026
Merged

Include Enumerable in Collection (#each_with_object etc.)#128
silug merged 2 commits into
mainfrom
include-enumerable-in-collection

Conversation

@jaevans

@jaevans jaevans commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Problem

ComplianceEngine::Collection (and subclasses Ces, Checks, Controls, Profiles) define #each but do not include Enumerable, so the standard Enumerable methods raise on every collection:

ces.each_with_object({}) { |(name, ce), acc| ... }
# => NoMethodError: undefined method 'each_with_object' for an instance of ComplianceEngine::Ces

A downstream consumer building an OVAL-id → CE reverse index with ces.each_with_object({}) crashed on this — the object walks like an enumerable (#each, #to_h, #select, #reject) but isn't one. See #127.

Fix

include Enumerable in ComplianceEngine::Collection. Because the class defines #each (yielding [name, component] pairs via to_h.each), this fills in #each_with_object, #map, #reduce, #find, #count, etc. for all collection types.

The explicit hash-like methods (#select, #reject, #keys) are defined on the class, so they shadow Enumerable's and keep returning Collections rather than Arrays (the behavior from #37) — verified by a test.

Tests

spec/classes/compliance_engine/ces_spec.rb gains an Enumerable interface group:

  • each_with_object over [name, component] pairs (the regression),
  • #map / #find,
  • #select / #reject still return Collections (no regression from the mixin).

bundle exec rspec spec/classes/compliance_engine/{ces,collection,checks,controls,component,ce}_spec.rb50 examples, 0 failures; RuboCop clean.

Fixes #127

Collections defined #each but did not include Enumerable, so
#each_with_object, #map, #reduce, #find, etc. raised NoMethodError on Ces,
Checks, Controls and Profiles. Mix in Enumerable; the explicit hash-like
#select/#reject/#keys shadow Enumerable's and keep returning Collections.

Fixes #127

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR makes ComplianceEngine::Collection (and therefore Ces, Checks, Controls, Profiles) a proper Ruby Enumerable by mixing in Enumerable, enabling standard methods like #each_with_object, #map, and #find when #each is defined.

Changes:

  • Add include Enumerable to ComplianceEngine::Collection.
  • Add RSpec coverage ensuring Enumerable methods work and that #select/#reject still return Collections.
  • Document the behavior change in CHANGELOG.md.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
lib/compliance_engine/collection.rb Mixes in Enumerable to provide the standard enumerable interface based on existing #each.
spec/classes/compliance_engine/ces_spec.rb Adds regression + interface tests for Enumerable behavior and Collection-returning #select/#reject.
CHANGELOG.md Notes the new Enumerable support for collections.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/compliance_engine/collection.rb Outdated
Comment thread CHANGELOG.md Outdated
Enumerable has no #keys, and Collection#keys is to_h.keys (an Array), so it
neither shadows Enumerable nor returns a Collection. Drop it from the comment
and changelog note; only #select/#reject return Collections.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

@silug
silug merged commit 0c9fa12 into main Jun 11, 2026
15 checks passed
@silug
silug deleted the include-enumerable-in-collection branch June 11, 2026 19:29
@github-project-automation github-project-automation Bot moved this from New to Done in Org Triage Jun 11, 2026
@silug silug removed this from Org Triage Jun 22, 2026
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.

Collections define #each but don't include Enumerable (#each_with_object etc. raise NoMethodError)

4 participants