mirror of
https://github.com/meineerde/rackstash.git
synced 2025-10-17 14:01:01 +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:
parent
bcc481bfbe
commit
2f0755c967
@ -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
|
||||
|
||||
@ -261,7 +261,24 @@ 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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user