Module: LogStash::Util::FieldReference
- Extended by:
- FieldReference
- Included in:
- FieldReference
- Defined in:
- lib/logstash/util/fieldreference.rb
Instance Method Summary collapse
- #compile(str) ⇒ Object
-
#exec(str, obj, &block) ⇒ Object
def compile.
Instance Method Details
#compile(str) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/logstash/util/fieldreference.rb', line 6 def compile(str) if str[0,1] != '[' return " lambda do |e, &block|\n return block.call(e, \#{str.inspect}) unless block.nil?\n return e[\#{str.inspect}]\n end\n CODE\n end\n\n code = \"lambda do |e, &block|\\n\"\n selectors = str.scan(/(?<=\\[).+?(?=\\])/)\n selectors.each_with_index do |tok, i|\n last = (i == selectors.count() - 1)\n code << \" # [\#{tok}]\#{ last ? \" (last selector)\" : \"\" }\\n\"\n \n if last\n code << <<-\"CODE\"\n return block.call(e, \#{tok.inspect}) unless block.nil?\n CODE\n end\n\n code << <<-\"CODE\"\n if e.is_a?(Array)\n e = e[\#{tok.to_i}]\n else\n e = e[\#{tok.inspect}]\n end\n return e if e.nil?\n CODE\n \n end\n code << \"return e\\nend\"\n #puts code\n return code\nend\n" |
#exec(str, obj, &block) ⇒ Object
def compile
43 44 45 46 47 |
# File 'lib/logstash/util/fieldreference.rb', line 43 def exec(str, obj, &block) @__fieldeval_cache ||= {} @__fieldeval_cache[str] ||= eval(compile(str)) return @__fieldeval_cache[str].call(obj, &block) end |