Include Enumerable in Collection (#each_with_object etc.) - #128
Merged
Conversation
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
Contributor
There was a problem hiding this comment.
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 EnumerabletoComplianceEngine::Collection. - Add RSpec coverage ensuring Enumerable methods work and that
#select/#rejectstill 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.
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.
silug
approved these changes
Jun 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
ComplianceEngine::Collection(and subclassesCes,Checks,Controls,Profiles) define#eachbut do notinclude Enumerable, so the standard Enumerable methods raise on every collection: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 EnumerableinComplianceEngine::Collection. Because the class defines#each(yielding[name, component]pairs viato_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.rbgains anEnumerable interfacegroup:each_with_objectover[name, component]pairs (the regression),#map/#find,#select/#rejectstill return Collections (no regression from the mixin).bundle exec rspec spec/classes/compliance_engine/{ces,collection,checks,controls,component,ce}_spec.rb→ 50 examples, 0 failures; RuboCop clean.Fixes #127