Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ActiveModelPersistence::Index#all_objects #22

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/active_model_persistence/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,21 @@ def objects(key)
key_to_objects_map[key] || []
end

# Returns an array of all objects in the index
# @example
# i = Index.new(name: 'id', key_source: :id, unique: true)
# e1 = Employee.new(id: 1, name: 'James')
# e2 = Employee.new(id: 2, name: 'Frank')
# i.add(e1.id, e1)
# i.add(e2.id, e2)
# i.all_objects # => [e1, e2]
#
# @return [Array<Object>] all objects in the index
#
def all_objects
key_to_objects_map.values.reduce(&:concat) || []
end

# Returns true if the index contains an object with the given key
#
# @example
Expand Down
60 changes: 60 additions & 0 deletions spec/active_model_persistence/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,66 @@

describe '#include?'

describe '#all_objects' do
subject { index.all_objects }

let(:index) { described_class.new(name: 'id', key_value_source: :id, unique: true) }

context 'with no objects in the index' do
let(:expected_objects) { [] }
it { is_expected.to eq(expected_objects) }
end

context 'when there is one object in the index' do
before do
index.add_or_update(object1)
end
let(:expected_objects) { [object1] }
it { is_expected.to eq(expected_objects) }
end

context 'when there are two objects in the index' do
before do
index.add_or_update(object1)
index.add_or_update(object2)
end
let(:expected_objects) { [object1, object2] }
it { is_expected.to eq(expected_objects) }
end

context 'when the index is non-unique' do
let(:index) { described_class.new(name: 'age', key_value_source: :age, unique: false) }

let(:indexable_class) do
Class.new do
include ActiveModelPersistence::Indexable

attribute :id, :integer
attribute :name, :string
attribute :age, :integer

index :age, unique: false
end
end

context 'and two of the objects have the same key value' do
let(:object1) { indexable_class.new(id: 1, name: 'James', age: 33) }
let(:object2) { indexable_class.new(id: 2, name: 'Frank', age: 33) }
let(:object3) { indexable_class.new(id: 3, name: 'Aaron', age: 30) }

before do
index.add_or_update(object1)
index.add_or_update(object2)
index.add_or_update(object3)
end

let(:expected_objects) { [object1, object2, object3] }

it { is_expected.to eq(expected_objects) }
end
end
end

describe '#add_or_update' do
context 'for an index whose key_value_source is a proc' do
let(:index) { described_class.new(name: 'id', key_value_source: ->(object) { object.id * 100 }, unique: true) }
Expand Down