diff --git a/lib/rackstash/filter_chain.rb b/lib/rackstash/filter_chain.rb index 91d4bf3..ff6d7e1 100644 --- a/lib/rackstash/filter_chain.rb +++ b/lib/rackstash/filter_chain.rb @@ -259,6 +259,13 @@ module Rackstash self end + # Returns an Array representation of the filter chain. + # + # @return [Array<#call>] an array of filters + def to_a + synchronize { @filters.dup } + end + # @return [String] an Array-compatible string representation of `self` def to_s synchronize { @filters.to_s } diff --git a/spec/rackstash/filter_chain_spec.rb b/spec/rackstash/filter_chain_spec.rb index 72c343b..8adf6c6 100644 --- a/spec/rackstash/filter_chain_spec.rb +++ b/spec/rackstash/filter_chain_spec.rb @@ -435,11 +435,28 @@ describe Rackstash::FilterChain do end end + describe '#to_a' do + it 'returns the array representation' do + filter_chain << filter + + expect(filter_chain.to_a) + .to be_instance_of(Array) + .and all be_equal(filter) + end + + it 'returns a duplicate' do + filter_chain << a_filter + array = filter_chain.to_a + + expect { array << a_filter }.not_to change { filter_chain.length } + end + end + describe '#to_s' do it 'returns the array representation' do filter_chain << -> {} - expect(filter_chain.to_s).to eql filter_chain.each.to_a.to_s + expect(filter_chain.to_s).to eql filter_chain.to_a.to_s end end end