From 6c145579ed14e2e5b270d11dba07a2fd7cde0e86 Mon Sep 17 00:00:00 2001 From: Holger Just Date: Wed, 2 Aug 2017 17:37:29 +0200 Subject: [PATCH] Add FilterChain#to_a to get a plain Array representation of the chain --- lib/rackstash/filter_chain.rb | 7 +++++++ spec/rackstash/filter_chain_spec.rb | 19 ++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) 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