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

Optionally retain scope when evaluating procs in collections

This allows to receive the scope as the argument to a proc-value instead
of always executing the proc in the scope's context.
This commit is contained in:
Holger Just 2017-09-21 22:30:45 +02:00
parent bcc481bfbe
commit 2f0755c967
2 changed files with 20 additions and 1 deletions

View File

@ -104,7 +104,9 @@ module Rackstash
def resolve_value(value, scope: nil)
return value unless value.is_a?(Proc)
scope.nil? ? value.call : scope.instance_exec(&value)
return value.call if scope.nil?
value.arity == 0 ? scope.instance_exec(&value) : value.call(scope)
rescue
value.inspect
end

View File

@ -261,9 +261,26 @@ describe Rackstash::Fields::AbstractCollection do
expect(normalize(hash, scope: scope)['beep']).to be_a Rackstash::Fields::Hash
expect(normalize(hash, scope: scope)['beep']['scope']).to eql 'SCOPE'
end
it 'passes the supplied scope but retains self' do
scope = 'scope'
test_self = self
hash = {
beep: ->(v) {
expect(self).to equal test_self
{ v.upcase => -> { self } }
}
}
expect(normalize(hash, scope: scope)).to be_a Rackstash::Fields::Hash
expect(normalize(hash, scope: scope)['beep']).to be_a Rackstash::Fields::Hash
expect(normalize(hash, scope: scope)['beep']['SCOPE']).to eql 'scope'
end
end
end
describe 'with Array' do
it 'wraps the array in a Rackstash::Fields::Array' do
array = ['beep', 'boop']