Skip to content

Commit 5abf4d8

Browse files
committed
❇️ Entities now implements and includes Enumerable
Which adds a whole bunch of useful methods to the class. Also, `to_a` was removed as it is now implemented by `Enumerable`.
1 parent e2940ae commit 5abf4d8

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

lib/netbox_client_ruby/entities.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
module NetboxClientRuby
55
module Entities
66
include NetboxClientRuby::Communication
7+
include Enumerable
78

89
def self.included(other_klass)
910
other_klass.extend ClassMethods
@@ -120,8 +121,8 @@ def [](index)
120121
as_entity raw_data_array[index]
121122
end
122123

123-
def to_a
124-
raw_data_array.map { |raw_entity| as_entity raw_entity }
124+
def each
125+
raw_data_array.each { |raw_entity| yield as_entity(raw_entity) }
125126
end
126127

127128
##

spec/netbox_client_ruby/entities_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,25 @@ class TestEntities3
115115
end
116116
end
117117

118+
describe '#each' do
119+
it 'yields all items once' do
120+
count = 0
121+
subject.each do |_entity|
122+
count += 1
123+
end
124+
125+
expect(count).to eq(3)
126+
end
127+
128+
it 'yields a single entity of the expected type' do
129+
subject.each do |entity|
130+
expect(entity).to be_a OpenStruct
131+
expect(entity).to respond_to :data
132+
expect(entity.data).to be_a Hash
133+
end
134+
end
135+
end
136+
118137
describe '#[]' do
119138
it 'returns the requested item as correct type' do
120139
expect(subject[0]).to be_a OpenStruct

0 commit comments

Comments
 (0)