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.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/jsi/json/pointer.rb', line 106 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 |