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

Add Rackstash::Fields::Array#<< to add a value at the end of the array

This commit is contained in:
Holger Just 2017-02-10 23:22:18 +01:00
parent ebd2a19251
commit ad038f4317
2 changed files with 29 additions and 0 deletions

View File

@ -20,6 +20,17 @@ module Rackstash
@raw[index] = normalize(value)
end
# Add a given value at the end of the array
#
# @param value [#call, Object] any value which can be serialized to JSON.
# The value will be normalized before being added so that only JSON-
# compatible objects are added into the array.
# @return [self]
def <<(value)
@raw << normalize(value)
self
end
def as_json(*)
@raw.map { |value|
value.is_a?(AbstractCollection) ? value.as_json : value

View File

@ -30,6 +30,24 @@ describe Rackstash::Fields::Array do
end
end
describe '#<<' do
it 'normalized the value' do
expect(array).to receive(:normalize).with('value').twice.and_return('normalized')
array << 'value'
expect(array[0]).to eql 'normalized'
expect(array[1]).to be nil
array << 'value'
expect(array[0]).to eql 'normalized'
expect(array[1]).to eql 'normalized'
end
it 'returns the array' do
expect(array << 'value').to equal array
end
end
describe '#as_json' do
before do
array[0] = 'value'