1
0
mirror of https://github.com/meineerde/rackstash.git synced 2025-10-17 14:01:01 +00:00

Allow multiple arguments to Rackstash::Fields::Array#push

We now also support the new alias to `Array#push` called `append` which
will be added to the core `Array` with Ruby 2.5.
This commit is contained in:
Holger Just 2017-06-16 23:16:38 +02:00
parent 6eccf6cc37
commit 649de80698
2 changed files with 39 additions and 7 deletions

View File

@ -85,7 +85,6 @@ module Rackstash
@raw << normalize(value) @raw << normalize(value)
self self
end end
alias push <<
# @return [::Array] deep-transforms the array into a plain Ruby Array # @return [::Array] deep-transforms the array into a plain Ruby Array
def as_json(*) def as_json(*)
@ -160,6 +159,20 @@ module Rackstash
self self
end end
# Append — Pushes the given object(s) on to the end of this array. All
# values will be normalized before being added. This method returns the
# array itself, so several appends may be chained together.
#
# @param values [::Array] a list of values to append at the end of `self`
# @param scope [Object, nil] if any of the (deeply-nested) values is a
# proc, it will be called in the instance scope of this object (when
# given).
# @return [self]
def push(*values, scope: nil)
concat(values, scope: scope)
end
alias append push
private private
def implicit(obj) def implicit(obj)

View File

@ -96,15 +96,13 @@ describe Rackstash::Fields::Array do
expect(array[1]).to eql 'normalized' expect(array[1]).to eql 'normalized'
end end
it 'can append only one value' do
expect { array.<< 'foo', 'bar' }.to raise_error ArgumentError
end
it 'returns the array' do it 'returns the array' do
expect(array << 'value').to equal array expect(array << 'value').to equal array
end end
it 'can use push as an alias' do
expect(array.push 'value').to equal array
expect(array[0]).to eql 'value'
end
end end
describe '#as_json' do describe '#as_json' do
@ -270,6 +268,27 @@ describe Rackstash::Fields::Array do
end end
end end
describe '#push' do
it 'can append multiple values' do
expect(array.push 'value', 'value2').to equal array
expect(array[0]).to eql 'value'
expect(array[1]).to eql 'value2'
end
it 'appends arrays as is' do
value = ['hello']
array.push value
expect(array[0]).to be_a Rackstash::Fields::Array
expect(array[0].to_a).to eql value
end
it 'can use append as an alias' do
expect(array.append 'foo').to equal array
expect(array[0]).to eql 'foo'
end
end
describe 'Converter' do describe 'Converter' do
it 'creates a new array' do it 'creates a new array' do
raw = [Time.now, 'foo'] raw = [Time.now, 'foo']