Method: JSI::JSON::Pointer#evaluate
- Defined in:
- lib/jsi/json/pointer.rb
#evaluate(document) ⇒ Object
takes a root json document and evaluates this pointer through the document, returning the value pointed to by this pointer.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/jsi/json/pointer.rb', line 84 def evaluate(document) res = reference_tokens.inject(document) do |value, token| if value.respond_to?(:to_ary) if token.is_a?(String) && token =~ /\A\d|[1-9]\d+\z/ token = token.to_i end unless token.is_a?(Integer) raise(ReferenceError, "Invalid resolution for #{to_s}: #{token.inspect} is not an integer and cannot be resolved in array #{value.inspect}") end unless (0...(value.respond_to?(:size) ? value : value.to_ary).size).include?(token) raise(ReferenceError, "Invalid resolution for #{to_s}: #{token.inspect} is not a valid index of #{value.inspect}") end (value.respond_to?(:[]) ? value : value.to_ary)[token] elsif value.respond_to?(:to_hash) unless (value.respond_to?(:key?) ? value : value.to_hash).key?(token) raise(ReferenceError, "Invalid resolution for #{to_s}: #{token.inspect} is not a valid key of #{value.inspect}") end (value.respond_to?(:[]) ? value : value.to_hash)[token] else raise(ReferenceError, "Invalid resolution for #{to_s}: #{token.inspect} cannot be resolved in #{value.inspect}") end end res end |