lib/jsonpathv2/enumerable.rb in jsonpathv2-0.0.6 vs lib/jsonpathv2/enumerable.rb in jsonpathv2-0.0.7

- old
+ new

@@ -27,11 +27,11 @@ each(context, key, pos + 1, &blk) if node == @object when /^\[(.*)\]$/ handle_wildecard(node, expr, context, key, pos, &blk) else if pos == (@path.size - 1) && node && allow_eval? - yield_value(blk, context, key) if eval("node #{@path[pos]}") + yield_value(blk, context, key) if instance_eval("node #{@path[pos]}") end end if pos > 0 && @path[pos - 1] == '..' case node @@ -114,33 +114,38 @@ return default if exp.nil? || exp.empty? return Integer(exp) if exp[0] != '(' return nil unless allow_eval? && @_current_node identifiers = /@?(\.(\w+))+/.match(exp) + # puts JsonPath.on(@_current_node, "#{identifiers}") unless identifiers.nil? || + # @_current_node + # .methods + # .include?(identifiers[2].to_sym) - if !identifiers.nil? && - !@_current_node.methods.include?(identifiers[2].to_sym) + unless identifiers.nil? || + @_current_node.methods.include?(identifiers[2].to_sym) exp_to_eval = exp.dup exp_to_eval[identifiers[0]] = identifiers[0].split('.').map do |el| el == '@' ? '@_current_node' : "['#{el}']" end.join begin - return eval(exp_to_eval) + return instance_eval(exp_to_eval) # if eval failed because of bad arguments or missing methods rescue StandardError return default end end + # otherwise eval as is # TODO: this eval is wrong, because hash accessor could be nil and nil # cannot be compared with anything, for instance, # @a_current_node['price'] - we can't be sure that 'price' are in every # node, but it's only in several nodes I wrapped this eval into rescue # returning false when error, but this eval should be refactored. begin - eval(exp.gsub(/@/, '@_current_node')) + instance_eval(exp.gsub(/@/, '@_current_node')) rescue false end end end