mirror of
https://github.com/meineerde/rackstash.git
synced 2025-12-19 15:01:12 +00:00
Add Rackstash::Fields::Array#<< to add a value at the end of the array
This commit is contained in:
parent
ebd2a19251
commit
ad038f4317
@ -20,6 +20,17 @@ module Rackstash
|
|||||||
@raw[index] = normalize(value)
|
@raw[index] = normalize(value)
|
||||||
end
|
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(*)
|
def as_json(*)
|
||||||
@raw.map { |value|
|
@raw.map { |value|
|
||||||
value.is_a?(AbstractCollection) ? value.as_json : value
|
value.is_a?(AbstractCollection) ? value.as_json : value
|
||||||
|
|||||||
@ -30,6 +30,24 @@ describe Rackstash::Fields::Array do
|
|||||||
end
|
end
|
||||||
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
|
describe '#as_json' do
|
||||||
before do
|
before do
|
||||||
array[0] = 'value'
|
array[0] = 'value'
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user