1
0
mirror of https://github.com/meineerde/rackstash.git synced 2025-12-19 15:01:12 +00:00

Allow to specify a custom scope in Rackstash::Fields::Array#concat

This commit is contained in:
Holger Just 2017-02-16 00:41:40 +01:00
parent 8232d139f4
commit c401a29ab3
2 changed files with 10 additions and 2 deletions

View File

@ -67,9 +67,11 @@ module Rackstash
# #
# @param array [Array, ::Array] an array of values. Each value is # @param array [Array, ::Array] an array of values. Each value is
# normalized before being added to `self`. # normalized before being added to `self`.
# @param scope [Object] if `array` or any of its (deeply-nested) values is
# a proc, it will be called in the instance scope of this object.
# @return [self] # @return [self]
def concat(array) def concat(array, scope: nil)
array = Array(normalize(array, wrap: false)) array = Array(normalize(array, wrap: false, scope: scope))
@raw.concat(array) @raw.concat(array)
self self
end end

View File

@ -143,6 +143,12 @@ describe Rackstash::Fields::Array do
it 'resolves nested procs' do it 'resolves nested procs' do
expect(array.concat(-> { [-> { :foo } ] } )).to contain_exactly 'foo' expect(array.concat(-> { [-> { :foo } ] } )).to contain_exactly 'foo'
end end
it 'resolves nested procs with a custom scope' do
expect(
array.concat(-> { [self, -> { self.to_s.upcase } ] }, scope: :stuff )
).to contain_exactly 'stuff', 'STUFF'
end
end end
describe '#empty?' do describe '#empty?' do